legate 0.1.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +345 -0
- data/bin/legate +13 -0
- data/examples/00_quickstart.rb +51 -0
- data/examples/01_simple_agent.rb +105 -0
- data/examples/02_multi_tool_agent.rb +140 -0
- data/examples/03_custom_tool.rb +93 -0
- data/examples/04_agent_instructions.rb +84 -0
- data/examples/05_state_and_sessions.rb +91 -0
- data/examples/06_callbacks.rb +186 -0
- data/examples/07_async_jobs.rb +112 -0
- data/examples/08_loop_agent.rb +197 -0
- data/examples/09_sequential_workflow.rb +40 -0
- data/examples/10_parallel_workflow.rb +34 -0
- data/examples/11_agent_delegation.rb +24 -0
- data/examples/12_http_client_tool.rb +156 -0
- data/examples/13_authentication.rb +220 -0
- data/examples/14_mcp_client.rb +154 -0
- data/examples/15_mcp_server.rb +79 -0
- data/examples/16_webhooks.rb +91 -0
- data/examples/README_sequential_agents.md +164 -0
- data/examples/advanced/auth/cookie_auth_tool.rb +146 -0
- data/examples/advanced/auth/custom_auth_flows_example.rb +626 -0
- data/examples/advanced/auth/excon_middleware.rb +317 -0
- data/examples/advanced/auth/excon_middleware_auth.rb +399 -0
- data/examples/advanced/auth/fiber_auth_example.rb +281 -0
- data/examples/advanced/auth/fiber_oidc_example.rb +403 -0
- data/examples/advanced/auth/httpbin_bearer_tool.rb +159 -0
- data/examples/advanced/auth/oauth2_auth.rb +419 -0
- data/examples/advanced/auth/oidc_auth.rb +514 -0
- data/examples/advanced/auth/openweather_api.rb +251 -0
- data/examples/advanced/auth/openweather_tool.rb +153 -0
- data/examples/advanced/auth/query_param_middleware_test.rb +138 -0
- data/examples/advanced/auth/service_account.rb +135 -0
- data/examples/advanced/auth/test_with_httpbin.rb +202 -0
- data/examples/advanced/auth/token_lifecycle_example.rb +428 -0
- data/examples/advanced/callback_monitoring.rb +679 -0
- data/examples/advanced/mas/fixed_delegation_example.rb +191 -0
- data/examples/advanced/mas/loop_workflow.rb +28 -0
- data/examples/advanced/mas/mock_planner.rb +77 -0
- data/examples/advanced/mas/proper_delegation_example.rb +276 -0
- data/examples/advanced/mcp/legate_mcp_server_resource_example.rb +182 -0
- data/examples/advanced/mcp/mcp_resource_server_example.rb +309 -0
- data/examples/advanced/mcp/mcp_server_async.rb +76 -0
- data/examples/advanced/mcp/mcp_server_async_tools.rb +122 -0
- data/examples/advanced/mcp/mcp_server_legate_agent.rb +95 -0
- data/examples/advanced/mcp/mcp_server_rack.rb +89 -0
- data/examples/advanced/random_calculator.rb +104 -0
- data/examples/advanced/sleep_agent.rb +153 -0
- data/examples/advanced/webhooks/webhook_e2e_runner.rb +110 -0
- data/examples/advanced/webhooks/webhook_receiver_agent.rb +58 -0
- data/examples/advanced/workflows/task_refinement_loop_agent.rb +278 -0
- data/examples/advanced/workflows/travel_planner_auto_sequential.rb +444 -0
- data/examples/advanced/workflows/travel_planner_parallel.rb +656 -0
- data/examples/advanced/workflows/travel_planner_sequential.rb +512 -0
- data/examples/tools/oauth2_example.rb +136 -0
- data/examples/tools/sleepy_tool.rb +42 -0
- data/lib/legate/activity_log.rb +71 -0
- data/lib/legate/agent.rb +959 -0
- data/lib/legate/agent_code_generator.rb +185 -0
- data/lib/legate/agent_definition.rb +812 -0
- data/lib/legate/agentic/decision.rb +49 -0
- data/lib/legate/agentic/loop.rb +134 -0
- data/lib/legate/agentic.rb +5 -0
- data/lib/legate/agents/loop_agent.rb +248 -0
- data/lib/legate/agents/parallel_agent.rb +163 -0
- data/lib/legate/agents/sequential_agent.rb +190 -0
- data/lib/legate/agents.rb +14 -0
- data/lib/legate/auth/config.rb +148 -0
- data/lib/legate/auth/coordinator.rb +218 -0
- data/lib/legate/auth/coordinators/oauth2_coordinator.rb +99 -0
- data/lib/legate/auth/coordinators/oidc_coordinator.rb +68 -0
- data/lib/legate/auth/coordinators/service_account_coordinator.rb +122 -0
- data/lib/legate/auth/credential.rb +157 -0
- data/lib/legate/auth/encryption.rb +108 -0
- data/lib/legate/auth/error.rb +94 -0
- data/lib/legate/auth/exchanged_credential.rb +180 -0
- data/lib/legate/auth/excon_middleware.rb +285 -0
- data/lib/legate/auth/http_client_utils.rb +364 -0
- data/lib/legate/auth/manager.rb +531 -0
- data/lib/legate/auth/manager_store.rb +394 -0
- data/lib/legate/auth/middleware_factory.rb +290 -0
- data/lib/legate/auth/runner.rb +279 -0
- data/lib/legate/auth/scheme.rb +125 -0
- data/lib/legate/auth/schemes/api_key.rb +212 -0
- data/lib/legate/auth/schemes/google_service_account.rb +108 -0
- data/lib/legate/auth/schemes/http_bearer.rb +98 -0
- data/lib/legate/auth/schemes/oauth2.rb +396 -0
- data/lib/legate/auth/schemes/openid_connect.rb +346 -0
- data/lib/legate/auth/schemes/service_account.rb +388 -0
- data/lib/legate/auth/schemes.rb +40 -0
- data/lib/legate/auth/token_manager.rb +362 -0
- data/lib/legate/auth/token_store.rb +86 -0
- data/lib/legate/auth/tool_context_extension.rb +97 -0
- data/lib/legate/auth/tool_integration.rb +188 -0
- data/lib/legate/auth/url_guard.rb +81 -0
- data/lib/legate/auth.rb +453 -0
- data/lib/legate/callbacks/callback_context.rb +71 -0
- data/lib/legate/cli/agent_commands.rb +950 -0
- data/lib/legate/cli/auth_commands.rb +520 -0
- data/lib/legate/cli/base_command.rb +24 -0
- data/lib/legate/cli/deployment_commands.rb +934 -0
- data/lib/legate/cli/output_helper.rb +108 -0
- data/lib/legate/cli/session_commands.rb +138 -0
- data/lib/legate/cli/skaffold_commands.rb +223 -0
- data/lib/legate/cli/tool_commands.rb +261 -0
- data/lib/legate/cli/web_commands.rb +182 -0
- data/lib/legate/cli.rb +40 -0
- data/lib/legate/configuration/webhooks.rb +113 -0
- data/lib/legate/configuration.rb +39 -0
- data/lib/legate/definition_store.rb +23 -0
- data/lib/legate/errors.rb +118 -0
- data/lib/legate/event.rb +161 -0
- data/lib/legate/gemini_ai_beta_patch.rb +39 -0
- data/lib/legate/generators/agent_generator.rb +412 -0
- data/lib/legate/generators/code_validator.rb +48 -0
- data/lib/legate/generators/legate/install_generator.rb +35 -0
- data/lib/legate/generators/legate/templates/create_legate_tables.rb.tt +36 -0
- data/lib/legate/generators/legate/templates/initializer.rb +18 -0
- data/lib/legate/generators/runtime_tool_loader.rb +76 -0
- data/lib/legate/generators/tool_generator.rb +408 -0
- data/lib/legate/generators.rb +11 -0
- data/lib/legate/global_definition_registry.rb +506 -0
- data/lib/legate/global_tool_manager.rb +135 -0
- data/lib/legate/llm/adapter.rb +69 -0
- data/lib/legate/llm/gemini.rb +172 -0
- data/lib/legate/llm/ollama.rb +80 -0
- data/lib/legate/llm.rb +34 -0
- data/lib/legate/mcp/client.rb +320 -0
- data/lib/legate/mcp/connection/sse.rb +292 -0
- data/lib/legate/mcp/connection/stdio.rb +273 -0
- data/lib/legate/mcp/connection_manager.rb +103 -0
- data/lib/legate/mcp/server/legate_agent_adapter.rb +170 -0
- data/lib/legate/mcp/server/legate_direct_agent_adapter.rb +140 -0
- data/lib/legate/mcp/server/legate_tool_adapter.rb +119 -0
- data/lib/legate/mcp/tool_wrapper.rb +138 -0
- data/lib/legate/mcp/util/schema_converter.rb +134 -0
- data/lib/legate/mcp.rb +23 -0
- data/lib/legate/plan_executor.rb +375 -0
- data/lib/legate/planner.rb +839 -0
- data/lib/legate/rails/railtie.rb +43 -0
- data/lib/legate/rails.rb +9 -0
- data/lib/legate/redaction.rb +32 -0
- data/lib/legate/session.rb +299 -0
- data/lib/legate/session_service/active_record.rb +300 -0
- data/lib/legate/session_service/base.rb +68 -0
- data/lib/legate/session_service/event_broadcast.rb +74 -0
- data/lib/legate/session_service/in_memory.rb +188 -0
- data/lib/legate/tool/metadata_dsl.rb +122 -0
- data/lib/legate/tool.rb +276 -0
- data/lib/legate/tool_code_generator.rb +103 -0
- data/lib/legate/tool_context.rb +350 -0
- data/lib/legate/tool_loader.rb +39 -0
- data/lib/legate/tool_registry.rb +73 -0
- data/lib/legate/tool_result.rb +61 -0
- data/lib/legate/tools/agent_tool.rb +187 -0
- data/lib/legate/tools/base/http_client.rb +319 -0
- data/lib/legate/tools/base/safe_url.rb +56 -0
- data/lib/legate/tools/base_async_job_tool.rb +91 -0
- data/lib/legate/tools/calculator.rb +89 -0
- data/lib/legate/tools/cat_facts.rb +81 -0
- data/lib/legate/tools/check_job_status_tool.rb +48 -0
- data/lib/legate/tools/current_time_tool.rb +64 -0
- data/lib/legate/tools/echo.rb +43 -0
- data/lib/legate/tools/http_request_tool.rb +105 -0
- data/lib/legate/tools/random_number_tool.rb +64 -0
- data/lib/legate/tools/read_webpage_tool.rb +92 -0
- data/lib/legate/tools/sleepy_tool.rb +74 -0
- data/lib/legate/tools/webhook_tool.rb +146 -0
- data/lib/legate/version.rb +5 -0
- data/lib/legate/web/app.rb +984 -0
- data/lib/legate/web/public/css/main.css +4980 -0
- data/lib/legate/web/public/images/favicon-256.png +0 -0
- data/lib/legate/web/public/images/favicon-32.png +0 -0
- data/lib/legate/web/public/images/legate-logo-dark.png +0 -0
- data/lib/legate/web/public/images/legate-logo-light.png +0 -0
- data/lib/legate/web/public/js/legate.js +616 -0
- data/lib/legate/web/public/styles/main.scss +4402 -0
- data/lib/legate/web/routes/agent_authentication_routes.rb +530 -0
- data/lib/legate/web/routes/agent_definition_routes.rb +803 -0
- data/lib/legate/web/routes/agent_generator_routes.rb +80 -0
- data/lib/legate/web/routes/agent_interaction_routes.rb +734 -0
- data/lib/legate/web/routes/agent_runtime_routes.rb +323 -0
- data/lib/legate/web/routes/api_routes.rb +56 -0
- data/lib/legate/web/routes/authentication_routes.rb +1541 -0
- data/lib/legate/web/routes/core_routes.rb +111 -0
- data/lib/legate/web/routes/documentation_routes.rb +220 -0
- data/lib/legate/web/routes/tool_generator_routes.rb +81 -0
- data/lib/legate/web/routes/tools_ui_routes.rb +207 -0
- data/lib/legate/web/sass_compiler.rb +73 -0
- data/lib/legate/web/views/_active_session_info.slim +25 -0
- data/lib/legate/web/views/_activity_list.slim +55 -0
- data/lib/legate/web/views/_agent_card.slim +56 -0
- data/lib/legate/web/views/_agent_generator_modal.slim +382 -0
- data/lib/legate/web/views/_agent_status_controls.slim +71 -0
- data/lib/legate/web/views/_agent_tool_table.slim +74 -0
- data/lib/legate/web/views/_chat_message.slim +95 -0
- data/lib/legate/web/views/_display_agent_configuration.slim +26 -0
- data/lib/legate/web/views/_display_agent_description.slim +11 -0
- data/lib/legate/web/views/_display_agent_fallback.slim +15 -0
- data/lib/legate/web/views/_display_agent_hierarchy.slim +93 -0
- data/lib/legate/web/views/_display_agent_instruction.slim +17 -0
- data/lib/legate/web/views/_display_agent_mcp.slim +13 -0
- data/lib/legate/web/views/_display_agent_model.slim +17 -0
- data/lib/legate/web/views/_display_agent_name.slim +42 -0
- data/lib/legate/web/views/_display_agent_output_key.slim +26 -0
- data/lib/legate/web/views/_display_agent_type.slim +65 -0
- data/lib/legate/web/views/_edit_agent_configuration.slim +74 -0
- data/lib/legate/web/views/_edit_agent_description.slim +16 -0
- data/lib/legate/web/views/_edit_agent_fallback.slim +25 -0
- data/lib/legate/web/views/_edit_agent_hierarchy.slim +98 -0
- data/lib/legate/web/views/_edit_agent_instruction.slim +49 -0
- data/lib/legate/web/views/_edit_agent_mcp.slim +33 -0
- data/lib/legate/web/views/_edit_agent_model.slim +23 -0
- data/lib/legate/web/views/_edit_agent_output_key.slim +36 -0
- data/lib/legate/web/views/_edit_agent_tools.slim +40 -0
- data/lib/legate/web/views/_edit_agent_type.slim +67 -0
- data/lib/legate/web/views/_session_error.slim +4 -0
- data/lib/legate/web/views/_skeleton.slim +69 -0
- data/lib/legate/web/views/_tool_card.slim +9 -0
- data/lib/legate/web/views/_tool_generator_modal.slim +311 -0
- data/lib/legate/web/views/agent.slim +436 -0
- data/lib/legate/web/views/agent_auth.slim +562 -0
- data/lib/legate/web/views/agents.slim +369 -0
- data/lib/legate/web/views/auth.slim +112 -0
- data/lib/legate/web/views/auth_credential_detail.slim +327 -0
- data/lib/legate/web/views/auth_credentials.slim +261 -0
- data/lib/legate/web/views/auth_debug.slim +94 -0
- data/lib/legate/web/views/auth_mapping_detail.slim +151 -0
- data/lib/legate/web/views/auth_mapping_new.slim +123 -0
- data/lib/legate/web/views/auth_mappings.slim +120 -0
- data/lib/legate/web/views/auth_scheme_detail.slim +274 -0
- data/lib/legate/web/views/auth_schemes.slim +259 -0
- data/lib/legate/web/views/auth_test.slim +418 -0
- data/lib/legate/web/views/chat.slim +192 -0
- data/lib/legate/web/views/docs_index.slim +105 -0
- data/lib/legate/web/views/docs_show.slim +105 -0
- data/lib/legate/web/views/error_404.slim +5 -0
- data/lib/legate/web/views/index.slim +148 -0
- data/lib/legate/web/views/layout.slim +144 -0
- data/lib/legate/web/views/tool_detail.slim +87 -0
- data/lib/legate/web/views/tools.slim +50 -0
- data/lib/legate/web/webhook_listener.rb +367 -0
- data/lib/legate/web.rb +9 -0
- data/lib/legate.rb +220 -0
- data/public/docs/advanced/callbacks.md +828 -0
- data/public/docs/advanced/mcp_schema_conversion.md +59 -0
- data/public/docs/authentication/api_reference/config.md +210 -0
- data/public/docs/authentication/api_reference/credential.md +246 -0
- data/public/docs/authentication/api_reference/encryption.md +218 -0
- data/public/docs/authentication/api_reference/exchanged_credential.md +271 -0
- data/public/docs/authentication/api_reference/excon_middleware.md +175 -0
- data/public/docs/authentication/api_reference/index.md +30 -0
- data/public/docs/authentication/api_reference/scheme.md +250 -0
- data/public/docs/authentication/api_reference/schemes/api_key.md +175 -0
- data/public/docs/authentication/api_reference/schemes/google_service_account.md +221 -0
- data/public/docs/authentication/api_reference/schemes/http_bearer.md +169 -0
- data/public/docs/authentication/api_reference/schemes/oauth2.md +343 -0
- data/public/docs/authentication/api_reference/schemes/oidc.md +73 -0
- data/public/docs/authentication/api_reference/schemes/openid_connect.md +311 -0
- data/public/docs/authentication/api_reference/schemes/service_account.md +287 -0
- data/public/docs/authentication/api_reference/token_manager.md +221 -0
- data/public/docs/authentication/api_reference/token_store.md +146 -0
- data/public/docs/authentication/api_reference/tool_context_extension.md +166 -0
- data/public/docs/authentication/guides/api_key.md +190 -0
- data/public/docs/authentication/guides/bearer.md +172 -0
- data/public/docs/authentication/guides/configuration.md +255 -0
- data/public/docs/authentication/guides/custom_flow.md +523 -0
- data/public/docs/authentication/guides/index.md +24 -0
- data/public/docs/authentication/guides/migration.md +435 -0
- data/public/docs/authentication/guides/oauth2.md +252 -0
- data/public/docs/authentication/guides/oidc.md +241 -0
- data/public/docs/authentication/guides/overview.md +155 -0
- data/public/docs/authentication/guides/secure_storage.md +301 -0
- data/public/docs/authentication/guides/service_account.md +228 -0
- data/public/docs/authentication/guides/token_lifecycle.md +295 -0
- data/public/docs/authentication/guides/web_ui_integration.md +504 -0
- data/public/docs/authentication/index.md +58 -0
- data/public/docs/authentication/troubleshooting/credential_storage.md +550 -0
- data/public/docs/authentication/troubleshooting/environment_variables.md +540 -0
- data/public/docs/authentication/troubleshooting/index.md +11 -0
- data/public/docs/authentication/troubleshooting/oauth2_issues.md +220 -0
- data/public/docs/authentication/troubleshooting/oidc_issues.md +412 -0
- data/public/docs/authentication/troubleshooting/token_refresh.md +338 -0
- data/public/docs/cli/legate_cli_usage.md +363 -0
- data/public/docs/core_concepts/legate_agent_lifecycle.md +124 -0
- data/public/docs/core_concepts/legate_architecture_overview.md +110 -0
- data/public/docs/core_concepts/legate_configuration.md +116 -0
- data/public/docs/core_concepts/legate_definition_store.md +102 -0
- data/public/docs/core_concepts/legate_planner.md +94 -0
- data/public/docs/core_concepts/legate_session_service.md +104 -0
- data/public/docs/error_handling/legate_error_handling.md +122 -0
- data/public/docs/examples.md +199 -0
- data/public/docs/getting_started.md +111 -0
- data/public/docs/guides/agentic_agents.md +137 -0
- data/public/docs/guides/ai_code_generators.md +437 -0
- data/public/docs/guides/auto_loading.md +326 -0
- data/public/docs/guides/configuring_agent_webhooks.md +219 -0
- data/public/docs/guides/http_client_usage.md +264 -0
- data/public/docs/guides/llm_providers.md +137 -0
- data/public/docs/guides/mcp_client_integration.md +232 -0
- data/public/docs/guides/mcp_server_exposure.md +206 -0
- data/public/docs/guides/rails_integration.md +128 -0
- data/public/docs/guides/sending_outbound_webhooks.md +227 -0
- data/public/docs/guides/streaming.md +112 -0
- data/public/docs/guides/webhooks.md +288 -0
- data/public/docs/introduction.md +51 -0
- data/public/docs/multi_agent_systems/advanced_features.md +57 -0
- data/public/docs/multi_agent_systems/agent_delegation.md +190 -0
- data/public/docs/multi_agent_systems/agent_hierarchy.md +49 -0
- data/public/docs/multi_agent_systems/state_management.md +47 -0
- data/public/docs/multi_agent_systems/workflow_agents.md +72 -0
- data/public/docs/tools/legate_built_in_tools.md +332 -0
- data/public/docs/tools/legate_tools_and_registry.md +263 -0
- data/public/docs/web_ui/legate_web_ui.md +137 -0
- metadata +823 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# This example demonstrates a practical use of LoopAgent for iterative refinement of text
|
|
4
|
+
# Using three agents in a loop - one to critique, another to improve based on the critique,
|
|
5
|
+
# and a third to assess when the refinement is complete
|
|
6
|
+
|
|
7
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __dir__))
|
|
8
|
+
require 'legate'
|
|
9
|
+
require 'legate/agents/loop_agent'
|
|
10
|
+
|
|
11
|
+
# Create a session service for state management
|
|
12
|
+
session_service = Legate::SessionService::InMemory.new
|
|
13
|
+
user_id = 'demo-user'
|
|
14
|
+
app_name = 'loop-example'
|
|
15
|
+
|
|
16
|
+
# Create a session with initial state
|
|
17
|
+
initial_text = 'The cat sat on the mat.'
|
|
18
|
+
session = session_service.create_session(
|
|
19
|
+
app_name: app_name,
|
|
20
|
+
user_id: user_id,
|
|
21
|
+
initial_state: {
|
|
22
|
+
current_text: initial_text,
|
|
23
|
+
refinement_done: false,
|
|
24
|
+
refinement_score: 0
|
|
25
|
+
}
|
|
26
|
+
)
|
|
27
|
+
session_id = session.id
|
|
28
|
+
|
|
29
|
+
puts "Created session with ID: #{session_id}"
|
|
30
|
+
puts "Initial text: #{initial_text}"
|
|
31
|
+
puts 'Refinement process will loop until score reaches 8 or max 5 iterations'
|
|
32
|
+
puts '------------------------------'
|
|
33
|
+
|
|
34
|
+
# 1. Critique Agent - analyzes text and provides feedback
|
|
35
|
+
critic_agent = Legate::AgentDefinition.new.define do |a|
|
|
36
|
+
a.name :critic_agent
|
|
37
|
+
a.description 'Analyzes text and provides critical feedback for improvement'
|
|
38
|
+
a.instruction 'You are a literary critic. Analyze the provided text and give constructive criticism.'
|
|
39
|
+
a.use_tool :echo
|
|
40
|
+
a.output_key :critique_result
|
|
41
|
+
end
|
|
42
|
+
Legate::GlobalDefinitionRegistry.register(critic_agent)
|
|
43
|
+
|
|
44
|
+
# 2. Improvement Agent - takes text and critique and improves the text
|
|
45
|
+
improver_agent = Legate::AgentDefinition.new.define do |a|
|
|
46
|
+
a.name :improver_agent
|
|
47
|
+
a.description 'Improves text based on critical feedback'
|
|
48
|
+
a.instruction 'You are a skilled writer. Take the existing text and the critique, then produce an improved version.'
|
|
49
|
+
a.use_tool :echo
|
|
50
|
+
a.output_key :improved_text
|
|
51
|
+
end
|
|
52
|
+
Legate::GlobalDefinitionRegistry.register(improver_agent)
|
|
53
|
+
|
|
54
|
+
# 3. Assessment Agent - checks if the refinement process is complete
|
|
55
|
+
assessment_agent = Legate::AgentDefinition.new.define do |a|
|
|
56
|
+
a.name :assessment_agent
|
|
57
|
+
a.description 'Determines if refinement process is complete'
|
|
58
|
+
a.instruction 'You assess whether the text refinement is complete based on the critique score.'
|
|
59
|
+
a.use_tool :echo
|
|
60
|
+
a.output_key :assessment_result
|
|
61
|
+
end
|
|
62
|
+
Legate::GlobalDefinitionRegistry.register(assessment_agent)
|
|
63
|
+
|
|
64
|
+
# Finally, define the loop agent to coordinate the refinement process
|
|
65
|
+
refinement_loop = Legate::AgentDefinition.new.define do |a|
|
|
66
|
+
a.name :text_refinement_agent
|
|
67
|
+
a.description 'Coordinates the text refinement process through multiple iterations'
|
|
68
|
+
a.instruction 'You coordinate a process of iterative text refinement.'
|
|
69
|
+
a.agent_type :loop
|
|
70
|
+
|
|
71
|
+
# Define the sub-agents to run in each iteration
|
|
72
|
+
a.loop_sub_agents %i[critic_agent improver_agent assessment_agent]
|
|
73
|
+
|
|
74
|
+
# Set loop termination conditions
|
|
75
|
+
a.loop_max_iterations 5
|
|
76
|
+
a.loop_condition(:refinement_done, true)
|
|
77
|
+
|
|
78
|
+
# Store the final result
|
|
79
|
+
a.output_key :refinement_result
|
|
80
|
+
end
|
|
81
|
+
Legate::GlobalDefinitionRegistry.register(refinement_loop)
|
|
82
|
+
|
|
83
|
+
# Create the agent instances and customize their behaviors
|
|
84
|
+
|
|
85
|
+
# 1. Critic Agent Implementation
|
|
86
|
+
critic_instance = Legate::Agent.new(definition: critic_agent)
|
|
87
|
+
def critic_instance.execute_plan(_plan, session, session_service, _invocation_id = nil)
|
|
88
|
+
# Get the session ID from the session object
|
|
89
|
+
session_id = session.id
|
|
90
|
+
|
|
91
|
+
# Get the current text
|
|
92
|
+
current_text = session_service.get_state(session_id: session_id, key: :current_text)
|
|
93
|
+
|
|
94
|
+
# For this demo, we'll simulate different scores at each iteration
|
|
95
|
+
# In a production system, this would be determined by actual LLM analysis
|
|
96
|
+
iteration = session_service.get_state(session_id: session_id, key: :count) || 0
|
|
97
|
+
|
|
98
|
+
score = case iteration
|
|
99
|
+
when 0 then 3
|
|
100
|
+
when 1 then 5
|
|
101
|
+
when 2 then 7
|
|
102
|
+
when 3 then 9
|
|
103
|
+
else 10
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
critique = if score < 5
|
|
107
|
+
"Basic text with simple structure. Score: #{score}/10. Needs more descriptive language and complexity."
|
|
108
|
+
elsif score < 8
|
|
109
|
+
"Good progress, but could use more creativity. Score: #{score}/10. Consider adding more vivid imagery."
|
|
110
|
+
else
|
|
111
|
+
"Excellent work, very polished. Score: #{score}/10. Minor refinements could still be made."
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Store the score in state for the assessment agent
|
|
115
|
+
session_service.set_state(session_id: session_id, key: :refinement_score, value: score)
|
|
116
|
+
|
|
117
|
+
critique_result = {
|
|
118
|
+
text: current_text,
|
|
119
|
+
critique: critique,
|
|
120
|
+
score: score
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
# Store the critique for other agents to use
|
|
124
|
+
session_service.set_state(session_id: session_id, key: :current_critique, value: critique_result)
|
|
125
|
+
|
|
126
|
+
# Create success result
|
|
127
|
+
result_hash = {
|
|
128
|
+
status: :success,
|
|
129
|
+
result: critique,
|
|
130
|
+
score: score
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
# Return the details and the result in the format expected by the parent method
|
|
134
|
+
{ details: [result_hash], last_result: result_hash }
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# 2. Improver Agent Implementation
|
|
138
|
+
improver_instance = Legate::Agent.new(definition: improver_agent)
|
|
139
|
+
def improver_instance.execute_plan(_plan, session, session_service, _invocation_id = nil)
|
|
140
|
+
# Get the session ID from the session object
|
|
141
|
+
session_id = session.id
|
|
142
|
+
|
|
143
|
+
# Get the current text and critique
|
|
144
|
+
current_text = session_service.get_state(session_id: session_id, key: :current_text)
|
|
145
|
+
critique_result = session_service.get_state(session_id: session_id, key: :current_critique)
|
|
146
|
+
|
|
147
|
+
# Keep track of iterations
|
|
148
|
+
iteration = session_service.get_state(session_id: session_id, key: :count) || 0
|
|
149
|
+
iteration += 1
|
|
150
|
+
session_service.set_state(session_id: session_id, key: :count, value: iteration)
|
|
151
|
+
|
|
152
|
+
# Generate improved text based on iteration number
|
|
153
|
+
# In a production system, this would use an LLM to generate the improved text
|
|
154
|
+
improved_text = case iteration
|
|
155
|
+
when 1
|
|
156
|
+
'The sleepy cat curled up on the worn mat.'
|
|
157
|
+
when 2
|
|
158
|
+
'The orange tabby cat stretched lazily before settling on the woven mat by the fireplace.'
|
|
159
|
+
when 3
|
|
160
|
+
'Basking in the afternoon sunlight, the orange tabby cat stretched lazily before settling on the intricately woven mat by the crackling fireplace.'
|
|
161
|
+
when 4
|
|
162
|
+
'Basking in the warm afternoon sunlight that streamed through the window, the orange tabby cat stretched lazily, arching its back, before settling comfortably on the intricately woven mat by the crackling fireplace.'
|
|
163
|
+
else
|
|
164
|
+
'Basking in the warm golden rays of the afternoon sunlight that streamed through the dusty bay window, the elderly orange tabby cat stretched lazily, arching its back with practiced grace, before settling comfortably on the intricately woven Persian mat positioned strategically by the crackling oak-wood fireplace.'
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Store the improved text for next iteration
|
|
168
|
+
session_service.set_state(session_id: session_id, key: :current_text, value: improved_text)
|
|
169
|
+
|
|
170
|
+
# Create success result
|
|
171
|
+
result_hash = {
|
|
172
|
+
status: :success,
|
|
173
|
+
result: "Improved text based on critique (score: #{critique_result[:score]}/10):\n#{improved_text}"
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
# Return the details and the result in the format expected by the parent method
|
|
177
|
+
{ details: [result_hash], last_result: result_hash }
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# 3. Assessment Agent Implementation
|
|
181
|
+
assessment_instance = Legate::Agent.new(definition: assessment_agent)
|
|
182
|
+
def assessment_instance.execute_plan(_plan, session, session_service, _invocation_id = nil)
|
|
183
|
+
# Get the session ID from the session object
|
|
184
|
+
session_id = session.id
|
|
185
|
+
|
|
186
|
+
# Get the current score
|
|
187
|
+
score = session_service.get_state(session_id: session_id, key: :refinement_score)
|
|
188
|
+
|
|
189
|
+
# Determine if we're done (score >= 8)
|
|
190
|
+
done = score >= 8
|
|
191
|
+
|
|
192
|
+
# Set the done flag to control loop termination
|
|
193
|
+
session_service.set_state(session_id: session_id, key: :refinement_done, value: done)
|
|
194
|
+
|
|
195
|
+
# Create success result
|
|
196
|
+
result_hash = {
|
|
197
|
+
status: :success,
|
|
198
|
+
result: if done
|
|
199
|
+
"Refinement complete! Final score: #{score}/10"
|
|
200
|
+
else
|
|
201
|
+
"Refinement should continue. Current score: #{score}/10"
|
|
202
|
+
end
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
# Return the details and the result in the format expected by the parent method
|
|
206
|
+
{ details: [result_hash], last_result: result_hash }
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# Create the loop agent to orchestrate the process
|
|
210
|
+
loop_agent_instance = Legate::Agents::LoopAgent.new(
|
|
211
|
+
definition: refinement_loop,
|
|
212
|
+
sub_agents: [critic_instance, improver_instance, assessment_instance]
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
# Start all agents
|
|
216
|
+
puts 'Starting agents...'
|
|
217
|
+
critic_instance.start
|
|
218
|
+
improver_instance.start
|
|
219
|
+
assessment_instance.start
|
|
220
|
+
loop_agent_instance.start
|
|
221
|
+
|
|
222
|
+
# Run the refinement loop
|
|
223
|
+
puts 'Starting text refinement process...'
|
|
224
|
+
puts '------------------------------'
|
|
225
|
+
|
|
226
|
+
result = loop_agent_instance.run_task(
|
|
227
|
+
session_id: session_id,
|
|
228
|
+
user_input: "Please refine this text: #{initial_text}",
|
|
229
|
+
session_service: session_service
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
puts 'Text refinement process complete!'
|
|
233
|
+
puts '------------------------------'
|
|
234
|
+
puts "Final result: #{result.content[:status]}"
|
|
235
|
+
puts "Iterations completed: #{result.content[:iterations_completed] || 'N/A'}"
|
|
236
|
+
puts "Loop condition met? #{result.content[:loop_condition_met] || 'N/A'}"
|
|
237
|
+
puts
|
|
238
|
+
|
|
239
|
+
if result.content[:status] == :error
|
|
240
|
+
puts "Error: #{result.content[:error_message]}"
|
|
241
|
+
puts '------------------------------'
|
|
242
|
+
exit 1
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# Print a summary of the refinement process
|
|
246
|
+
puts 'Refinement process summary:'
|
|
247
|
+
puts '------------------------------'
|
|
248
|
+
puts "Initial text: #{initial_text}"
|
|
249
|
+
puts
|
|
250
|
+
|
|
251
|
+
if result.content[:iterations]
|
|
252
|
+
result.content[:iterations].each_with_index do |iteration, i|
|
|
253
|
+
puts "ITERATION #{i + 1}:"
|
|
254
|
+
|
|
255
|
+
# Extract and show results from each sub-agent
|
|
256
|
+
critic_result = iteration[:results].find { |r| r[:agent] == :critic_agent }
|
|
257
|
+
improver_result = iteration[:results].find { |r| r[:agent] == :improver_agent }
|
|
258
|
+
assessment_result = iteration[:results].find { |r| r[:agent] == :assessment_agent }
|
|
259
|
+
|
|
260
|
+
if critic_result && improver_result && assessment_result
|
|
261
|
+
puts "Critique: #{critic_result[:result][:result]}"
|
|
262
|
+
puts "Score: #{critic_result[:result][:score]}/10"
|
|
263
|
+
puts "Improvement: #{improver_result[:result][:result]}"
|
|
264
|
+
puts "Assessment: #{assessment_result[:result][:result]}"
|
|
265
|
+
else
|
|
266
|
+
puts 'Incomplete iteration data'
|
|
267
|
+
end
|
|
268
|
+
puts
|
|
269
|
+
end
|
|
270
|
+
else
|
|
271
|
+
puts 'No iteration details available.'
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
# Show final refined text
|
|
275
|
+
final_text = session_service.get_state(session_id: session_id, key: :current_text)
|
|
276
|
+
puts 'FINAL REFINED TEXT:'
|
|
277
|
+
puts final_text
|
|
278
|
+
puts '------------------------------'
|