insika 0.2.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 +4 -4
- data/CHANGELOG.md +145 -17
- data/README.md +7 -1
- data/bin/insika +211 -8
- data/docs/AGENTS.md +377 -2
- data/docs/API.md +56 -0
- data/docs/CHANNELS.md +99 -9
- data/docs/CONTEXT.md +127 -16
- data/docs/DEPLOY.md +5 -2
- data/docs/EVALS.md +19 -0
- data/docs/FACTS.md +135 -0
- data/docs/HARVEST.md +117 -0
- data/docs/LOADTEST.md +14 -8
- data/docs/OBSERVABILITY.md +54 -0
- data/docs/REFINEMENT.md +3 -3
- data/docs/RELEASING.md +27 -0
- data/docs/RUNNING-LOCAL.md +3 -3
- data/docs/SECURITY.md +61 -0
- data/docs/SKILLS.md +2 -2
- data/docs/SOAK.md +127 -0
- data/docs/TOOLS.md +70 -2
- data/docs/domain.md +115 -0
- data/docs/index.md +1 -1
- data/lib/insika/agent_profile.rb +145 -10
- 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 +118 -13
- data/lib/insika/channels/relay.rb +77 -3
- data/lib/insika/channels/web.rb +2 -2
- data/lib/insika/chat_builder.rb +84 -1
- data/lib/insika/checkpoint_store.rb +16 -0
- data/lib/insika/commands/agent_payload.rb +3 -3
- data/lib/insika/commands/cancel_followup.rb +49 -0
- data/lib/insika/commands/delete_tenant_data.rb +95 -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/reject_harvest.rb +38 -0
- data/lib/insika/commands/resolve_proposal.rb +108 -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/send_message.rb +103 -6
- data/lib/insika/commands/session_purge.rb +67 -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 +3 -0
- data/lib/insika/context/provider.rb +17 -3
- data/lib/insika/context/providers/briefing.rb +96 -0
- data/lib/insika/context/providers/memory.rb +16 -7
- data/lib/insika/context/providers/prompt.rb +30 -2
- 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/distill.rb +224 -0
- data/lib/insika/distill_engine.rb +169 -0
- data/lib/insika/doctor.rb +716 -6
- data/lib/insika/dsl/runtime.rb +9 -3
- data/lib/insika/dsl/server_boot.rb +70 -1
- data/lib/insika/dsl.rb +83 -3
- data/lib/insika/edge_limiter.rb +28 -7
- data/lib/insika/env_schema.rb +9 -2
- data/lib/insika/errors.rb +11 -0
- data/lib/insika/evals/assertions.rb +3 -2
- data/lib/insika/evals/pairwise.rb +18 -6
- data/lib/insika/evidence.rb +183 -0
- data/lib/insika/executor.rb +687 -70
- data/lib/insika/followup_engine.rb +207 -0
- data/lib/insika/followup_policy.rb +221 -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 +1 -1
- 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/media.rb +298 -0
- data/lib/insika/memory_audit_store.rb +85 -0
- data/lib/insika/memory_store.rb +264 -23
- data/lib/insika/message_origin.rb +6 -1
- data/lib/insika/model_visible.rb +87 -0
- data/lib/insika/model_visible_trace_store.rb +66 -0
- data/lib/insika/onboarding.rb +6 -1
- data/lib/insika/outbox_store.rb +42 -4
- data/lib/insika/outcome_store.rb +147 -0
- 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 +22 -1
- 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 +281 -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/schema_guard.rb +35 -0
- data/lib/insika/server/app.rb +144 -4
- data/lib/insika/server/rack_app.rb +16 -0
- 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 +4 -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/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 +875 -14
- data/lib/insika/studio/assets/dist/application.css +1 -1
- data/lib/insika/studio/assets/dist/application.js +22 -22
- data/lib/insika/studio/assets/dist/favicon.svg +6 -0
- data/lib/insika/studio/forms.rb +262 -5
- data/lib/insika/studio/nav_icons.rb +6 -1
- data/lib/insika/studio/views/agent_detail.erb +627 -84
- data/lib/insika/studio/views/agents.erb +10 -6
- data/lib/insika/studio/views/approvals.erb +4 -1
- 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 +4 -1
- 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 +2 -1
- data/lib/insika/studio/views/layout.erb +1 -0
- 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 +56 -1
- data/lib/insika/studio/views/settings.erb +2 -0
- data/lib/insika/studio/views/skills.erb +0 -1
- 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 +0 -1
- data/lib/insika/task_store.rb +21 -1
- data/lib/insika/testing/store_contract.rb +27 -6
- data/lib/insika/tick.rb +25 -1
- 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 +44 -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_state.rb +38 -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 +300 -13
- data/lib/insika.rb +92 -3
- metadata +77 -1
data/lib/insika/media.rb
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Insika
|
|
4
|
+
# WS9: the engine transports MEDIA, never meaning. Content parts ride the
|
|
5
|
+
# message contract — `{ "type": "text", "text": … }`, `{ "type": "image",
|
|
6
|
+
# "url": … }`, `{ "type": "audio", "url": … }` — and the Executor turns them
|
|
7
|
+
# into a turn: audio is transcribed (text marked `source: :voice`), images
|
|
8
|
+
# attach to the model ask and the first URL is `{{ctx.image_url}}` for
|
|
9
|
+
# data/HTTP tools. This class owns the PURE parts (normalization) and
|
|
10
|
+
# the STT SEAM (injectable — specs stub it; the default fetches the audio and
|
|
11
|
+
# transcribes via RubyLLM behind a lazy require, so the core stays gem-free
|
|
12
|
+
# at load). `Media::Output` is the generated-media half (WS9, saída): the
|
|
13
|
+
# turn can PRODUCE an image or an audio clip when the agent opted in
|
|
14
|
+
# (`AgentProfile#outputs`) AND the channel declared it can receive it
|
|
15
|
+
# (`channel.capabilities`) — nothing leaks by default.
|
|
16
|
+
module Media
|
|
17
|
+
# A single content part, normalized.
|
|
18
|
+
Part = Data.define(:type, :text, :url) do
|
|
19
|
+
def audio? = type == "audio"
|
|
20
|
+
def image? = type == "image"
|
|
21
|
+
def text? = type == "text"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# -> [Part]: normalize the raw parts (string|symbol keys), skipping anything
|
|
25
|
+
# that is not a well-formed text/image/audio part. Lenient on purpose — the
|
|
26
|
+
# SURFACE validates the contract with `well_formed?` (a malformed part is a
|
|
27
|
+
# 422 before dispatch); here a stray entry must not break the turn.
|
|
28
|
+
def self.parts(raw)
|
|
29
|
+
Array(raw).filter_map do |p|
|
|
30
|
+
next unless p.is_a?(Hash)
|
|
31
|
+
|
|
32
|
+
type = (p[:type] || p["type"]).to_s
|
|
33
|
+
url = (p[:url] || p["url"]).to_s
|
|
34
|
+
text = (p[:text] || p["text"]).to_s
|
|
35
|
+
case type
|
|
36
|
+
when "text" then text.empty? ? nil : Part.new("text", text, nil)
|
|
37
|
+
when "image", "audio" then url.empty? ? nil : Part.new(type, nil, url)
|
|
38
|
+
else nil
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The SURFACE's contract check (server edge): true when EVERY entry is a
|
|
44
|
+
# well-formed content part — a Hash whose type is text (with text), image
|
|
45
|
+
# or audio (with url). The edge raises a 422 on the first offender; the
|
|
46
|
+
# engine itself stays lenient (`parts` skips strays so a non-HTTP transport
|
|
47
|
+
# that bypassed the edge cannot break a turn).
|
|
48
|
+
def self.well_formed?(raw)
|
|
49
|
+
Array(raw).all? do |p|
|
|
50
|
+
next false unless p.is_a?(Hash)
|
|
51
|
+
|
|
52
|
+
case (p[:type] || p["type"]).to_s
|
|
53
|
+
when "text" then !(p[:text] || p["text"]).to_s.empty?
|
|
54
|
+
when "image", "audio" then !(p[:url] || p["url"]).to_s.empty?
|
|
55
|
+
# a part WITHOUT a type is admitted only as a bare text part (the
|
|
56
|
+
# shape the input joiner already tolerates) — anything else is refused.
|
|
57
|
+
when "" then !(p[:text] || p["text"]).to_s.empty?
|
|
58
|
+
else false
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The OUTPUT media kinds a channel may declare it can receive
|
|
64
|
+
# (`channel.capabilities`). The closed list is the "abstraction admits
|
|
65
|
+
# only what leaks" rule: an unknown value is refused at the edge, never
|
|
66
|
+
# silently ignored.
|
|
67
|
+
OUTPUT_CAPABILITIES = %w[image_output audio_output].freeze
|
|
68
|
+
|
|
69
|
+
# -> [String]: the capabilities a raw `channel` hash declares. Lenient on
|
|
70
|
+
# the key spelling (symbol|string) at both boundaries (request parse vs
|
|
71
|
+
# persisted command payload); [] = the channel declared nothing.
|
|
72
|
+
def self.channel_capabilities(raw)
|
|
73
|
+
channel = raw.is_a?(Hash) ? raw : {}
|
|
74
|
+
Array(channel[:capabilities] || channel["capabilities"]).map(&:to_s)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The ceilings on INBOUND media (a URL a consumer sent us). Both fetches
|
|
78
|
+
# stream into the cap and refuse past it: the bytes land in THIS process,
|
|
79
|
+
# so an uncapped one is a hostile URL away from growing it until it dies.
|
|
80
|
+
MAX_AUDIO_BYTES = 1_000_000 # a voice note, not a warehouse
|
|
81
|
+
MAX_IMAGE_BYTES = 5_000_000 # a photo, not a poster
|
|
82
|
+
|
|
83
|
+
def self.audio_parts(parts) = parts.select(&:audio?)
|
|
84
|
+
def self.image_parts(parts) = parts.select(&:image?)
|
|
85
|
+
|
|
86
|
+
# The STT seam: ->(url) { text } (default: fetch + RubyLLM transcription).
|
|
87
|
+
# Injected so a spec never touches the network; the default is built lazily
|
|
88
|
+
# when the turn first carries audio.
|
|
89
|
+
def self.default_transcriber(stt_model:, stt_language: nil)
|
|
90
|
+
lambda do |url|
|
|
91
|
+
fetch_and_transcribe(url, model: stt_model, language: stt_language)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def self.fetch_and_transcribe(url, model:, language:)
|
|
96
|
+
require "net/http"
|
|
97
|
+
require "uri"
|
|
98
|
+
require "ruby_llm" # lazy — the core loads without it (load-guard)
|
|
99
|
+
|
|
100
|
+
bytes = fetch_binary(url)
|
|
101
|
+
audio = RubyLLM::Attachment.new(bytes)
|
|
102
|
+
options = { model: model, assume_model_exists: true }
|
|
103
|
+
options[:language] = language if language
|
|
104
|
+
RubyLLM::Transcription.transcribe(audio, **options).text
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Egress-guarded binary fetch of a media URL. Blocked like the webhook: the
|
|
108
|
+
# url is consumer config/input, so a private/loopback/metadata target is
|
|
109
|
+
# refused (SSRF) unless the deployment opts out. Size-capped (the caller
|
|
110
|
+
# picks the ceiling; the default is the audio one).
|
|
111
|
+
def self.fetch_binary(url, max_bytes: MAX_AUDIO_BYTES)
|
|
112
|
+
violation = Insika::EgressGuard.violation(url, **egress_opt_out)
|
|
113
|
+
raise Insika::MediaError, "media egress blocked for #{url}: #{violation}" if violation
|
|
114
|
+
|
|
115
|
+
uri = URI.parse(url)
|
|
116
|
+
opts = { use_ssl: uri.scheme == "https", open_timeout: 30, read_timeout: 60 }
|
|
117
|
+
Net::HTTP.start(uri.host, uri.port, opts) do |http|
|
|
118
|
+
buf = +"".b
|
|
119
|
+
http.request(Net::HTTP::Get.new(uri)) do |resp|
|
|
120
|
+
raise Insika::MediaError, "media fetch HTTP #{resp.code}" unless resp.is_a?(Net::HTTPSuccess)
|
|
121
|
+
|
|
122
|
+
resp.read_body { |chunk| buf << chunk; break if buf.bytesize > max_bytes }
|
|
123
|
+
end
|
|
124
|
+
raise Insika::MediaError, "media exceeds #{max_bytes} bytes" if buf.bytesize > max_bytes
|
|
125
|
+
buf
|
|
126
|
+
end
|
|
127
|
+
rescue URI::InvalidURIError
|
|
128
|
+
raise Insika::MediaError, "invalid media URL"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# The opt-out the comment above promises, read from the SAME env the
|
|
132
|
+
# data-tool guard reads (INSIKA_EGRESS_ALLOW_HTTP / _ALLOW_PRIVATE): without
|
|
133
|
+
# this, a local run serving media over http:// ALWAYS failed, however the
|
|
134
|
+
# deployment was configured. INSIKA_EGRESS_HOSTS is deliberately NOT applied:
|
|
135
|
+
# that allowlist pins the handful of hosts a tool may call, while media URLs
|
|
136
|
+
# come from the channel's CDN — honouring it here would break every real
|
|
137
|
+
# deployment that narrows its tools.
|
|
138
|
+
def self.egress_opt_out
|
|
139
|
+
{ allow_http: Insika::EnvSchema.truthy?(ENV["INSIKA_EGRESS_ALLOW_HTTP"]),
|
|
140
|
+
allow_private: Insika::EnvSchema.truthy?(ENV["INSIKA_EGRESS_ALLOW_PRIVATE"]) }
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# WS9 (saída): generated media. The OUTPUT shape is an additive part —
|
|
144
|
+
# `{ "type": "image"|"audio", "mime_type": …, "base64": …, "model": … }`
|
|
145
|
+
# — that rides the turn's `output_parts` (terminal event + /v1/responses
|
|
146
|
+
# envelope), NEVER the answer text: the customer's channel consumes the
|
|
147
|
+
# bytes, the model's prose stays the answer.
|
|
148
|
+
#
|
|
149
|
+
# The GENERATION SEAMS are injectable like the STT seam: each is a
|
|
150
|
+
# `->(content, config) { [ part_hash, usage_hash ] }` (part_hash already
|
|
151
|
+
# carries its "type"), specs stub them, and the defaults hit the provider
|
|
152
|
+
# behind lazy requires:
|
|
153
|
+
# · image — RubyLLM.paint (the gem has vision AND painting), billed
|
|
154
|
+
# tokens merged into the turn's usage like any ask;
|
|
155
|
+
# · tts — RubyLLM still has NO speech API (as of 1.16.0), so the default
|
|
156
|
+
# is a thin POST to the OpenAI-compatible `<base>/audio/speech`
|
|
157
|
+
# endpoint (base + key from the provider config the chat uses — a
|
|
158
|
+
# deployment pointing OpenAI at a gateway keeps TTS pointing there).
|
|
159
|
+
# OpenAI's speech API reports no token usage; the part carries the
|
|
160
|
+
# model so the consumer can price it, and the turn counts the call.
|
|
161
|
+
module Output
|
|
162
|
+
DEFAULT_IMAGE_SIZE = "1024x1024"
|
|
163
|
+
DEFAULT_TTS_MODEL = "tts-1"
|
|
164
|
+
DEFAULT_TTS_VOICE = "alloy"
|
|
165
|
+
DEFAULT_TTS_FORMAT = "mp3"
|
|
166
|
+
# Base64 inlines into the envelope — a cap so a pathological generation
|
|
167
|
+
# cannot blow up the SSE frame. A generated 1024x1024 PNG sits well under.
|
|
168
|
+
MAX_EMBEDDED_BYTES = 8 * 1024 * 1024
|
|
169
|
+
|
|
170
|
+
class << self
|
|
171
|
+
# -> { image: seam, tts: seam } with the DEFAULTS bound to a context
|
|
172
|
+
# (the graph's RubyLLM::Context when it owns credentials — nil = the
|
|
173
|
+
# process-wide RubyLLM constant). Built lazily on first generation so
|
|
174
|
+
# the core loads without ruby_llm (load-guard).
|
|
175
|
+
def defaults(context:)
|
|
176
|
+
{
|
|
177
|
+
image: ->(prompt, config) { generate_image(prompt, config: config, context: context) },
|
|
178
|
+
tts: ->(text, config) { synthesize_speech(text, config: config, context: context) }
|
|
179
|
+
}
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# -> [Part, usage]: paint via RubyLLM. usage is the provider's token
|
|
183
|
+
# counts ({ input_tokens:, output_tokens: } — merged into the turn's
|
|
184
|
+
# usage by the Executor); a provider without counts reports nothing.
|
|
185
|
+
def generate_image(prompt, config:, context:)
|
|
186
|
+
require "ruby_llm" # lazy — the core loads without it (load-guard)
|
|
187
|
+
|
|
188
|
+
cfg = Insika::Coercion.deep_stringify(config || {})
|
|
189
|
+
model = Insika::Coercion.presence(cfg["model"]) || image_model(context)
|
|
190
|
+
api = context || RubyLLM
|
|
191
|
+
image = api.paint(prompt.to_s, model: model, assume_model_exists: true,
|
|
192
|
+
size: presence(cfg["size"]) || DEFAULT_IMAGE_SIZE)
|
|
193
|
+
data = image.respond_to?(:data) ? image.data : nil
|
|
194
|
+
raise Insika::MediaError, "image generation returned no embeddable data" if data.to_s.empty?
|
|
195
|
+
|
|
196
|
+
enforce_embedded_size!(data, "generated image")
|
|
197
|
+
mime = image.respond_to?(:mime_type) ? image.mime_type : nil
|
|
198
|
+
model_id = image.respond_to?(:model_id) ? image.model_id : nil
|
|
199
|
+
usage = image.respond_to?(:usage) ? token_usage(image.usage) : {}
|
|
200
|
+
part = { "type" => "image", "mime_type" => presence(mime) || "image/png",
|
|
201
|
+
"base64" => data, "model" => presence(model_id) }
|
|
202
|
+
[part.compact, usage]
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# -> [Part, {}]: synthesize speech via the OpenAI-compatible
|
|
206
|
+
# `<base>/audio/speech` endpoint. `context` supplies the base URL + key
|
|
207
|
+
# (the same config the chat uses — see `speech_endpoint`). The bytes
|
|
208
|
+
# embed base64 in the part; the usage is empty (no token counts on the
|
|
209
|
+
# speech API) and the part carries the model for consumer-side pricing.
|
|
210
|
+
def synthesize_speech(text, config:, context:)
|
|
211
|
+
require "net/http"
|
|
212
|
+
require "uri"
|
|
213
|
+
require "json"
|
|
214
|
+
require "base64"
|
|
215
|
+
|
|
216
|
+
cfg = Insika::Coercion.deep_stringify(config || {})
|
|
217
|
+
model = presence(cfg["model"]) || DEFAULT_TTS_MODEL
|
|
218
|
+
voice = presence(cfg["voice"]) || DEFAULT_TTS_VOICE
|
|
219
|
+
format = presence(cfg["format"]) || DEFAULT_TTS_FORMAT
|
|
220
|
+
base, key = speech_endpoint(context)
|
|
221
|
+
if key.to_s.empty?
|
|
222
|
+
raise Insika::MediaError,
|
|
223
|
+
"TTS needs an OpenAI API key (provider config) — set it on the " \
|
|
224
|
+
"provider the agent uses, or inject a tts seam"
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
uri = URI.parse("#{base}/audio/speech")
|
|
228
|
+
req = Net::HTTP::Post.new(uri)
|
|
229
|
+
req["Authorization"] = "Bearer #{key}"
|
|
230
|
+
req["Content-Type"] = "application/json"
|
|
231
|
+
req.body = JSON.generate(model: model, voice: voice, input: text.to_s,
|
|
232
|
+
response_format: format)
|
|
233
|
+
opts = { use_ssl: uri.scheme == "https", open_timeout: 30, read_timeout: 60 }
|
|
234
|
+
bytes = Net::HTTP.start(uri.host, uri.port, opts) do |http|
|
|
235
|
+
resp = http.request(req)
|
|
236
|
+
raise Insika::MediaError, "TTS HTTP #{resp.code}" unless resp.is_a?(Net::HTTPSuccess)
|
|
237
|
+
|
|
238
|
+
# stream into the cap — a rogue/broken endpoint must not grow the
|
|
239
|
+
# process past MAX_EMBEDDED_BYTES before the refusal.
|
|
240
|
+
buf = +"".b
|
|
241
|
+
resp.read_body do |chunk|
|
|
242
|
+
buf << chunk
|
|
243
|
+
break if buf.bytesize > MAX_EMBEDDED_BYTES
|
|
244
|
+
end
|
|
245
|
+
buf
|
|
246
|
+
end
|
|
247
|
+
enforce_embedded_size!(bytes, "synthesized speech")
|
|
248
|
+
part = { "type" => "audio", "mime_type" => mime_for(format), "base64" => Base64.strict_encode64(bytes), "model" => model }
|
|
249
|
+
[part.compact, {}]
|
|
250
|
+
rescue URI::InvalidURIError
|
|
251
|
+
raise Insika::MediaError, "invalid TTS endpoint"
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
private
|
|
255
|
+
|
|
256
|
+
# The OpenAI-compatible base URL + key behind the chat's provider
|
|
257
|
+
# config. A RubyLLM::Context owns the deployment's credentials; the
|
|
258
|
+
# global config is the fallback (a graph without its own context).
|
|
259
|
+
def speech_endpoint(context)
|
|
260
|
+
config = context.respond_to?(:config) ? context.config : RubyLLM.config
|
|
261
|
+
base = config.respond_to?(:openai_api_base) ? config.openai_api_base : nil
|
|
262
|
+
key = config.respond_to?(:openai_api_key) ? config.openai_api_key : nil
|
|
263
|
+
[presence(base) || "https://api.openai.com/v1", key]
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def image_model(context)
|
|
267
|
+
config = context.respond_to?(:config) ? context.config : RubyLLM.config
|
|
268
|
+
config.respond_to?(:default_image_model) ? config.default_image_model : nil
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def token_usage(raw)
|
|
272
|
+
usage = raw.is_a?(Hash) ? raw : {}
|
|
273
|
+
{
|
|
274
|
+
input_tokens: usage[:input_tokens] || usage["input_tokens"] || usage["prompt_tokens"],
|
|
275
|
+
output_tokens: usage[:output_tokens] || usage["output_tokens"] || usage["completion_tokens"]
|
|
276
|
+
}.compact
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def mime_for(format)
|
|
280
|
+
{ "mp3" => "audio/mpeg", "opus" => "audio/opus", "aac" => "audio/aac",
|
|
281
|
+
"wav" => "audio/wav", "flac" => "audio/flac" }[format.to_s] || "audio/mpeg"
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def enforce_embedded_size!(data, label)
|
|
285
|
+
size = data.respond_to?(:bytesize) ? data.bytesize : data.to_s.bytesize
|
|
286
|
+
return if size <= MAX_EMBEDDED_BYTES
|
|
287
|
+
|
|
288
|
+
raise Insika::MediaError,
|
|
289
|
+
"#{label} too large to embed (#{size} bytes > #{MAX_EMBEDDED_BYTES})"
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def presence(value)
|
|
293
|
+
Insika::Coercion.presence(value)
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest/sha2"
|
|
4
|
+
require "json"
|
|
5
|
+
require "time"
|
|
6
|
+
|
|
7
|
+
module Insika
|
|
8
|
+
# append-only, per-cell audit of MEMORY mutations. Every
|
|
9
|
+
# operator mutation of a cell appends a line (who, when, what, old->new
|
|
10
|
+
# digests). The entry holds DIGESTS of values, never the values — the forget
|
|
11
|
+
# line records that a deletion happened without the deleted content (the
|
|
12
|
+
# digest is not invertible, and keys are fact names — provenance, not
|
|
13
|
+
# payload). Capped per cell (no retention hook, no delete path: the audit
|
|
14
|
+
# outlives what it describes). `record` rescues EVERYTHING — a failed audit
|
|
15
|
+
# write never fails the mutation it describes.
|
|
16
|
+
#
|
|
17
|
+
# The capped-list RMW caveat (context_trace_store.rb's discipline): one cell
|
|
18
|
+
# key, written on the command's fiber; a cross-process race loses the loser's
|
|
19
|
+
# append — a trace-level loss, not a correctness one.
|
|
20
|
+
class MemoryAuditStore
|
|
21
|
+
SCOPE = "memory_audit" # store key = the memory cell scope
|
|
22
|
+
MAX_PER_CELL = 200 # oldest dropped; the cap bounds growth
|
|
23
|
+
|
|
24
|
+
Entry = Data.define(:at, :action, :actor, :key, :tenant, :customer,
|
|
25
|
+
:old_hash, :new_hash, :note)
|
|
26
|
+
|
|
27
|
+
def initialize(store:, clock: nil)
|
|
28
|
+
@store = store
|
|
29
|
+
@clock = clock # -> Time, injectable for specs
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# action: "put" | "forget" | "purge". key = the fact name (provenance,
|
|
33
|
+
# not payload). Appends + caps. Rescues EVERYTHING -> Entry | nil (the
|
|
34
|
+
# audit never breaks a command).
|
|
35
|
+
def record(cell:, action:, actor:, key: nil, tenant: nil, customer: nil,
|
|
36
|
+
old_hash: nil, new_hash: nil, note: nil)
|
|
37
|
+
entry = {
|
|
38
|
+
"at" => timestamp,
|
|
39
|
+
"action" => action.to_s,
|
|
40
|
+
"actor" => actor.to_s,
|
|
41
|
+
"key" => Coercion.presence(key),
|
|
42
|
+
"tenant" => Coercion.presence(tenant),
|
|
43
|
+
"customer" => Coercion.presence(customer),
|
|
44
|
+
"old_hash" => Coercion.presence(old_hash),
|
|
45
|
+
"new_hash" => Coercion.presence(new_hash),
|
|
46
|
+
"note" => Coercion.presence(note)
|
|
47
|
+
}
|
|
48
|
+
list = (@store.get(SCOPE, cell.to_s) || []) + [entry]
|
|
49
|
+
@store.set(SCOPE, cell.to_s, list.last(MAX_PER_CELL))
|
|
50
|
+
to_entry(entry)
|
|
51
|
+
rescue StandardError
|
|
52
|
+
nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# -> [Entry] most recent first. [] if none. A broken backend degrades to
|
|
56
|
+
# [] — the audit is read to RENDER, never to gate.
|
|
57
|
+
def for_cell(cell, limit: 100)
|
|
58
|
+
Array(@store.get(SCOPE, cell.to_s)).reverse.first(limit).map { |e| to_entry(e) }
|
|
59
|
+
rescue StandardError
|
|
60
|
+
[]
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The digest the callers share: SHA-256 hexdigest of JSON.generate(value)
|
|
64
|
+
# (or value.to_s for non-JSON scalars). -> String
|
|
65
|
+
def self.digest(value)
|
|
66
|
+
payload = case value
|
|
67
|
+
when Hash, Array then JSON.generate(value)
|
|
68
|
+
else value.to_s
|
|
69
|
+
end
|
|
70
|
+
Digest::SHA256.hexdigest(payload)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
def to_entry(record)
|
|
76
|
+
Entry.new(at: record["at"], action: record["action"], actor: record["actor"],
|
|
77
|
+
key: record["key"], tenant: record["tenant"], customer: record["customer"],
|
|
78
|
+
old_hash: record["old_hash"], new_hash: record["new_hash"], note: record["note"])
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def timestamp = (clock ? clock.call : Time.now.utc).utc.iso8601(6)
|
|
82
|
+
|
|
83
|
+
def clock = @clock
|
|
84
|
+
end
|
|
85
|
+
end
|