69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/brainiac/plugins/zoho/triage.rb', line 69
def dispatch(email, rule)
agent_name = rule["dispatch_agent"]
email_message = "#{email["subject"]} #{email["fromAddress"]}"
if intent_skip?(email_message, agent_name: agent_name, source: :zoho, channel: "Zoho email triage")
LOG.info "[Zoho:Triage] Intent skip — not dispatching #{agent_name} for: #{email["subject"]}" if defined?(LOG)
return nil
end
timestamp = Time.now.strftime("%Y%m%d-%H%M%S")
triage_dir = File.join(ENV.fetch("BRAINIAC_DIR", File.join(Dir.home, ".brainiac")), "tmp", "zoho", "triage")
FileUtils.mkdir_p(triage_dir)
response_file = File.join(triage_dir, "triage-#{timestamp}.json")
log_file = File.join(triage_dir, "triage-#{timestamp}.log")
prompt_file = write_prompt(email, response_file, timestamp, triage_dir)
agent_key = agent_name.downcase.gsub(/[^a-z0-9-]/, "-")
project_config = default_project_config
resolved = resolve_project_cli_config(project_config || {})
cmd = [resolved["agent_cli"]]
cmd.push("--agent", agent_key)
cmd.concat(resolved["agent_cli_args"].split)
spawn_env = {}
agent_env = agent_env_for(agent_name)
spawn_env.merge!(agent_env) unless agent_env.empty?
work_dir = project_config ? project_config["repo_path"] : Dir.pwd
LOG.info "[Zoho:Triage] Dispatching #{agent_name} for: #{email["subject"]}"
LOG.info "[Zoho:Triage] Command: #{cmd.join(" ")}"
pid = spawn(spawn_env, *cmd,
chdir: work_dir, in: prompt_file,
out: [log_file, "w"], err: %i[child out])
monitor(pid, response_file, log_file, prompt_file, email, rule)
pid
end
|