From a01f19b112d3f1da524b6a57337d68fa2d65c467 Mon Sep 17 00:00:00 2001 From: Hare Date: Thu, 8 Jan 2026 20:35:28 +0900 Subject: [PATCH] feat: Adjust module re-exports and publishing settings --- worker-macros/Cargo.toml | 1 + worker-types/Cargo.toml | 1 + worker/examples/worker_cli.rs | 5 +- worker/src/lib.rs | 66 +++++++++++++++---- worker/src/llm_client/mod.rs | 2 +- worker/src/timeline/mod.rs | 33 ++++++++++ .../{ => timeline}/text_block_collector.rs | 2 +- worker/src/{ => timeline}/timeline.rs | 0 .../src/{ => timeline}/tool_call_collector.rs | 2 +- worker/src/worker.rs | 4 +- worker/tests/common/mod.rs | 2 +- worker/tests/subscriber_test.rs | 3 +- worker/tests/worker_fixtures.rs | 3 +- 13 files changed, 101 insertions(+), 23 deletions(-) create mode 100644 worker/src/timeline/mod.rs rename worker/src/{ => timeline}/text_block_collector.rs (99%) rename worker/src/{ => timeline}/timeline.rs (100%) rename worker/src/{ => timeline}/tool_call_collector.rs (99%) diff --git a/worker-macros/Cargo.toml b/worker-macros/Cargo.toml index 16e04da..04ae47a 100644 --- a/worker-macros/Cargo.toml +++ b/worker-macros/Cargo.toml @@ -2,6 +2,7 @@ name = "worker-macros" version = "0.1.0" edition = "2024" +publish = false [lib] proc-macro = true diff --git a/worker-types/Cargo.toml b/worker-types/Cargo.toml index d878a53..c19db01 100644 --- a/worker-types/Cargo.toml +++ b/worker-types/Cargo.toml @@ -2,6 +2,7 @@ name = "worker-types" version = "0.1.0" edition = "2024" +publish = false [dependencies] async-trait = "0.1.89" diff --git a/worker/examples/worker_cli.rs b/worker/examples/worker_cli.rs index be046b2..b6f8a2b 100644 --- a/worker/examples/worker_cli.rs +++ b/worker/examples/worker_cli.rs @@ -40,8 +40,8 @@ use tracing_subscriber::EnvFilter; use clap::{Parser, ValueEnum}; use worker::{ - ControlFlow, Handler, HookError, TextBlockEvent, TextBlockKind, ToolResult, ToolUseBlockEvent, - ToolUseBlockKind, Worker, WorkerHook, + Worker, + hook::{ControlFlow, HookError, ToolResult, WorkerHook}, llm_client::{ LlmClient, providers::{ @@ -49,6 +49,7 @@ use worker::{ openai::OpenAIClient, }, }, + timeline::{Handler, TextBlockEvent, TextBlockKind, ToolUseBlockEvent, ToolUseBlockKind}, }; use worker_macros::tool_registry; diff --git a/worker/src/lib.rs b/worker/src/lib.rs index 72b5bc3..6180d9e 100644 --- a/worker/src/lib.rs +++ b/worker/src/lib.rs @@ -5,9 +5,9 @@ //! # 主要なコンポーネント //! //! - [`Worker`] - LLMとの対話を管理する中心コンポーネント -//! - [`Tool`] - LLMから呼び出し可能なツール -//! - [`WorkerHook`] - ターン進行への介入 -//! - [`WorkerSubscriber`] - ストリーミングイベントの購読 +//! - [`tool::Tool`] - LLMから呼び出し可能なツール +//! - [`hook::WorkerHook`] - ターン進行への介入 +//! - [`subscriber::WorkerSubscriber`] - ストリーミングイベントの購読 //! //! # Quick Start //! @@ -19,6 +19,7 @@ //! .system_prompt("You are a helpful assistant."); //! //! // ツールを登録(オプション) +//! use worker::tool::Tool; //! worker.register_tool(my_tool); //! //! // 対話を実行 @@ -36,14 +37,57 @@ //! ``` pub mod llm_client; +pub mod timeline; + mod subscriber_adapter; -mod text_block_collector; -mod timeline; -mod tool_call_collector; mod worker; -pub use text_block_collector::TextBlockCollector; -pub use timeline::*; -pub use tool_call_collector::ToolCallCollector; -pub use worker::*; -pub use worker_types::*; +// ============================================================================= +// トップレベル公開(最も頻繁に使う型) +// ============================================================================= + +pub use worker::{Worker, WorkerConfig, WorkerError}; +pub use worker_types::{ContentPart, Message, MessageContent, Role}; + +// ============================================================================= +// 意味のあるモジュールとして公開 +// ============================================================================= + +/// ツール定義 +/// +/// LLMから呼び出し可能なツールを定義するためのトレイトと型。 +pub mod tool { + pub use worker_types::{Tool, ToolError}; +} + +/// Hook機能 +/// +/// ターンの進行・ツール実行に介入するためのトレイトと型。 +pub mod hook { + pub use worker_types::{ControlFlow, HookError, ToolCall, ToolResult, TurnResult, WorkerHook}; +} + +/// イベント購読 +/// +/// LLMからのストリーミングイベントをリアルタイムで受信するためのトレイト。 +pub mod subscriber { + pub use worker_types::WorkerSubscriber; +} + +/// イベント型 +/// +/// LLMからのストリーミングレスポンスを表現するイベント型。 +/// Timeline層を直接使用する場合に必要です。 +pub mod event { + pub use worker_types::{ + BlockAbort, BlockDelta, BlockMetadata, BlockStart, BlockStop, BlockType, DeltaContent, + ErrorEvent, Event, PingEvent, ResponseStatus, StatusEvent, StopReason, UsageEvent, + }; +} + +/// Worker状態 +/// +/// Type-stateパターンによるキャッシュ保護のための状態マーカー型。 +pub mod state { + pub use worker_types::{Locked, Mutable, WorkerState}; +} diff --git a/worker/src/llm_client/mod.rs b/worker/src/llm_client/mod.rs index 7e01614..e2b7477 100644 --- a/worker/src/llm_client/mod.rs +++ b/worker/src/llm_client/mod.rs @@ -1,6 +1,6 @@ //! LLMクライアント層 //! -//! 各LLMプロバイダと通信し、統一された[`Event`](crate::Event)ストリームを出力します。 +//! 各LLMプロバイダと通信し、統一された[`Event`](crate::event::Event)ストリームを出力します。 //! //! # サポートするプロバイダ //! diff --git a/worker/src/timeline/mod.rs b/worker/src/timeline/mod.rs new file mode 100644 index 0000000..0df211f --- /dev/null +++ b/worker/src/timeline/mod.rs @@ -0,0 +1,33 @@ +//! Timeline層 +//! +//! LLMからのイベントストリームを受信し、登録されたHandlerにディスパッチします。 +//! +//! # 主要コンポーネント +//! +//! - [`Timeline`] - イベントストリームの管理とディスパッチ +//! - [`Handler`] - イベントを処理するトレイト +//! - [`TextBlockCollector`] - テキストブロックを収集するHandler +//! - [`ToolCallCollector`] - ツール呼び出しを収集するHandler + +mod text_block_collector; +mod timeline; +mod tool_call_collector; + +// 公開API +pub use text_block_collector::TextBlockCollector; +pub use timeline::{ErasedHandler, HandlerWrapper, Timeline}; +pub use tool_call_collector::ToolCallCollector; + +// worker-typesからのre-export +pub use worker_types::{ + // Core traits + Handler, Kind, + // Block Kinds + TextBlockKind, ThinkingBlockKind, ToolUseBlockKind, + // Block Events + TextBlockEvent, TextBlockStart, TextBlockStop, + ThinkingBlockEvent, ThinkingBlockStart, ThinkingBlockStop, + ToolUseBlockEvent, ToolUseBlockStart, ToolUseBlockStop, + // Meta Kinds + ErrorKind, PingKind, StatusKind, UsageKind, +}; diff --git a/worker/src/text_block_collector.rs b/worker/src/timeline/text_block_collector.rs similarity index 99% rename from worker/src/text_block_collector.rs rename to worker/src/timeline/text_block_collector.rs index 7a28663..455934e 100644 --- a/worker/src/text_block_collector.rs +++ b/worker/src/timeline/text_block_collector.rs @@ -84,7 +84,7 @@ impl Handler for TextBlockCollector { #[cfg(test)] mod tests { use super::*; - use crate::Timeline; + use crate::timeline::Timeline; use worker_types::Event; /// TextBlockCollectorが単一のテキストブロックを正しく収集することを確認 diff --git a/worker/src/timeline.rs b/worker/src/timeline/timeline.rs similarity index 100% rename from worker/src/timeline.rs rename to worker/src/timeline/timeline.rs diff --git a/worker/src/tool_call_collector.rs b/worker/src/timeline/tool_call_collector.rs similarity index 99% rename from worker/src/tool_call_collector.rs rename to worker/src/timeline/tool_call_collector.rs index 8e1d092..9817ccb 100644 --- a/worker/src/tool_call_collector.rs +++ b/worker/src/timeline/tool_call_collector.rs @@ -97,7 +97,7 @@ impl Handler for ToolCallCollector { #[cfg(test)] mod tests { use super::*; - use crate::Timeline; + use crate::timeline::Timeline; use worker_types::Event; #[test] diff --git a/worker/src/worker.rs b/worker/src/worker.rs index 1dca9ca..fe6aafb 100644 --- a/worker/src/worker.rs +++ b/worker/src/worker.rs @@ -5,14 +5,12 @@ use std::sync::{Arc, Mutex}; use futures::StreamExt; use tracing::{debug, info, trace, warn}; -use crate::Timeline; +use crate::timeline::{TextBlockCollector, Timeline, ToolCallCollector}; use crate::llm_client::{ClientError, LlmClient, Request, ToolDefinition}; use crate::subscriber_adapter::{ ErrorSubscriberAdapter, StatusSubscriberAdapter, TextBlockSubscriberAdapter, ToolUseBlockSubscriberAdapter, UsageSubscriberAdapter, }; -use crate::text_block_collector::TextBlockCollector; -use crate::tool_call_collector::ToolCallCollector; use worker_types::{ ContentPart, ControlFlow, HookError, Locked, Message, MessageContent, Mutable, Tool, ToolCall, ToolError, ToolResult, TurnResult, WorkerHook, WorkerState, WorkerSubscriber, diff --git a/worker/tests/common/mod.rs b/worker/tests/common/mod.rs index 018f54a..2a6dfcd 100644 --- a/worker/tests/common/mod.rs +++ b/worker/tests/common/mod.rs @@ -9,7 +9,7 @@ use std::sync::{Arc, Mutex}; use async_trait::async_trait; use futures::Stream; use worker::llm_client::{ClientError, LlmClient, Request}; -use worker::{Handler, TextBlockEvent, TextBlockKind, Timeline}; +use worker::timeline::{Handler, TextBlockEvent, TextBlockKind, Timeline}; use worker_types::{BlockType, DeltaContent, Event}; use std::sync::atomic::{AtomicUsize, Ordering}; diff --git a/worker/tests/subscriber_test.rs b/worker/tests/subscriber_test.rs index 34cf4c0..f9a6147 100644 --- a/worker/tests/subscriber_test.rs +++ b/worker/tests/subscriber_test.rs @@ -7,7 +7,8 @@ mod common; use std::sync::{Arc, Mutex}; use common::MockLlmClient; -use worker::{Worker, WorkerSubscriber}; +use worker::subscriber::WorkerSubscriber; +use worker::Worker; use worker_types::{ ErrorEvent, Event, ResponseStatus, StatusEvent, TextBlockEvent, ToolCall, ToolUseBlockEvent, UsageEvent, diff --git a/worker/tests/worker_fixtures.rs b/worker/tests/worker_fixtures.rs index 3e3aee3..9a43b3d 100644 --- a/worker/tests/worker_fixtures.rs +++ b/worker/tests/worker_fixtures.rs @@ -205,8 +205,7 @@ async fn test_worker_with_programmatic_events() { /// id, name, input(JSON)を正しく抽出できることを検証する。 #[tokio::test] async fn test_tool_call_collector_integration() { - use worker::Timeline; - use worker::ToolCallCollector; + use worker::timeline::{Timeline, ToolCallCollector}; use worker_types::Event; // ToolUseブロックを含むイベントシーケンス