Module: Chocomint::Factory

Defined in:
lib/chocomint/factory.rb

Overview

config から各コンポーネントを組み立てて Orchestrator を返す。

Class Method Summary collapse

Class Method Details

.api_key(llm_config) ⇒ Object



143
144
145
146
# File 'lib/chocomint/factory.rb', line 143

def api_key(llm_config)
  env_name = llm_config["api_key_env"]
  env_name && ENV[env_name]
end

.build(config, base_dir: Dir.pwd) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/chocomint/factory.rb', line 13

def build(config, base_dir: Dir.pwd)
  path_guard = build_path_guard(config, base_dir: base_dir)

  registry = Tools::Registry.new
  registry.register(Tools::WriteFile.new(path_guard: path_guard,
                                         max_file_bytes: config.max_file_bytes))
  registry.register(Tools::ReadFile.new(path_guard: path_guard,
                                        max_file_bytes: config.max_file_bytes))
  registry.register(Tools::ListDir.new(path_guard: path_guard))
  registry.register(Tools::AppendFile.new(path_guard: path_guard,
                                          max_file_bytes: config.max_file_bytes))
  registry.register(Tools::DeleteFile.new(path_guard: path_guard))
  registry.register(Tools::FileInfo.new(path_guard: path_guard))
  registry.register(Tools::MakeDir.new(path_guard: path_guard))
  registry.register(Tools::RunCommand.new(allowed_commands: config.allowed_commands,
                                          timeout_sec: config.timeout_sec,
                                          max_output_bytes: config.max_output_bytes,
                                          cwd: base_dir,
                                          path_guard: path_guard,
                                          allow_all: config.allow_all_commands?))
  registry.register(Tools::Edit.new(path_guard: path_guard,
                                    max_file_bytes: config.max_file_bytes))
  registry.register(Tools::Grep.new(path_guard: path_guard,
                                    max_output_bytes: config.max_output_bytes,
                                    base_dir: base_dir))
  registry.register(Tools::Glob.new(path_guard: path_guard, base_dir: base_dir))
  registry.register(Tools::Ls.new(path_guard: path_guard))
  registry.register(Tools::Shell.build("bash", timeout_sec: config.timeout_sec,
                                               max_output_bytes: config.max_output_bytes,
                                               cwd: base_dir,
                                               path_guard: path_guard))
  registry.register(Tools::Shell.build("powershell", timeout_sec: config.timeout_sec,
                                                     max_output_bytes: config.max_output_bytes,
                                                     cwd: base_dir,
                                                     path_guard: path_guard))

  # ロガーは LLM クライアントにも注入するため先に生成する。
  logger = Logger::SqliteLogger.new(config.sqlite_path)

  # ウェブサイト参照ツールは取得した本文を LLM (ChatClient) に要約・抽出させる。
  registry.register(Tools::FetchUrl.new(chat_client: chat_client(config, logger),
                                        timeout_sec: config.timeout_sec))

  primary = build_primary(config, registry, logger)
  verifier = build_verifier_client(config, logger)
  semantic = Validator::SemanticValidator.new(
    verifier_client: verifier, enabled: config.verifier_enabled?, path_guard: path_guard
  )

  machine = Validator::MachineValidator.new(
    path_guard: path_guard, max_output_bytes: config.max_output_bytes
  )

  orchestrator = Orchestrator.new(
    primary: primary,
    registry: registry,
    machine_validator: machine,
    semantic_validator: semantic,
    logger: logger,
    max_retry: config.max_retry,
    timeout_sec: config.timeout_sec
  )

  # マルチステップ (複数ツールを順に実行する要求) に対応する外側ループ。
  # 単一ツールで完結する要求は 1 ステップ目の全体判定で即完了する。
  Planner.new(
    primary: primary,
    orchestrator: orchestrator,
    verifier: config.verifier_enabled? ? verifier : nil,
    logger: logger,
    max_steps: config.max_steps,
    step_retry: config.max_retry
  )
end

.build_chat_client(config, base_dir: Dir.pwd) ⇒ Object

会話 (ツール不要の普通の質問) に答えるための生クライアント。 /edit チャットで、タスク判定が「会話」だったときに直接応答させるのに使う。



90
91
92
# File 'lib/chocomint/factory.rb', line 90

def build_chat_client(config, base_dir: Dir.pwd)
  chat_client(config, Logger::SqliteLogger.new(config.sqlite_path))
end

.build_path_guard(config, base_dir: Dir.pwd) ⇒ Object

config から PathGuard を組み立てる (edit UI がファイルアクセスに使う)。



9
10
11
# File 'lib/chocomint/factory.rb', line 9

def build_path_guard(config, base_dir: Dir.pwd)
  Security::PathGuard.new(config.allowed_dirs, base_dir: base_dir)
end

.build_primary(config, registry, logger) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chocomint/factory.rb', line 110

def build_primary(config, registry, logger)
  client = LLM::BaseClient.new(
    base_url: config.llm.fetch("base_url"),
    model: config.llm.fetch("model"),
    api_key: api_key(config.llm),
    max_tokens: config.llm.fetch("max_tokens", 1024),
    timeout: config.llm_timeout_sec,
    payload_logger: logger,
    role: "primary",
    provider: config.llm_provider
  )
  LLM::PrimaryClient.new(base_client: client,
                         tool_definitions: registry.tool_definitions)
end

.build_verifier_client(config, logger) ⇒ Object

verifier が無効なら nil を返す (Planner / SemanticValidator 双方でそのまま扱える)。



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chocomint/factory.rb', line 126

def build_verifier_client(config, logger)
  return nil unless config.verifier_enabled?

  # verifier は primary と同じモデルを共有する (VRAM 節約のため単一モデルに統一)。
  client = LLM::BaseClient.new(
    base_url: config.llm.fetch("base_url"),
    model: config.llm.fetch("model"),
    api_key: api_key(config.llm),
    max_tokens: config.verifier.fetch("max_tokens", 1024),
    timeout: config.llm_timeout_sec,
    payload_logger: logger,
    role: "verifier",
    provider: config.llm_provider
  )
  LLM::VerifierClient.new(base_client: client)
end

.chat_client(config, logger) ⇒ Object

ChatClient を組み立てる。build 内では既存 logger を再利用するため logger を受け取る (fetch_url ツールと /edit チャットの双方で使う)。



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chocomint/factory.rb', line 96

def chat_client(config, logger)
  client = LLM::BaseClient.new(
    base_url: config.llm.fetch("base_url"),
    model: config.llm.fetch("model"),
    api_key: api_key(config.llm),
    max_tokens: config.llm.fetch("max_tokens", 1024),
    timeout: config.llm_timeout_sec,
    payload_logger: logger,
    role: "chat",
    provider: config.llm_provider
  )
  LLM::ChatClient.new(base_client: client)
end