discourse/plugins/discourse-ai/lib/agents/artifact_update_strategies/base.rb
Sam e3fae646d4
DEV: AI persona to agent migration (#38319)
Co-authored-by: Keegan George <kgeorge13@gmail.com>
2026-03-10 15:59:45 +11:00

82 lines
2.1 KiB
Ruby
Vendored

# frozen_string_literal: true
module DiscourseAi
module Agents
module ArtifactUpdateStrategies
class InvalidFormatError < StandardError
end
class Base
attr_reader :post,
:user,
:artifact,
:artifact_version,
:instructions,
:llm,
:cancel_manager,
:execution_context
def initialize(
llm:,
post:,
user:,
artifact:,
artifact_version:,
instructions:,
cancel_manager: nil,
execution_context: nil
)
@llm = llm
@post = post
@user = user
@artifact = artifact
@artifact_version = artifact_version
@instructions = instructions
@cancel_manager = cancel_manager
@execution_context = execution_context
end
def apply(&progress)
changes = generate_changes(&progress)
parsed_changes = parse_changes(changes)
apply_changes(parsed_changes)
end
def storage_api
if @artifact.metadata.is_a?(Hash) && @artifact.metadata["requires_storage"]
DiscourseAi::Agents::Tools::CreateArtifact.storage_api
end
end
private
def generate_changes(&progress)
response = +""
llm.generate(
build_prompt,
user: user,
cancel_manager: cancel_manager,
execution_context:,
) do |partial|
progress.call(partial) if progress
response << partial
end
response
end
def build_prompt
# To be implemented by subclasses
raise NotImplementedError
end
def parse_changes(response)
# To be implemented by subclasses
raise NotImplementedError
end
def apply_changes(changes)
# To be implemented by subclasses
raise NotImplementedError
end
end
end
end
end