1 Commits
Author SHA1 Message Date
Hare aa4e48d53e Merge pull request 'alpha-release: 0.0.1' (#1) from develop into master
Reviewed-on: #1
2026-01-08 20:40:24 +09:00
105 changed files with 3709 additions and 7190 deletions
+109
View File
@@ -0,0 +1,109 @@
---
description: ドキュメントコメントの書き方ガイドライン
---
# ドキュメントコメント スタイルガイド
## 基本原則
1. **利用者視点で書く**: 「何をするものか」「どう使うか」を先に、「なぜそう実装したか」は後に
2. **型パラメータはバッククォートで囲む**: `Handler<K>` ✓ / Handler<K> ✗
3. **Examplesは`worker::`パスで書く**: re-export先のパスを使用
## 構造テンプレート
```rust
/// [1行目: 何をするものか - 利用者が最初に知りたいこと]
///
/// [詳細説明: いつ使うか、なぜ使うか、注意点など]
///
/// # Examples
///
/// ```
/// use worker::SomeType;
///
/// let instance = SomeType::new();
/// instance.do_something();
/// ```
///
/// # Notes (オプション)
///
/// 実装上の注意事項や制限があれば記載
pub struct SomeType { ... }
```
## 良い例・悪い例
### 構造体/Trait
```rust
// ❌ 悪い例(実装視点)
/// Handler<K>からErasedHandler<K>へのラッパー
/// 各Handlerは独自のScope型を持つため、Timelineで保持するには型消去が必要
// ✅ 良い例(利用者視点)
/// `Handler<K>`を`ErasedHandler<K>`として扱うためのラッパー
///
/// 通常は直接使用せず、`Timeline::on_text_block()`などのメソッド経由で
/// 自動的にラップされます。
```
### メソッド
```rust
// ❌ 悪い例(処理内容の説明のみ)
/// ツールを登録する
// ✅ 良い例(何が起きるか、どう使うか)
/// ツールを登録する
///
/// 登録されたツールはLLMからの呼び出しで自動的に実行されます。
/// 同名のツールを登録した場合、後から登録したものが優先されます。
///
/// # Examples
///
/// ```
/// use worker::{Worker, Tool};
///
/// worker.register_tool(MyTool::new());
/// ```
```
### 型パラメータ
```rust
// ❌ HTMLタグとして解釈されてしまう
/// Handler<K>を保持するフィールド
// ✅ バッククォートで囲む
/// `Handler<K>`を保持するフィールド
```
## ドキュメントの配置
| 項目 | 配置場所 |
|-----|---------|
| 型/trait/関数のdoc | 定義元のクレート(worker-types等) |
| モジュールdoc (`//!`) | 各クレートのlib.rsに書く |
| 実装詳細 | 実装コメント (`//`) を使用 |
| 利用者向けでない内部型 | `#[doc(hidden)]`または`pub(crate)` |
## Examplesのuseパス
re-exportされる型のExamplesでは、最終的な公開パスを使用:
```rust
// worker-types/src/tool.rs でも
/// # Examples
/// ```
/// use worker::Tool; // ✓ worker_types::Tool ではなく
/// ```
```
## チェックリスト
- [ ] 1行目は「何をするものか」を利用者視点で説明しているか
- [ ] 型パラメータ (`<T>`, `<K>` 等) はバッククォートで囲んでいるか
- [ ] 主要なpub APIにはExamplesがあるか
- [ ] Examplesの`use`パスは`worker::`になっているか
- [ ] `cargo doc --no-deps`で警告が出ないか
+33 -5
View File
@@ -1,7 +1,35 @@
# llm-worker-rs Development Instructions # llm-worker-rs 開発instruction
## Package Management Rules ## パッケージ管理ルール
- When adding or updating crate dependencies, always use the `cargo` command. Do - クレートに依存関係を追加・更新する際は必ず
not manually edit `Cargo.toml` directly; always manage dependencies via `cargo`コマンドを使い、`Cargo.toml`を直接手で書き換えず、必ずコマンド経由で管理すること。
commands.
## worker-types
`worker-types` クレートには次の条件を満たす型だけを置く。
1. **共有セマンティクスの源泉**
- ランタイム(`worker`)、proc-macro(`worker-macros`)、外部利用者のすべてで同じ定義を共有したい値型。
- 例: `BlockId`, `ProviderEvent`, `ToolArgumentsDelta` などイベント/DTO群。
2. **シリアライズ境界を越えるもの**
- serde経由でプロセス外へ渡したり、APIレスポンスとして公開するもの。
- ロジックを持たない純粋なデータキャリアに限定する。
3. **依存の最小化が必要な型**
- `serde`, `serde_json` 程度の軽量依存で収まる。
4. **マクロが直接参照する型**
- 属性/derive/proc-macro が型に対してコード生成する場合は `worker-macros` ->
`worker-types` の単方向依存を維持するため、対象型を `worker-types` に置く。
5. **副作用を伴わないこと**
- `worker-types` 内では I/O・状態保持・スレッド操作などの副作用を禁止。
- 振る舞いを持つ場合でも `impl`
は純粋な計算か軽量ユーティリティのみに留める。
この基準に当てはまらない型(例えばクライアント状態管理、エラー型で追加依存が必要、プロバイダ固有ロジックなど)は
`worker` クレート側に配置し、どうしても公開が必要なら `worker`
経由で再エクスポートする。 何にせよ、`worker` ->
`worker-types`の片方向依存を維持すること。
Generated
+557 -254
View File
File diff suppressed because it is too large Load Diff
+3 -8
View File
@@ -1,12 +1,7 @@
[workspace] [workspace]
resolver = "2" resolver = "2"
members = [ members = [
"llm-worker", "worker",
"llm-worker-macros", "worker-types",
"worker-macros",
] ]
[workspace.package]
publish = true
edition = "2024"
license = "MIT"
repository = "https://gitea.hareworks.net/Hare/llm_worker_rs"
-8
View File
@@ -1,8 +0,0 @@
Copyright 2026 Hare
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+1 -35
View File
@@ -1,35 +1 @@
# llm-worker # llm-worker-rs
Rusty, Efficient, and Agentic LLM Client Library
`llm-worker` is a Rust library for building autonomous LLM-powered systems. Define tools, register hooks, and let the Worker handle the agentic loop — tool calls are executed automatically until the task completes.
## Features
- Autonomous Execution: The `Worker` manages the full request-response-tool cycle. You provide tools and input; it loops until done.
- Multi-Provider Support: Unified interface for Anthropic, Gemini, OpenAI, and Ollama.
- Tool System: Define tools as async functions. The Worker automatically parses LLM tool calls, executes them in parallel, and feeds results back.
- Hook System: Intercept execution flow with `before_tool_call`, `after_tool_call`, and `on_turn_end` hooks for validation, logging, or self-correction.
- Event-Driven Streaming: Subscribe to real-time events (text deltas, tool calls, usage) for responsive UIs.
- Cache-Aware State Management: Type-state pattern (`Mutable``CacheLocked`) ensures KV cache efficiency by protecting the conversation prefix.
## Quick Start
```rust
use llm_worker::{Worker, Message};
// Create a Worker with your LLM client
let mut worker = Worker::new(client)
.system_prompt("You are a helpful assistant.");
// Register tools (optional)
worker.register_tool(SearchTool::new());
worker.register_tool(CalculatorTool::new());
// Run — the Worker handles tool calls automatically
let history = worker.run("What is 2+2?").await?;
```
## License
MIT
-7
View File
@@ -1,7 +0,0 @@
[licenses]
allow = [
"MIT",
"Apache-2.0",
"Unicode-3.0",
]
confidence-threshold = 0.8
-83
View File
@@ -1,83 +0,0 @@
# Worker API/DSL 実装計画
## 目的
- [Open Responses](https://www.openresponses.org)(以後"OR")に準拠した正規化を前提に、
Item/Part の2段スコープを扱える Worker API を設計する。
- APIの煩雑化を防ぐため、worker.on_xxx として公開するのを避けつつ、
Text/Thinking/Tool など型の違いを静的に扱える DSL を提供する。
## 方針
- 内部は Timeline が Event を正規化し、Item/Part/Meta
を単一ストリームとして扱う。
- API では Item/Part 型ごとに ctx を持てるようにし、DSL
で記述の冗長さを削減する。
- まず macro_rules! 版を作り、必要なら proc-macro に拡張する。
- Item/Part の型パラメータはクレートが公開する Kind 型を使う。
## 仕様の前提
- Item は OR の item (message, function_call, reasoning など) に対応する。
- Part は OR の content part (output_text, reasoning_text など) に対応する。
- Item は必ず start/stop を持つ。Part は Item 内で複数発生し得る。
- Item/Part の型指定は `Item<Message>` / `Part<ReasoningText>` のように書く。
## 設計ステップ
### 1. 内部イベントモデルの整理
- Event を Item/Part/Meta の3層に整理する。
- ItemEvent / PartEvent は型パラメータで区別する。
- 例: ItemEvent<Message>, PartEvent<Message, OutputText>
### 2. スコープの二段化
- Item ctx: Item 型ごとに1つ
- Part ctx: Part 型ごとに1つ
- Part のイベントでは常に item ctx と part ctx の両方を渡す。
### 3. Handler trait の再定義
- Item/Part を型で指定できる trait を導入する。
- 例:
- trait ItemHandler<I>
- trait PartHandler<I, P>
- PartHandler には ItemHandler の ItemCtx を必須で渡す。
- Part の ctx 型は `PartKind::Ctx` 方式 or enum 方式で切り替える。
### 4. Timeline との結合
- Timeline は ItemStart で ItemCtx を生成
- PartStart で PartCtx を生成
- Delta/Stop は対応 ctx に流す
- ItemStop で ItemCtx を破棄
### 5. DSL (macro_rules!) の導入
- まず宣言的 DSL を提供する。
- 例:
- handler! { Item<Message> { type ItemCtx = ...; Part<OutputText> { type
PartCtx = ...; } } }
- DSL は ItemHandler / PartHandler 実装を生成する。
- Item/Part の Kind 型はクレートが公開する型を参照する。
### 6. 拡張ポイント
- 追加 Part (output_image など) を DSL に追加しやすい形にする。
- 必要なら proc-macro に移行して構文自由度を上げる。
## 実装順序
1. Event/Item/Part の型定義の整理
2. Item/Part ctx を持つ Timeline 実装
3. Handler trait の定義・既存コードの移行
4. macro_rules! DSL の実装
5. 既存ユースケースの移植
## TODO
- Item と Part の型対応表を整理する
- OR と既存 llm_client の差分を再確認する
- Tool args の delta を OR 拡張として扱うか検討する
- macro_rules! で表現可能な DSL の最小文法を確定する
-80
View File
@@ -1,80 +0,0 @@
# Open Responses mapping (llm_client -> Open Responses)
This document maps the current `llm_client` event model to Open Responses items
and streaming events. It focuses on output streaming; input items are noted
where they are the closest semantic match.
## Legend
- **OR item**: Open Responses item types used in `response.output`.
- **OR event**: Open Responses streaming events (`response.*`).
- **Note**: Gaps or required adaptation decisions.
## Response lifecycle / meta events
| llm_client | Open Responses | Note |
| ------------------------ | ------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| `StatusEvent::Started` | `response.created`, `response.queued`, `response.in_progress` | OR has finer-grained lifecycle states; pick a subset or map Started -> `response.in_progress`. |
| `StatusEvent::Completed` | `response.completed` | |
| `StatusEvent::Failed` | `response.failed` | |
| `StatusEvent::Cancelled` | (no direct event) | Could map to `response.incomplete` or `response.failed` depending on semantics. |
| `UsageEvent` | `response.completed` payload usage | OR reports usage on the response object, not as a dedicated streaming event. |
| `ErrorEvent` | `error` event | OR has a dedicated error streaming event. |
| `PingEvent` | (no direct event) | OR does not define a heartbeat event. |
## Output block lifecycle
### Text block
| llm_client | Open Responses | Note |
| ------------------------------------------------- | ---------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| `BlockStart { block_type: Text, metadata: Text }` | `response.output_item.added` with item type `message` (assistant) | OR output items are message/function_call/reasoning. This creates the message item. |
| `BlockDelta { delta: Text(..) }` | `response.output_text.delta` | Text deltas map 1:1 to output text deltas. |
| `BlockStop { block_type: Text }` | `response.output_text.done` + `response.content_part.done` + `response.output_item.done` | OR emits separate done events for content parts and items. |
### Tool use (function call)
| llm_client | Open Responses | Note |
| -------------------------------------------------------------------- | --------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| `BlockStart { block_type: ToolUse, metadata: ToolUse { id, name } }` | `response.output_item.added` with item type `function_call` | OR uses `call_id` + `name` + `arguments` string. Map `id` -> `call_id`. |
| `BlockDelta { delta: InputJson(..) }` | `response.function_call_arguments.delta` | OR spec does not explicitly require argument deltas; treat as OpenAI-compatible extension if adopted. |
| `BlockStop { block_type: ToolUse }` | `response.function_call_arguments.done` + `response.output_item.done` | Item status can be set to `completed` or `incomplete`. |
### Tool result (function call output)
| llm_client | Open Responses | Note |
| ----------------------------------------------------------------------------- | ------------------------------------- | ---------------------------------------------------------------------------------------- |
| `BlockStart { block_type: ToolResult, metadata: ToolResult { tool_use_id } }` | **Input item** `function_call_output` | OR treats tool results as input items, not output items. This is a request-side mapping. |
| `BlockDelta` | (no direct output event) | OR does not stream tool output deltas as response events. |
| `BlockStop` | (no direct output event) | Tool output lives on the next request as an input item. |
### Thinking / reasoning
| llm_client | Open Responses | Note |
| --------------------------------------------------------- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `BlockStart { block_type: Thinking, metadata: Thinking }` | `response.output_item.added` with item type `reasoning` | OR models reasoning as a separate item type. |
| `BlockDelta { delta: Thinking(..) }` | `response.reasoning.delta` | OR has dedicated reasoning delta events. |
| `BlockStop { block_type: Thinking }` | `response.reasoning.done` | OR separates reasoning summary events (`response.reasoning_summary_*`) from reasoning deltas. Decide whether Thinking maps to full reasoning or summary only. |
## Stop reasons
| llm_client `StopReason` | Open Responses | Note |
| ----------------------- | ------------------------------------------------------------------------------ | ---------------------------------------------- |
| `EndTurn` | `response.completed` + item status `completed` | |
| `MaxTokens` | `response.incomplete` + item status `incomplete` | |
| `StopSequence` | `response.completed` | |
| `ToolUse` | `response.completed` for message item, followed by `function_call` output item | OR models tool call as a separate output item. |
## Gaps / open decisions
- `PingEvent` has no OR equivalent. If needed, keep as internal only.
- `Cancelled` status needs a policy: map to `response.incomplete` or
`response.failed`.
- OR has `response.refusal.delta` / `response.refusal.done`. `llm_client` has no
refusal delta type; consider adding a new block or delta variant if needed.
- OR splits _item_ and _content part_ lifecycles. `llm_client` currently has a
single block lifecycle, so mapping should decide whether to synthesize
`content_part.*` events or ignore them.
- The OR specification does not state how `function_call.arguments` stream
deltas; `response.function_call_arguments.*` should be treated as a compatible
extension if required.
+1 -1
View File
@@ -17,7 +17,7 @@ LLMを用いたワーカーを作成する小型のSDK・ライブラリ。
module構成概念図 module構成概念図
```plaintext ```
worker worker
├── context ├── context
├── llm_client ├── llm_client
+3 -3
View File
@@ -27,7 +27,7 @@ RustのType-stateパターンを利用し、Workerの状態によって利用可
* 自由な編集が可能な状態。 * 自由な編集が可能な状態。
* システムプロンプトの設定・変更が可能。 * システムプロンプトの設定・変更が可能。
* メッセージ履歴の初期構築(ロード、編集)が可能。 * メッセージ履歴の初期構築(ロード、編集)が可能。
* **`CacheLocked` (キャッシュ保護状態)** * **`Locked` (キャッシュ保護状態)**
* キャッシュの有効活用を目的とした、前方不変状態。 * キャッシュの有効活用を目的とした、前方不変状態。
* **システムプロンプトの変更不可** * **システムプロンプトの変更不可**
* **既存メッセージ履歴の変更不可**(追記のみ許可)。 * **既存メッセージ履歴の変更不可**(追記のみ許可)。
@@ -47,7 +47,7 @@ worker.history_mut().push(initial_message);
// 3. ロックしてLocked状態へ遷移 // 3. ロックしてLocked状態へ遷移
// これにより、ここまでのコンテキストが "Fixed Prefix" として扱われる // これにより、ここまでのコンテキストが "Fixed Prefix" として扱われる
let mut locked_worker: Worker<CacheLocked> = worker.lock(); let mut locked_worker: Worker<Locked> = worker.lock();
// 4. 利用 (Locked状態) // 4. 利用 (Locked状態)
// 実行は可能。新しいメッセージは履歴の末尾に追記される。 // 実行は可能。新しいメッセージは履歴の末尾に追記される。
@@ -65,4 +65,4 @@ locked_worker.run(new_user_input).await?;
* **状態パラメータの導入**: `Worker<S: WorkerState>` の導入。 * **状態パラメータの導入**: `Worker<S: WorkerState>` の導入。
* **コンテキスト所有権の委譲**: `run` メソッドの引数でコンテキストを受け取るのではなく、`Worker` 内部に `history: Vec<Message>` を保持し管理する形へ移行する。 * **コンテキスト所有権の委譲**: `run` メソッドの引数でコンテキストを受け取るのではなく、`Worker` 内部に `history: Vec<Message>` を保持し管理する形へ移行する。
* **APIの分離**: `Mutable` 特有のメソッド(setter等)と、`CacheLocked` でも使えるメソッド(実行、参照等)をトレイト境界で分離する。 * **APIの分離**: `Mutable` 特有のメソッド(setter等)と、`Locked` でも使えるメソッド(実行、参照等)をトレイト境界で分離する。
-70
View File
@@ -1,70 +0,0 @@
# 非同期キャンセル設計
Workerの非同期キャンセル機構についての設計ドキュメント。
## 概要
`tokio::sync::mpsc`の通知チャネルを用いて、別タスクからWorkerの実行を安全にキャンセルできる。
```rust
let worker = Arc::new(Mutex::new(Worker::new(client)));
// 実行タスク
let w = worker.clone();
let handle = tokio::spawn(async move {
w.lock().await.run("prompt").await
});
// キャンセル
worker.lock().await.cancel();
```
## キャンセル時の処理フロー
```
キャンセル検知
timeline.abort_current_block() // 進行中ブロックの終端処理
run_on_abort_hooks("Cancelled") // on_abort フック呼び出し
Err(WorkerError::Cancelled) // エラー返却
```
## API
| メソッド | 説明 |
| ----------------- | ------------------------------ |
| `cancel()` | キャンセルをトリガー |
| `cancel_sender()` | キャンセル通知用のSenderを取得 |
## on_abort フック
`Hook::on_abort(&self, reason: &str)`がキャンセル時に呼ばれる。
クリーンアップ処理やログ記録に使用できる。
```rust
async fn on_abort(&self, reason: &str) -> Result<(), HookError> {
log::info!("Aborted: {}", reason);
Ok(())
}
```
呼び出しタイミング:
- `WorkerError::Cancelled` — reason: `"Cancelled"`
- `ControlFlow::Abort(reason)` — reason: フックが指定した理由
---
## 既知の問題
### on_abort の発火基準
`on_abort`**interrupt(中断)** された場合に必ず発火する。
interrupt の例:
- `WorkerError::Cancelled`(キャンセル)
- `WorkerError::Aborted`(フックによるAbort
- ストリーム/ツール/クライアント/Hook の各種エラーで処理が中断された場合
+125 -205
View File
@@ -3,8 +3,7 @@
## 概要 ## 概要
HookはWorker層でのターン制御に介入するためのメカニズムです。 HookはWorker層でのターン制御に介入するためのメカニズムです。
Claude CodeのHooks機能に着想を得ており、メッセージ送信・ツール実行・ターン終了の各ポイントで処理を差し込むことができます。
メッセージ送信・ツール実行・ターン終了等の各ポイントで処理を差し込むことができます。
## コンセプト ## コンセプト
@@ -12,184 +11,120 @@ HookはWorker層でのターン制御に介入するためのメカニズムで
- **Contextへのアクセス**: メッセージ履歴を読み書き可能 - **Contextへのアクセス**: メッセージ履歴を読み書き可能
- **非破壊的チェーン**: 複数のHookを登録順に実行、後続Hookへの影響を制御 - **非破壊的チェーン**: 複数のHookを登録順に実行、後続Hookへの影響を制御
## Hook一覧
| Hook | タイミング | 主な用途 | 戻り値 |
| ------------------ | -------------------------- | -------------------------- | ---------------------- |
| `on_prompt_submit` | `run()` 呼び出し時 | ユーザーメッセージの前処理 | `OnPromptSubmitResult` |
| `pre_llm_request` | 各ターンのLLM送信前 | コンテキスト改変/検証 | `PreLlmRequestResult` |
| `pre_tool_call` | ツール実行前 | 実行許可/引数改変 | `PreToolCallResult` |
| `post_tool_call` | ツール実行後 | 結果加工/マスキング | `PostToolCallResult` |
| `on_turn_end` | ツールなしでターン終了直前 | 検証/リトライ指示 | `OnTurnEndResult` |
| `on_abort` | 中断時 | クリーンアップ/通知 | `()` |
## Hook Trait ## Hook Trait
```rust ```rust
#[async_trait] #[async_trait]
pub trait Hook<E: HookEventKind>: Send + Sync { pub trait WorkerHook: Send + Sync {
async fn call(&self, input: &mut E::Input) -> Result<E::Output, HookError>; /// メッセージ送信前
/// リクエストに含まれるメッセージリストを改変できる
async fn on_message_send(
&self,
context: &mut Vec<Message>,
) -> Result<ControlFlow, HookError> {
Ok(ControlFlow::Continue)
}
/// ツール実行前
/// 実行をキャンセルしたり、引数を書き換えることができる
async fn before_tool_call(
&self,
tool_call: &mut ToolCall,
) -> Result<ControlFlow, HookError> {
Ok(ControlFlow::Continue)
}
/// ツール実行後
/// 結果を書き換えたり、隠蔽したりできる
async fn after_tool_call(
&self,
tool_result: &mut ToolResult,
) -> Result<ControlFlow, HookError> {
Ok(ControlFlow::Continue)
}
/// ターン終了時
/// 生成されたメッセージを検査し、必要ならリトライを指示できる
async fn on_turn_end(
&self,
messages: &[Message],
) -> Result<TurnResult, HookError> {
Ok(TurnResult::Finish)
}
} }
``` ```
## 制御フロー型 ## 制御フロー型
### HookEventKind / Result ### ControlFlow
Hookイベントごとに入力/出力型を分離し、意味のない制御フローを排除する Hook処理の継続/中断を制御する列挙型
```rust ```rust
pub trait HookEventKind { pub enum ControlFlow {
type Input; /// 処理を続行(後続Hookも実行)
type Output;
}
pub struct OnPromptSubmit;
pub struct PreLlmRequest;
pub struct PreToolCall;
pub struct PostToolCall;
pub struct OnTurnEnd;
pub struct OnAbort;
pub enum OnPromptSubmitResult {
Continue,
Cancel(String),
}
pub enum PreLlmRequestResult {
Continue,
Cancel(String),
}
pub enum PreToolCallResult {
Continue, Continue,
/// 現在の処理をスキップ(ツール実行をスキップ等)
Skip, Skip,
/// 処理全体を中断(エラーとして扱う)
Abort(String), Abort(String),
Pause,
}
pub enum PostToolCallResult {
Continue,
Abort(String),
}
pub enum OnTurnEndResult {
Finish,
ContinueWithMessages(Vec<Message>),
Paused,
} }
``` ```
### Tool Call Context ### TurnResult
`pre_tool_call` / `post_tool_call` は、ツール実行の文脈を含む入力を受け取る ターン終了時の判定結果を表す列挙型
```rust ```rust
pub struct ToolCallContext { pub enum TurnResult {
pub call: ToolCall, /// ターンを正常終了
pub meta: ToolMeta, // 不変メタデータ Finish,
pub tool: Arc<dyn Tool>, // 状態アクセス用 /// メッセージを追加してターン継続(自己修正など)
} ContinueWithMessages(Vec<Message>),
pub struct PostToolCallContext {
pub call: ToolCall,
pub result: ToolResult,
pub meta: ToolMeta,
pub tool: Arc<dyn Tool>,
} }
``` ```
## 呼び出しタイミング ## 呼び出しタイミング
``` ```
Worker::run(user_input) Worker::run() ループ
├─▶ on_prompt_submit ───────────────────────────┐ ├─▶ on_message_send ──────────────────────────────┐
│ ユーザーメッセージの前処理・検証 │
│ (最初の1回のみ) │
│ │
└─▶ loop {
├─▶ pre_llm_request ──────────────────────│
│ コンテキストの改変、バリデーション、 │ │ コンテキストの改変、バリデーション、 │
│ システムプロンプト注入などが可能 │ │ システムプロンプト注入などが可能 │
│ (毎ターン実行) │
│ │ │ │
├─▶ LLMリクエスト送信 & ストリーム処理 │ ├─▶ LLMリクエスト送信 & ストリーム処理 │
│ │ │ │
├─▶ ツール呼び出しがある場合: │ ├─▶ ツール呼び出しがある場合: │
│ │ │ │ │ │
│ ├─▶ pre_tool_call (各ツールごと・逐次) │ │ ├─▶ before_tool_call (各ツールごと・逐次)
│ │ 実行可否の判定、引数の改変 │ │ │ 実行可否の判定、引数の改変 │
│ │ │ │ │ │
│ ├─▶ ツール並列実行 (join_all) │ │ ├─▶ ツール並列実行 (join_all) │
│ │ │ │ │ │
│ └─▶ post_tool_call (各結果ごと・逐次) │ │ └─▶ after_tool_call (各結果ごと・逐次)
│ 結果の確認、加工、ログ出力 │ │ 結果の確認、加工、ログ出力 │
│ │ │ │
├─▶ ツール結果をコンテキストに追加 ├─▶ ツール結果をコンテキストに追加 → ループ先頭へ
│ → ループ先頭へ │
│ │ │ │
└─▶ ツールなしの場合: │ └─▶ ツールなしの場合: │
│ │ │ │
└─▶ on_turn_end ───────────────────┘ └─▶ on_turn_end ─────────────────────────────┘
最終応答のチェック(Lint/Fmt等) 最終応答のチェック(Lint/Fmt等)
エラーがあればContinueWithMessagesでリトライ エラーがあればContinueWithMessagesでリトライ
}
※ 中断時は on_abort が呼ばれる
``` ```
## 各Hookの詳細 ## 各Hookの詳細
### on_prompt_submit ### on_message_send
**呼び出しタイミング**: `run()` **呼び出しタイミング**: LLMへリクエスト送信前(ターンループの冒頭)
でユーザーメッセージを受け取った直後(最初の1回のみ)
**用途**: **用途**:
- ユーザー入力のバリデーション
- 入力のサニタイズ・フィルタリング
- ログ出力
- `OnPromptSubmitResult::Cancel` による実行キャンセル
**入力**: `&mut Message` - ユーザーメッセージ(改変可能)
**例**: 入力のバリデーション
```rust
struct InputValidator;
#[async_trait]
impl Hook<OnPromptSubmit> for InputValidator {
async fn call(
&self,
message: &mut Message,
) -> Result<OnPromptSubmitResult, HookError> {
if let MessageContent::Text(text) = &message.content {
if text.trim().is_empty() {
return Ok(OnPromptSubmitResult::Cancel("Empty input".to_string()));
}
}
Ok(OnPromptSubmitResult::Continue)
}
}
```
### pre_llm_request
**呼び出しタイミング**: 各ターンのLLMリクエスト送信前(ループの毎回)
**用途**:
- コンテキストへのシステムメッセージ注入 - コンテキストへのシステムメッセージ注入
- メッセージのバリデーション - メッセージのバリデーション
- 機密情報のフィルタリング - 機密情報のフィルタリング
- リクエスト内容のログ出力 - リクエスト内容のログ出力
- `PreLlmRequestResult::Cancel` による送信キャンセル
**入力**: `&mut Vec<Message>` - コンテキスト全体(改変可能)
**例**: メッセージにタイムスタンプを追加 **例**: メッセージにタイムスタンプを追加
@@ -197,33 +132,27 @@ impl Hook<OnPromptSubmit> for InputValidator {
struct TimestampHook; struct TimestampHook;
#[async_trait] #[async_trait]
impl Hook<PreLlmRequest> for TimestampHook { impl WorkerHook for TimestampHook {
async fn call( async fn on_message_send(
&self, &self,
context: &mut Vec<Message>, context: &mut Vec<Message>,
) -> Result<PreLlmRequestResult, HookError> { ) -> Result<ControlFlow, HookError> {
let timestamp = chrono::Local::now().to_rfc3339(); let timestamp = chrono::Local::now().to_rfc3339();
context.insert(0, Message::user(format!("[{}]", timestamp))); context.insert(0, Message::user(format!("[{}]", timestamp)));
Ok(PreLlmRequestResult::Continue) Ok(ControlFlow::Continue)
} }
} }
``` ```
### pre_tool_call ### before_tool_call
**呼び出しタイミング**: 各ツール実行前(並列実行フェーズの前) **呼び出しタイミング**: 各ツール実行前(並列実行フェーズの前)
**用途**: **用途**:
- 危険なツールのブロック - 危険なツールのブロック
- 引数のサニタイズ - 引数のサニタイズ
- 確認プロンプトの表示(UIとの連携) - 確認プロンプトの表示(UIとの連携)
- 実行ログの記録 - 実行ログの記録
- `PreToolCallResult::Pause` による一時停止
**入力**:
- `ToolCallContext``ToolCall` + `ToolMeta` + `Arc<dyn Tool>`
**例**: 特定ツールをブロック **例**: 特定ツールをブロック
@@ -233,52 +162,46 @@ struct ToolBlocker {
} }
#[async_trait] #[async_trait]
impl Hook<PreToolCall> for ToolBlocker { impl WorkerHook for ToolBlocker {
async fn call( async fn before_tool_call(
&self, &self,
ctx: &mut ToolCallContext, tool_call: &mut ToolCall,
) -> Result<PreToolCallResult, HookError> { ) -> Result<ControlFlow, HookError> {
if self.blocked_tools.contains(&ctx.call.name) { if self.blocked_tools.contains(&tool_call.name) {
println!("Blocked tool: {}", ctx.call.name); println!("Blocked tool: {}", tool_call.name);
Ok(PreToolCallResult::Skip) Ok(ControlFlow::Skip)
} else { } else {
Ok(PreToolCallResult::Continue) Ok(ControlFlow::Continue)
} }
} }
} }
``` ```
### post_tool_call ### after_tool_call
**呼び出しタイミング**: 各ツール実行後(並列実行フェーズの後) **呼び出しタイミング**: 各ツール実行後(並列実行フェーズの後)
**用途**: **用途**:
- 結果の加工・フォーマット - 結果の加工・フォーマット
- 機密情報のマスキング - 機密情報のマスキング
- 結果のキャッシュ - 結果のキャッシュ
- 実行結果のログ出力 - 実行結果のログ出力
**入力**:
- `PostToolCallContext``ToolCall` + `ToolResult` + `ToolMeta` +
`Arc<dyn Tool>`
**例**: 結果にプレフィックスを追加 **例**: 結果にプレフィックスを追加
```rust ```rust
struct ResultFormatter; struct ResultFormatter;
#[async_trait] #[async_trait]
impl Hook<PostToolCall> for ResultFormatter { impl WorkerHook for ResultFormatter {
async fn call( async fn after_tool_call(
&self, &self,
ctx: &mut PostToolCallContext, tool_result: &mut ToolResult,
) -> Result<PostToolCallResult, HookError> { ) -> Result<ControlFlow, HookError> {
if !ctx.result.is_error { if !tool_result.is_error {
ctx.result.content = format!("[OK] {}", ctx.result.content); tool_result.content = format!("[OK] {}", tool_result.content);
} }
Ok(PostToolCallResult::Continue) Ok(ControlFlow::Continue)
} }
} }
``` ```
@@ -288,22 +211,10 @@ impl Hook<PostToolCall> for ResultFormatter {
**呼び出しタイミング**: ツール呼び出しなしでターンが終了する直前 **呼び出しタイミング**: ツール呼び出しなしでターンが終了する直前
**用途**: **用途**:
- 生成されたコードのLint/Fmt - 生成されたコードのLint/Fmt
- 出力形式のバリデーション - 出力形式のバリデーション
- 自己修正のためのリトライ指示 - 自己修正のためのリトライ指示
- 最終結果のログ出力 - 最終結果のログ出力
- `OnTurnEndResult::Paused` による一時停止
### on_abort
**呼び出しタイミング**: キャンセル/エラー/AbortなどでWorkerが中断された時
**用途**:
- クリーンアップ処理
- 中断理由のログ出力
- 外部システムへの通知
**例**: JSON形式のバリデーション **例**: JSON形式のバリデーション
@@ -311,11 +222,11 @@ impl Hook<PostToolCall> for ResultFormatter {
struct JsonValidator; struct JsonValidator;
#[async_trait] #[async_trait]
impl Hook<OnTurnEnd> for JsonValidator { impl WorkerHook for JsonValidator {
async fn call( async fn on_turn_end(
&self, &self,
messages: &mut Vec<Message>, messages: &[Message],
) -> Result<OnTurnEndResult, HookError> { ) -> Result<TurnResult, HookError> {
// 最後のアシスタントメッセージを取得 // 最後のアシスタントメッセージを取得
let last = messages.iter().rev() let last = messages.iter().rev()
.find(|m| m.role == Role::Assistant); .find(|m| m.role == Role::Assistant);
@@ -325,25 +236,25 @@ impl Hook<OnTurnEnd> for JsonValidator {
// JSONとしてパースを試みる // JSONとしてパースを試みる
if serde_json::from_str::<serde_json::Value>(text).is_err() { if serde_json::from_str::<serde_json::Value>(text).is_err() {
// 失敗したらリトライ指示 // 失敗したらリトライ指示
return Ok(OnTurnEndResult::ContinueWithMessages(vec![ return Ok(TurnResult::ContinueWithMessages(vec![
Message::user("Invalid JSON. Please fix and try again.") Message::user("Invalid JSON. Please fix and try again.")
])); ]));
} }
} }
} }
Ok(OnTurnEndResult::Finish) Ok(TurnResult::Finish)
} }
} }
``` ```
## 複数Hookの実行順序 ## 複数Hookの実行順序
Hookは**イベントごとに登録順**に実行されます。 Hookは**登録順**に実行されます。
```rust ```rust
worker.add_pre_tool_call_hook(HookA); // 1番目に実行 worker.add_hook(HookA); // 1番目に実行
worker.add_pre_tool_call_hook(HookB); // 2番目に実行 worker.add_hook(HookB); // 2番目に実行
worker.add_pre_tool_call_hook(HookC); // 3番目に実行 worker.add_hook(HookC); // 3番目に実行
``` ```
### 制御フローの伝播 ### 制御フローの伝播
@@ -351,7 +262,6 @@ worker.add_pre_tool_call_hook(HookC); // 3番目に実行
- `Continue`: 後続Hookも実行 - `Continue`: 後続Hookも実行
- `Skip`: 現在の処理をスキップし、後続Hookは実行しない - `Skip`: 現在の処理をスキップし、後続Hookは実行しない
- `Abort`: 即座にエラーを返し、処理全体を中断 - `Abort`: 即座にエラーを返し、処理全体を中断
- `Pause`: Workerを一時停止(再開は`resume`
``` ```
Hook A: Continue → Hook B: Skip → (Hook Cは実行されない) Hook A: Continue → Hook B: Skip → (Hook Cは実行されない)
@@ -361,39 +271,52 @@ Hook A: Continue → Hook B: Skip → (Hook Cは実行されない)
Hook A: Continue → Hook B: Abort("reason") Hook A: Continue → Hook B: Abort("reason")
WorkerError::Aborted WorkerError::Aborted
Hook A: Continue → Hook B: Pause
WorkerResult::Paused
``` ```
## 設計上のポイント ## 設計上のポイント
### 1. イベントごとの実装 ### 1. デフォルト実装
必要なイベントのみ `Hook<Event>` を実装する 全メソッドにデフォルト実装があるため、必要なメソッドだけオーバーライドすれば良い
```rust
struct SimpleLogger;
#[async_trait]
impl WorkerHook for SimpleLogger {
// on_message_send だけ実装
async fn on_message_send(
&self,
context: &mut Vec<Message>,
) -> Result<ControlFlow, HookError> {
println!("Sending {} messages", context.len());
Ok(ControlFlow::Continue)
}
// 他のメソッドはデフォルト(Continue/Finish
}
```
### 2. 可変参照による改変 ### 2. 可変参照による改変
`&mut`で引数を受け取るため、直接改変が可能。 `&mut`で引数を受け取るため、直接改変が可能。
```rust ```rust
async fn call(&self, ctx: &mut ToolCallContext) -> ... { async fn before_tool_call(&self, tool_call: &mut ToolCall) -> ... {
// 引数を直接書き換え // 引数を直接書き換え
ctx.call.input["sanitized"] = json!(true); tool_call.input["sanitized"] = json!(true);
Ok(PreToolCallResult::Continue) Ok(ControlFlow::Continue)
} }
``` ```
### 3. 並列実行との統合 ### 3. 並列実行との統合
- `pre_tool_call`: 並列実行**前**に逐次実行(許可判定のため) - `before_tool_call`: 並列実行**前**に逐次実行(許可判定のため)
- ツール実行: `join_all`で**並列**実行 - ツール実行: `join_all`で**並列**実行
- `post_tool_call`: 並列実行**後**に逐次実行(結果加工のため) - `after_tool_call`: 並列実行**後**に逐次実行(結果加工のため)
### 4. Send + Sync 要件 ### 4. Send + Sync 要件
`Hook``Send + Sync`を要求するため、スレッドセーフな実装が必要。 `WorkerHook``Send + Sync`を要求するため、スレッドセーフな実装が必要。
状態を持つ場合は`Arc<Mutex<T>>``AtomicUsize`などを使用する。 状態を持つ場合は`Arc<Mutex<T>>``AtomicUsize`などを使用する。
```rust ```rust
@@ -402,10 +325,10 @@ struct CountingHook {
} }
#[async_trait] #[async_trait]
impl Hook<PreToolCall> for CountingHook { impl WorkerHook for CountingHook {
async fn call(&self, _: &mut ToolCallContext) -> Result<PreToolCallResult, HookError> { async fn before_tool_call(&self, _: &mut ToolCall) -> Result<ControlFlow, HookError> {
self.count.fetch_add(1, Ordering::SeqCst); self.count.fetch_add(1, Ordering::SeqCst);
Ok(PreToolCallResult::Continue) Ok(ControlFlow::Continue)
} }
} }
``` ```
@@ -413,13 +336,13 @@ impl Hook<PreToolCall> for CountingHook {
## 典型的なユースケース ## 典型的なユースケース
| ユースケース | 使用Hook | 処理内容 | | ユースケース | 使用Hook | 処理内容 |
| ------------------ | -------------------- | -------------------------- | |-------------|----------|----------|
| ツール許可制御 | `pre_tool_call` | 危険なツールをSkip | | ツール許可制御 | `before_tool_call` | 危険なツールをSkip |
| 実行ログ | `pre/post_tool_call` | 呼び出しと結果を記録 | | 実行ログ | `before/after_tool_call` | 呼び出しと結果を記録 |
| 出力バリデーション | `on_turn_end` | 形式チェック、リトライ指示 | | 出力バリデーション | `on_turn_end` | 形式チェック、リトライ指示 |
| コンテキスト注入 | `on_message_send` | システムメッセージ追加 | | コンテキスト注入 | `on_message_send` | システムメッセージ追加 |
| 結果のサニタイズ | `post_tool_call` | 機密情報のマスキング | | 結果のサニタイズ | `after_tool_call` | 機密情報のマスキング |
| レート制限 | `pre_tool_call` | 呼び出し頻度の制御 | | レート制限 | `before_tool_call` | 呼び出し頻度の制御 |
## TODO ## TODO
@@ -427,14 +350,11 @@ impl Hook<PreToolCall> for CountingHook {
現在のHooks実装は基本的なユースケースをカバーしているが、以下の点について将来的に厳密な仕様を定義する必要がある: 現在のHooks実装は基本的なユースケースをカバーしているが、以下の点について将来的に厳密な仕様を定義する必要がある:
- **エラーハンドリングの明確化**: - **エラーハンドリングの明確化**: `HookError`発生時のリカバリー戦略、部分的な失敗の扱い
`HookError`発生時のリカバリー戦略、部分的な失敗の扱い
- **Hook間の依存関係**: 複数Hookの実行順序が結果に影響する場合のセマンティクス - **Hook間の依存関係**: 複数Hookの実行順序が結果に影響する場合のセマンティクス
- **非同期キャンセル**: Hook実行中のキャンセル(タイムアウト等)の振る舞い - **非同期キャンセル**: Hook実行中のキャンセル(タイムアウト等)の振る舞い
- **状態の一貫性**: - **状態の一貫性**: `on_message_send`で改変されたコンテキストが後続処理で期待通りに反映される保証
`on_message_send`で改変されたコンテキストが後続処理で期待通りに反映される保証 - **リトライ制限**: `on_turn_end`での`ContinueWithMessages`による無限ループ防止策
- **リトライ制限**:
`on_turn_end`での`ContinueWithMessages`による無限ループ防止策
- **Hook優先度**: 登録順以外の優先度指定メカニズムの必要性 - **Hook優先度**: 登録順以外の優先度指定メカニズムの必要性
- **条件付きHook**: 特定条件でのみ有効化されるHookパターン - **条件付きHook**: 特定条件でのみ有効化されるHookパターン
- **テスト容易性**: Hookのモック/スタブ作成のためのユーティリティ - **テスト容易性**: Hookのモック/スタブ作成のためのユーティリティ
-191
View File
@@ -1,191 +0,0 @@
# Tool 設計
## 概要
`llm-worker`のツールシステムは、LLMが外部リソースにアクセスしたり計算を実行するための仕組みを提供する。
メタ情報の不変性とセッションスコープの状態管理を両立させる設計となっている。
## 主要な型
```
type ToolDefinition
Fn() -> (ToolMeta, Arc<dyn Tool>)
worker.register_tool() で呼び出し
- struct ToolMeta (name, desc, schema)
不変・登録時固定
- trait Tool (executer)
登録時生成・セッション中再利用
```
### ToolMeta
ツールのメタ情報を保持する不変構造体。登録時に固定され、Worker内で変更されない。
```rust
pub struct ToolMeta {
pub name: String,
pub description: String,
pub input_schema: Value,
}
```
**目的:**
- LLM へのツール定義として送信
- Hook からの参照(読み取り専用)
- 登録後の不変性を保証
### Tool trait
ツールの実行ロジックのみを定義するトレイト。
```rust
#[async_trait]
pub trait Tool: Send + Sync {
async fn execute(&self, input_json: &str) -> Result<String, ToolError>;
}
```
**設計方針:**
- メタ情報(name, description, schema)は含まない
- 状態を持つことが可能(セッション中のカウンターなど)
- `Send + Sync` で並列実行に対応
**インスタンスのライフサイクル:**
1. `register_tool()` 呼び出し時にファクトリが実行され、インスタンスが生成される
2. LLM がツールを呼び出すと、既存インスタンスの `execute()` が実行される
3. 同じセッション中は同一インスタンスが再利用される
※ 「最初に呼ばれたとき」の遅延初期化ではなく、**登録時の即時初期化**である。
### ToolDefinition
メタ情報とツールインスタンスを生成するファクトリ。
```rust
pub type ToolDefinition = Arc<dyn Fn() -> (ToolMeta, Arc<dyn Tool>) + Send + Sync>;
```
**なぜファクトリか:**
- Worker への登録時に一度だけ呼び出される
- メタ情報とインスタンスを同時に生成し、整合性を保証
- クロージャでコンテキスト(`self.clone()`)をキャプチャ可能
## Worker でのツール管理
```rust
// Worker 内部
tools: HashMap<String, (ToolMeta, Arc<dyn Tool>)>
// 登録 API
pub fn register_tool(&mut self, factory: ToolDefinition) -> Result<(), ToolRegistryError>
```
登録時の処理:
1. ファクトリを呼び出し `(meta, instance)` を取得
2. 同名ツールが既に登録されていればエラー
3. HashMap に `(meta, instance)` を保存
## マクロによる自動生成
`#[tool_registry]` マクロは `{method}_definition()` メソッドを生成する。
```rust
#[tool_registry]
impl MyApp {
/// 検索を実行する
#[tool]
async fn search(&self, query: String) -> String {
// 実装
}
}
// 生成されるコード:
impl MyApp {
pub fn search_definition(&self) -> ToolDefinition {
let ctx = self.clone();
Arc::new(move || {
let meta = ToolMeta::new("search")
.description("検索を実行する")
.input_schema(/* schemars で生成 */);
let tool = Arc::new(ToolSearch { ctx: ctx.clone() });
(meta, tool)
})
}
}
```
## Hook との連携
Hook は `ToolCallContext` / `AfterToolCallContext`
を通じてメタ情報とインスタンスにアクセスできる。
```rust
pub struct ToolCallContext {
pub call: ToolCall, // 呼び出し情報(改変可能)
pub meta: ToolMeta, // メタ情報(読み取り専用)
pub tool: Arc<dyn Tool>, // インスタンス(状態アクセス用)
}
```
**用途:**
- `meta` で名前やスキーマを確認
- `tool` でツールの内部状態を読み取り(ダウンキャスト必要)
- `call` の引数を改変してツールに渡す
## 使用例
### 手動実装
```rust
struct Counter { count: AtomicUsize }
impl Tool for Counter {
async fn execute(&self, _: &str) -> Result<String, ToolError> {
let n = self.count.fetch_add(1, Ordering::SeqCst);
Ok(format!("count: {}", n))
}
}
let def: ToolDefinition = Arc::new(|| {
let meta = ToolMeta::new("counter")
.description("カウンターを増加")
.input_schema(json!({"type": "object"}));
(meta, Arc::new(Counter { count: AtomicUsize::new(0) }))
});
worker.register_tool(def)?;
```
### マクロ使用(推奨)
```rust
#[tool_registry]
impl App {
#[tool]
async fn greet(&self, name: String) -> String {
format!("Hello, {}!", name)
}
}
let app = App;
worker.register_tool(app.greet_definition())?;
```
## 設計上の決定
| 問題 | 決定 | 理由 |
| -------------------- | ------------------------------ | ---------------------------------------------- |
| メタ情報の変更可能性 | ToolMeta を分離・不変化 | 登録後の整合性を保証 |
| 状態管理 | 登録時にインスタンス生成 | セッション中の状態保持、同一インスタンス再利用 |
| Factory vs Instance | Factory + 登録時即時呼び出し | コンテキストキャプチャと登録時検証 |
| Hook からのアクセス | Context に meta と tool を含む | 柔軟な介入を可能に |
+58 -94
View File
@@ -33,46 +33,39 @@ Workerは以下のループ(ターン)を実行します。
1. **Start Turn**: `Worker::run(messages)` 呼び出し 1. **Start Turn**: `Worker::run(messages)` 呼び出し
2. **Hook: OnMessageSend**: 2. **Hook: OnMessageSend**:
- ユーザーメッセージの改変、バリデーション、キャンセルが可能。 * ユーザーメッセージの改変、バリデーション、キャンセルが可能。
- コンテキストへのシステムプロンプト注入などもここで行う想定。 * コンテキストへのシステムプロンプト注入などもここで行う想定。
3. **Request & Stream**: 3. **Request & Stream**:
- LLMへリクエスト送信。イベントストリーム開始。 * LLMへリクエスト送信。イベントストリーム開始。
- `Timeline`によるイベント処理。 * `Timeline`によるイベント処理。
4. **Tool Handling (Parallel)**: 4. **Tool Handling (Parallel)**:
- レスポンス内に含まれる全てのTool Callを収集。 * レスポンス内に含まれる全てのTool Callを収集。
- 各Toolに対して **Hook: BeforeToolCall** を実行(実行可否、引数改変)。 * 各Toolに対して **Hook: BeforeToolCall** を実行(実行可否、引数改変)。
- 許可されたToolを**並列実行 (`join_all`)**。 * 許可されたToolを**並列実行 (`join_all`)**。
- 各Tool実行後に **Hook: AfterToolCall** を実行(結果の確認、加工)。 * 各Tool実行後に **Hook: AfterToolCall** を実行(結果の確認、加工)。
5. **Next Request Decision**: 5. **Next Request Decision**:
- Tool実行結果がある場合 -> 結果をMessageとしてContextに追加し、**Step * Tool実行結果がある場合 -> 結果をMessageとしてContextに追加し、**Step 3へ戻る** (自動ループ)。
3へ戻る** (自動ループ) * Tool実行がない場合 -> Step 6へ
- Tool実行がない場合 -> Step 6へ。
6. **Hook: OnTurnEnd**: 6. **Hook: OnTurnEnd**:
- 最終的な応答に対するチェック(Lint/Fmt)。 * 最終的な応答に対するチェック(Lint/Fmt)。
- エラーがある場合、エラーメッセージをContextに追加して **Step 3へ戻る** * エラーがある場合、エラーメッセージをContextに追加して **Step 3へ戻る** ことで自己修正を促せる。
ことで自己修正を促せる * 問題なければターン終了
- 問題なければターン終了。
## Tool 設計 ## Tool 設計
### アーキテクチャ概要 ### アーキテクチャ概要
Rustの静的型付けシステムとLLMの動的なツール呼び出し(文字列による指定)を、**Trait Rustの静的型付けシステムとLLMの動的なツール呼び出し(文字列による指定)を、**Trait Object** と **動的ディスパッチ** を用いて接続します。
Object** と **動的ディスパッチ** を用いて接続します。
1. **共通インターフェース (`Tool` Trait)**: 1. **共通インターフェース (`Tool` Trait)**: 全てのツールが実装すべき共通の振る舞い(メタデータ取得と実行)を定義します。
全てのツールが実装すべき共通の振る舞い(メタデータ取得と実行)を定義します。 2. **ラッパー生成 (`#[tool]` Macro)**: ユーザー定義のメソッドをラップし、`Tool` Traitを実装した構造体を自動生成します。
2. **ラッパー生成 (`#[tool]` Macro)**: ユーザー定義のメソッドをラップし、`Tool` 3. **レジストリ (`HashMap`)**: Workerは動的ディスパッチ用に `HashMap<String, Box<dyn Tool>>` でツールを管理します。
Traitを実装した構造体を自動生成します。
3. **レジストリ (`HashMap`)**: Workerは動的ディスパッチ用に
`HashMap<String, Box<dyn Tool>>` でツールを管理します。
この仕組みにより、「名前からツールを探し、JSON引数を型変換して関数を実行する」フローを安全に実現します。 この仕組みにより、「名前からツールを探し、JSON引数を型変換して関数を実行する」フローを安全に実現します。
### 1. Tool Trait 定義 ### 1. Tool Trait 定義
ツールが最低限持つべきインターフェースです。`Send + Sync` ツールが最低限持つべきインターフェースです。`Send + Sync` を必須とし、マルチスレッド(並列実行)に対応します。
を必須とし、マルチスレッド(並列実行)に対応します。
```rust ```rust
#[async_trait] #[async_trait]
@@ -120,8 +113,7 @@ impl MyApp {
**マクロ展開後のイメージ (擬似コード):** **マクロ展開後のイメージ (擬似コード):**
マクロは、元のメソッドに対応する**ラッパー構造体**を生成します。このラッパーが マクロは、元のメソッドに対応する**ラッパー構造体**を生成します。このラッパーが `Tool` Trait を実装します。
`Tool` Trait を実装します。
```rust ```rust
// 1. 引数をデシリアライズ用の中間構造体に変換 // 1. 引数をデシリアライズ用の中間構造体に変換
@@ -163,18 +155,15 @@ impl Tool for GetUserTool {
### 3. Workerによる実行フロー ### 3. Workerによる実行フロー
Workerは生成されたラッパー構造体を `Box<dyn Tool>` Workerは生成されたラッパー構造体を `Box<dyn Tool>` として保持し、以下のフローで実行します。
として保持し、以下のフローで実行します。
1. **登録**: 1. **登録**: アプリケーション開始時、コンテキスト(`MyApp`)から各ツールのラッパー(`GetUserTool`)を生成し、WorkerのMapに登録。
アプリケーション開始時、コンテキスト(`MyApp`)から各ツールのラッパー(`GetUserTool`)を生成し、WorkerのMapに登録 2. **解決**: LLMからのレスポンスに含まれる `ToolUse { name: "get_user", ... }` を受け取る
2. **解決**: LLMからのレスポンスに含まれる `ToolUse { name: "get_user", ... }`
を受け取る。
3. **検索**: `name` をキーに Map から `Box<dyn Tool>` を取得。 3. **検索**: `name` をキーに Map から `Box<dyn Tool>` を取得。
4. **実行**: 4. **実行**:
- `tool.execute(json)` を呼び出す。 * `tool.execute(json)` を呼び出す。
- 内部で `serde_json` による型変換とメソッド実行が行われる。 * 内部で `serde_json` による型変換とメソッド実行が行われる。
- 結果が返る。 * 結果が返る。
これにより、型安全性を保ちつつ、動的なツール実行が可能になります。 これにより、型安全性を保ちつつ、動的なツール実行が可能になります。
@@ -182,88 +171,64 @@ Workerは生成されたラッパー構造体を `Box<dyn Tool>`
### コンセプト ### コンセプト
- **制御の介入**: * **制御の介入**: ターンの進行、メッセージの内容、ツールの実行に対して介入します。
ターンの進行、メッセージの内容、ツールの実行に対して介入します。 * **Contextへのアクセス**: メッセージ履歴(Context)を読み書きできます。
- **Contextへのアクセス**: メッセージ履歴(Context)を読み書きできます。
### Hook Trait ### Hook Trait
```rust ```rust
#[async_trait] #[async_trait]
pub trait Hook<E: HookEventKind>: Send + Sync { pub trait WorkerHook: Send + Sync {
async fn call(&self, input: &mut E::Input) -> Result<E::Output, Error>; /// メッセージ送信前。
/// リクエストに含まれるメッセージリストを改変できる。
async fn on_message_send(&self, context: &mut Vec<Message>) -> Result<ControlFlow, Error> {
Ok(ControlFlow::Continue)
} }
pub trait HookEventKind { /// ツール実行前。
type Input; /// 実行をキャンセルしたり、引数を書き換えることができる。
type Output; async fn before_tool_call(&self, tool_call: &mut ToolCall) -> Result<ControlFlow, Error> {
Ok(ControlFlow::Continue)
} }
pub struct OnMessageSend; /// ツール実行後。
pub struct BeforeToolCall; /// 結果を書き換えたり、隠蔽したりできる。
pub struct AfterToolCall; async fn after_tool_call(&self, tool_result: &mut ToolResult) -> Result<ControlFlow, Error> {
pub struct OnTurnEnd; Ok(ControlFlow::Continue)
pub struct OnAbort;
pub enum OnMessageSendResult {
Continue,
Cancel(String),
} }
pub enum BeforeToolCallResult { /// ターン終了時。
/// 生成されたメッセージを検査し、必要ならリトライ(ContinueWithMessages)を指示できる。
async fn on_turn_end(&self, messages: &[Message]) -> Result<TurnResult, Error> {
Ok(TurnResult::Finish)
}
}
pub enum ControlFlow {
Continue, Continue,
Skip, // Tool実行などをスキップ Skip, // Tool実行などをスキップ
Abort(String), // 処理中断 Abort(String), // 処理中断
Pause,
} }
pub enum AfterToolCallResult { pub enum TurnResult {
Continue,
Abort(String),
}
pub enum OnTurnEndResult {
Finish, Finish,
ContinueWithMessages(Vec<Message>), // メッセージを追加してターン継続(自己修正など) ContinueWithMessages(Vec<Message>), // メッセージを追加してターン継続(自己修正など)
Paused,
}
```
### Tool Call Context
`before_tool_call` / `after_tool_call`
は、ツール実行の文脈を含む入力を受け取る。
```rust
pub struct ToolCallContext {
pub call: ToolCall,
pub meta: ToolMeta, // 不変メタデータ
pub tool: Arc<dyn Tool>, // 状態アクセス用
}
pub struct ToolResultContext {
pub result: ToolResult,
pub meta: ToolMeta,
pub tool: Arc<dyn Tool>,
} }
``` ```
## 実装方針 ## 実装方針
1. **Worker Struct**: 1. **Worker Struct**:
- `Timeline`を所有。 * `Timeline`を所有。
- `Handler`として「ToolCallCollector」をTimelineに登録。 * `Handler`として「ToolCallCollector」をTimelineに登録。
- `stream`終了後に収集したToolCallを処理するロジックを持つ。 * `stream`終了後に収集したToolCallを処理するロジックを持つ。
- **履歴管理**: `set_history`, `with_messages`, `history_mut`
等を通じて、会話履歴の注入や編集を可能にする。
2. **Tool Executor Handler**: 2. **Tool Executor Handler**:
- Timeline上ではツール実行を行わず、あくまで「ToolCallブロックの収集」に徹する(Toolの実行は非同期かつ並列で、ストリーム終了後あるいはブロック確定後に行うため)。 * Timeline上ではツール実行を行わず、あくまで「ToolCallブロックの収集」に徹する(Toolの実行は非同期かつ並列で、ストリーム終了後あるいはブロック確定後に行うため)。
- ただし、リアルタイム性を重視する場合(ストリーミング中にToolを実行開始等)は将来的な拡張とするが、現状は「結果が揃うのを待って」という要件に従い、収集フェーズと実行フェーズを分ける。 * ただし、リアルタイム性を重視する場合(ストリーミング中にToolを実行開始等)は将来的な拡張とするが、現状は「結果が揃うのを待って」という要件に従い、収集フェーズと実行フェーズを分ける。
3. **worker-macros**: 3. **worker-macros**:
- `syn`, `quote` を用いて、関数定義から `Tool` トレイト実装と `InputSchema` * `syn`, `quote` を用いて、関数定義から `Tool` トレイト実装と `InputSchema` (schemars利用) を生成。
(schemars利用) を生成。
## Worker Event API 設計 ## Worker Event API 設計
@@ -272,7 +237,6 @@ pub struct ToolResultContext {
Workerは内部でイベントを処理し結果を返しますが、UIへのストリーミング表示やリアルタイムフィードバックには、イベントを外部に公開する仕組みが必要です。 Workerは内部でイベントを処理し結果を返しますが、UIへのストリーミング表示やリアルタイムフィードバックには、イベントを外部に公開する仕組みが必要です。
**要件**: **要件**:
1. テキストデルタをリアルタイムでUIに表示 1. テキストデルタをリアルタイムでUIに表示
2. ツール呼び出しの進行状況を表示 2. ツール呼び出しの進行状況を表示
3. ブロック完了時に累積結果を受け取る 3. ブロック完了時に累積結果を受け取る
@@ -282,7 +246,7 @@ Workerは内部でイベントを処理し結果を返しますが、UIへのス
Worker APIは **Timeline層のHandler機構の薄いラッパー** として設計します。 Worker APIは **Timeline層のHandler機構の薄いラッパー** として設計します。
| 層 | 目的 | 提供するもの | | 層 | 目的 | 提供するもの |
| ------------------------ | ------------------ | ---------------------------------- | |---|------|-------------|
| **Handler (Timeline層)** | 内部実装、役割分離 | スコープ管理 + Deltaイベント | | **Handler (Timeline層)** | 内部実装、役割分離 | スコープ管理 + Deltaイベント |
| **Worker Event API** | ユーザー向け利便性 | Handler露出 + Completeイベント追加 | | **Worker Event API** | ユーザー向け利便性 | Handler露出 + Completeイベント追加 |
@@ -465,8 +429,8 @@ impl<C: LlmClient> Worker<C> {
### 設計上のポイント ### 設計上のポイント
1. **Handlerの再利用**: 既存のHandler traitをそのまま活用 1. **Handlerの再利用**: 既存のHandler traitをそのまま活用
2. **スコープ管理の維持**: 2. **スコープ管理の維持**: ブロックイベントはStart→Delta→Endのライフサイクルを保持
ブロックイベントはStart→Delta→Endのライフサイクルを保持
3. **選択的購読**: on_*で必要なイベントだけ、またはSubscriberで一括 3. **選択的購読**: on_*で必要なイベントだけ、またはSubscriberで一括
4. **累積イベントの追加**: Worker層でComplete系イベントを追加提供 4. **累積イベントの追加**: Worker層でComplete系イベントを追加提供
5. **後方互換性**: 従来の`run()`も引き続き使用可能 5. **後方互換性**: 従来の`run()`も引き続き使用可能
Generated
+3 -3
View File
@@ -35,11 +35,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1771369470, "lastModified": 1767116409,
"narHash": "sha256-0NBlEBKkN3lufyvFegY4TYv5mCNHbi5OmBDrzihbBMQ=", "narHash": "sha256-5vKw92l1GyTnjoLzEagJy5V5mDFck72LiQWZSOnSicw=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "0182a361324364ae3f436a63005877674cf45efb", "rev": "cad22e7d996aea55ecab064e84834289143e44a0",
"type": "github" "type": "github"
}, },
"original": { "original": {
-16
View File
@@ -1,16 +0,0 @@
[package]
name = "llm-worker-macros"
description = "llm-worker's proc macros"
version = "0.2.0"
publish.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
[lib]
proc-macro = true
[dependencies]
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full"] }
-29
View File
@@ -1,29 +0,0 @@
[package]
name = "llm-worker"
description = "A library for building autonomous LLM-powered systems"
version = "0.2.1"
publish.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "2.0"
tracing = "0.1"
async-trait = "0.1"
futures = "0.3"
tokio = { version = "1.49", features = ["macros", "rt-multi-thread"] }
tokio-util = "0.7"
reqwest = { version = "0.13.1", default-features = false, features = ["stream", "json", "native-tls", "http2"] }
eventsource-stream = "0.2"
llm-worker-macros = { path = "../llm-worker-macros", version = "0.2" }
[dev-dependencies]
clap = { version = "4.5", features = ["derive", "env"] }
schemars = "1.2"
tempfile = "3.24"
dotenv = "0.15"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
trybuild = "1.0.116"
-71
View File
@@ -1,71 +0,0 @@
//! Worker cancellation demo
//!
//! Example of cancelling from another thread during streaming
use llm_worker::llm_client::providers::anthropic::AnthropicClient;
use llm_worker::{Worker, WorkerResult};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load .env file
dotenv::dotenv().ok();
// Initialize logging
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
)
.init();
let api_key =
std::env::var("ANTHROPIC_API_KEY").expect("ANTHROPIC_API_KEY environment variable not set");
let client = AnthropicClient::new(&api_key, "claude-sonnet-4-20250514");
let worker = Arc::new(Mutex::new(Worker::new(client)));
println!("🚀 Starting Worker...");
println!("💡 Will cancel after 2 seconds\n");
// Get cancel sender first (without holding lock)
let cancel_tx = {
let w = worker.lock().await;
w.cancel_sender()
};
// Task 1: Run Worker
let worker_clone = worker.clone();
let task = tokio::spawn(async move {
let mut w = worker_clone.lock().await;
println!("📡 Sending request to LLM...");
match w.run("Tell me a very long story about a brave knight. Make it as detailed as possible with many paragraphs.").await {
Ok(WorkerResult::Finished) => {
println!("✅ Task completed normally");
}
Ok(WorkerResult::Paused) => {
println!("⏸️ Task paused");
}
Err(e) => {
println!("❌ Task error: {}", e);
}
}
});
// Task 2: Cancel after 2 seconds
tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(2)).await;
println!("\n🛑 Cancelling worker...");
let _ = cancel_tx.send(()).await;
});
// Wait for task completion
task.await?;
println!("\n✨ Demo complete!");
Ok(())
}
-446
View File
@@ -1,446 +0,0 @@
//! Public event types for Worker layer
//!
//! Event representation exposed to external users.
use serde::{Deserialize, Serialize};
// =============================================================================
// Core Event Types (from llm_client layer)
// =============================================================================
/// Streaming events from LLM
///
/// Responses from each LLM provider are processed uniformly
/// as a stream of `Event`.
///
/// # Event Types
///
/// - **Meta events**: `Ping`, `Usage`, `Status`, `Error`
/// - **Block events**: `BlockStart`, `BlockDelta`, `BlockStop`, `BlockAbort`
///
/// # Block Lifecycle
///
/// Text and tool calls have events in the order of
/// `BlockStart` → `BlockDelta`(multiple) → `BlockStop`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Event {
/// Heartbeat
Ping(PingEvent),
/// Token usage
Usage(UsageEvent),
/// Stream status change
Status(StatusEvent),
/// Error occurred
Error(ErrorEvent),
/// Block start (text, tool use, etc.)
BlockStart(BlockStart),
/// Block delta data
BlockDelta(BlockDelta),
/// Block normal end
BlockStop(BlockStop),
/// Block abort
BlockAbort(BlockAbort),
}
// =============================================================================
// Meta Events
// =============================================================================
/// Ping event (heartbeat)
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct PingEvent {
pub timestamp: Option<u64>,
}
/// Usage event
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct UsageEvent {
/// Input token count
pub input_tokens: Option<u64>,
/// Output token count
pub output_tokens: Option<u64>,
/// Total token count
pub total_tokens: Option<u64>,
/// Cache read token count
pub cache_read_input_tokens: Option<u64>,
/// Cache creation token count
pub cache_creation_input_tokens: Option<u64>,
}
/// Status event
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StatusEvent {
pub status: ResponseStatus,
}
/// Response status
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ResponseStatus {
/// Stream started
Started,
/// Completed normally
Completed,
/// Cancelled
Cancelled,
/// Error occurred
Failed,
}
/// Error event
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ErrorEvent {
pub code: Option<String>,
pub message: String,
}
// =============================================================================
// Block Types
// =============================================================================
/// Block type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BlockType {
/// Text generation
Text,
/// Thinking (Claude Extended Thinking, etc.)
Thinking,
/// Tool call
ToolUse,
/// Tool result
ToolResult,
}
/// Block start event
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BlockStart {
/// Block index
pub index: usize,
/// Block type
pub block_type: BlockType,
/// Block-specific metadata
pub metadata: BlockMetadata,
}
impl BlockStart {
pub fn block_type(&self) -> BlockType {
self.block_type
}
}
/// Block metadata
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BlockMetadata {
Text,
Thinking,
ToolUse { id: String, name: String },
ToolResult { tool_use_id: String },
}
/// Block delta event
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BlockDelta {
/// Block index
pub index: usize,
/// Delta content
pub delta: DeltaContent,
}
/// Delta content
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum DeltaContent {
/// Text delta
Text(String),
/// Thinking delta
Thinking(String),
/// JSON substring of tool arguments
InputJson(String),
}
impl DeltaContent {
/// Get block type of the delta
pub fn block_type(&self) -> BlockType {
match self {
DeltaContent::Text(_) => BlockType::Text,
DeltaContent::Thinking(_) => BlockType::Thinking,
DeltaContent::InputJson(_) => BlockType::ToolUse,
}
}
}
/// Block stop event
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BlockStop {
/// Block index
pub index: usize,
/// Block type
pub block_type: BlockType,
/// Stop reason
pub stop_reason: Option<StopReason>,
}
impl BlockStop {
pub fn block_type(&self) -> BlockType {
self.block_type
}
}
/// Block abort event
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BlockAbort {
/// Block index
pub index: usize,
/// Block type
pub block_type: BlockType,
/// Abort reason
pub reason: String,
}
impl BlockAbort {
pub fn block_type(&self) -> BlockType {
self.block_type
}
}
/// Stop reason
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum StopReason {
/// Natural end
EndTurn,
/// Max tokens reached
MaxTokens,
/// Stop sequence reached
StopSequence,
/// Tool use
ToolUse,
}
// =============================================================================
// Builder / Factory helpers
// =============================================================================
impl Event {
/// Create text block start event
pub fn text_block_start(index: usize) -> Self {
Event::BlockStart(BlockStart {
index,
block_type: BlockType::Text,
metadata: BlockMetadata::Text,
})
}
/// Create text delta event
pub fn text_delta(index: usize, text: impl Into<String>) -> Self {
Event::BlockDelta(BlockDelta {
index,
delta: DeltaContent::Text(text.into()),
})
}
/// Create text block stop event
pub fn text_block_stop(index: usize, stop_reason: Option<StopReason>) -> Self {
Event::BlockStop(BlockStop {
index,
block_type: BlockType::Text,
stop_reason,
})
}
/// Create tool use block start event
pub fn tool_use_start(index: usize, id: impl Into<String>, name: impl Into<String>) -> Self {
Event::BlockStart(BlockStart {
index,
block_type: BlockType::ToolUse,
metadata: BlockMetadata::ToolUse {
id: id.into(),
name: name.into(),
},
})
}
/// Create tool input delta event
pub fn tool_input_delta(index: usize, json: impl Into<String>) -> Self {
Event::BlockDelta(BlockDelta {
index,
delta: DeltaContent::InputJson(json.into()),
})
}
/// Create tool use block stop event
pub fn tool_use_stop(index: usize) -> Self {
Event::BlockStop(BlockStop {
index,
block_type: BlockType::ToolUse,
stop_reason: Some(StopReason::ToolUse),
})
}
/// Create usage event
pub fn usage(input_tokens: u64, output_tokens: u64) -> Self {
Event::Usage(UsageEvent {
input_tokens: Some(input_tokens),
output_tokens: Some(output_tokens),
total_tokens: Some(input_tokens + output_tokens),
cache_read_input_tokens: None,
cache_creation_input_tokens: None,
})
}
/// Create ping event
pub fn ping() -> Self {
Event::Ping(PingEvent { timestamp: None })
}
}
// =============================================================================
// Conversions: timeline::event -> worker::event
// =============================================================================
impl From<crate::timeline::event::ResponseStatus> for ResponseStatus {
fn from(value: crate::timeline::event::ResponseStatus) -> Self {
match value {
crate::timeline::event::ResponseStatus::Started => ResponseStatus::Started,
crate::timeline::event::ResponseStatus::Completed => ResponseStatus::Completed,
crate::timeline::event::ResponseStatus::Cancelled => ResponseStatus::Cancelled,
crate::timeline::event::ResponseStatus::Failed => ResponseStatus::Failed,
}
}
}
impl From<crate::timeline::event::BlockType> for BlockType {
fn from(value: crate::timeline::event::BlockType) -> Self {
match value {
crate::timeline::event::BlockType::Text => BlockType::Text,
crate::timeline::event::BlockType::Thinking => BlockType::Thinking,
crate::timeline::event::BlockType::ToolUse => BlockType::ToolUse,
crate::timeline::event::BlockType::ToolResult => BlockType::ToolResult,
}
}
}
impl From<crate::timeline::event::BlockMetadata> for BlockMetadata {
fn from(value: crate::timeline::event::BlockMetadata) -> Self {
match value {
crate::timeline::event::BlockMetadata::Text => BlockMetadata::Text,
crate::timeline::event::BlockMetadata::Thinking => BlockMetadata::Thinking,
crate::timeline::event::BlockMetadata::ToolUse { id, name } => {
BlockMetadata::ToolUse { id, name }
}
crate::timeline::event::BlockMetadata::ToolResult { tool_use_id } => {
BlockMetadata::ToolResult { tool_use_id }
}
}
}
}
impl From<crate::timeline::event::DeltaContent> for DeltaContent {
fn from(value: crate::timeline::event::DeltaContent) -> Self {
match value {
crate::timeline::event::DeltaContent::Text(text) => DeltaContent::Text(text),
crate::timeline::event::DeltaContent::Thinking(text) => DeltaContent::Thinking(text),
crate::timeline::event::DeltaContent::InputJson(json) => DeltaContent::InputJson(json),
}
}
}
impl From<crate::timeline::event::StopReason> for StopReason {
fn from(value: crate::timeline::event::StopReason) -> Self {
match value {
crate::timeline::event::StopReason::EndTurn => StopReason::EndTurn,
crate::timeline::event::StopReason::MaxTokens => StopReason::MaxTokens,
crate::timeline::event::StopReason::StopSequence => StopReason::StopSequence,
crate::timeline::event::StopReason::ToolUse => StopReason::ToolUse,
}
}
}
impl From<crate::timeline::event::PingEvent> for PingEvent {
fn from(value: crate::timeline::event::PingEvent) -> Self {
PingEvent {
timestamp: value.timestamp,
}
}
}
impl From<crate::timeline::event::UsageEvent> for UsageEvent {
fn from(value: crate::timeline::event::UsageEvent) -> Self {
UsageEvent {
input_tokens: value.input_tokens,
output_tokens: value.output_tokens,
total_tokens: value.total_tokens,
cache_read_input_tokens: value.cache_read_input_tokens,
cache_creation_input_tokens: value.cache_creation_input_tokens,
}
}
}
impl From<crate::timeline::event::StatusEvent> for StatusEvent {
fn from(value: crate::timeline::event::StatusEvent) -> Self {
StatusEvent {
status: value.status.into(),
}
}
}
impl From<crate::timeline::event::ErrorEvent> for ErrorEvent {
fn from(value: crate::timeline::event::ErrorEvent) -> Self {
ErrorEvent {
code: value.code,
message: value.message,
}
}
}
impl From<crate::timeline::event::BlockStart> for BlockStart {
fn from(value: crate::timeline::event::BlockStart) -> Self {
BlockStart {
index: value.index,
block_type: value.block_type.into(),
metadata: value.metadata.into(),
}
}
}
impl From<crate::timeline::event::BlockDelta> for BlockDelta {
fn from(value: crate::timeline::event::BlockDelta) -> Self {
BlockDelta {
index: value.index,
delta: value.delta.into(),
}
}
}
impl From<crate::timeline::event::BlockStop> for BlockStop {
fn from(value: crate::timeline::event::BlockStop) -> Self {
BlockStop {
index: value.index,
block_type: value.block_type.into(),
stop_reason: value.stop_reason.map(Into::into),
}
}
}
impl From<crate::timeline::event::BlockAbort> for BlockAbort {
fn from(value: crate::timeline::event::BlockAbort) -> Self {
BlockAbort {
index: value.index,
block_type: value.block_type.into(),
reason: value.reason,
}
}
}
impl From<crate::timeline::event::Event> for Event {
fn from(value: crate::timeline::event::Event) -> Self {
match value {
crate::timeline::event::Event::Ping(p) => Event::Ping(p.into()),
crate::timeline::event::Event::Usage(u) => Event::Usage(u.into()),
crate::timeline::event::Event::Status(s) => Event::Status(s.into()),
crate::timeline::event::Event::Error(e) => Event::Error(e.into()),
crate::timeline::event::Event::BlockStart(s) => Event::BlockStart(s.into()),
crate::timeline::event::Event::BlockDelta(d) => Event::BlockDelta(d.into()),
crate::timeline::event::Event::BlockStop(s) => Event::BlockStop(s.into()),
crate::timeline::event::Event::BlockAbort(a) => Event::BlockAbort(a.into()),
}
}
}
-233
View File
@@ -1,233 +0,0 @@
//! Hook-related type definitions
//!
//! Types used for turn control and intervention in the Worker layer
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;
// =============================================================================
// Hook Event Kinds
// =============================================================================
pub trait HookEventKind: Send + Sync + 'static {
type Input;
type Output;
}
pub struct OnPromptSubmit;
pub struct PreLlmRequest;
pub struct PreToolCall;
pub struct PostToolCall;
pub struct OnTurnEnd;
pub struct OnAbort;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OnPromptSubmitResult {
Continue,
Cancel(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PreLlmRequestResult {
Continue,
Cancel(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PreToolCallResult {
Continue,
Skip,
Abort(String),
Pause,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PostToolCallResult {
Continue,
Abort(String),
}
#[derive(Debug, Clone)]
pub enum OnTurnEndResult {
Finish,
ContinueWithMessages(Vec<crate::Item>),
Paused,
}
use std::sync::Arc;
use crate::tool::{Tool, ToolMeta};
/// Input context for PreToolCall
pub struct ToolCallContext {
/// Tool call information (modifiable)
pub call: ToolCall,
/// Tool meta information (immutable)
pub meta: ToolMeta,
/// Tool instance (for state access)
pub tool: Arc<dyn Tool>,
}
/// Input context for PostToolCall
pub struct PostToolCallContext {
/// Tool call information
pub call: ToolCall,
/// Tool execution result (modifiable)
pub result: ToolResult,
/// Tool meta information (immutable)
pub meta: ToolMeta,
/// Tool instance (for state access)
pub tool: Arc<dyn Tool>,
}
impl HookEventKind for OnPromptSubmit {
type Input = crate::Item;
type Output = OnPromptSubmitResult;
}
impl HookEventKind for PreLlmRequest {
type Input = Vec<crate::Item>;
type Output = PreLlmRequestResult;
}
impl HookEventKind for PreToolCall {
type Input = ToolCallContext;
type Output = PreToolCallResult;
}
impl HookEventKind for PostToolCall {
type Input = PostToolCallContext;
type Output = PostToolCallResult;
}
impl HookEventKind for OnTurnEnd {
type Input = Vec<crate::Item>;
type Output = OnTurnEndResult;
}
impl HookEventKind for OnAbort {
type Input = String;
type Output = ();
}
// =============================================================================
// Tool Call / Result Types
// =============================================================================
/// Tool call information
///
/// Represents a ToolUse block from LLM, modifiable in Hook processing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
/// Tool call ID (used for linking with response)
pub id: String,
/// Tool name
pub name: String,
/// Input arguments (JSON)
pub input: Value,
}
/// Tool execution result
///
/// Represents the result after tool execution, modifiable in Hook processing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResult {
/// Corresponding tool call ID
pub tool_use_id: String,
/// Result content
pub content: String,
/// Whether this is an error
#[serde(default)]
pub is_error: bool,
}
impl ToolResult {
/// Create a success result
pub fn success(tool_use_id: impl Into<String>, content: impl Into<String>) -> Self {
Self {
tool_use_id: tool_use_id.into(),
content: content.into(),
is_error: false,
}
}
/// Create an error result
pub fn error(tool_use_id: impl Into<String>, content: impl Into<String>) -> Self {
Self {
tool_use_id: tool_use_id.into(),
content: content.into(),
is_error: true,
}
}
}
// =============================================================================
// Hook Error
// =============================================================================
/// Hook error
#[derive(Debug, Error)]
pub enum HookError {
/// Processing was aborted
#[error("Aborted: {0}")]
Aborted(String),
/// Internal error
#[error("Hook error: {0}")]
Internal(String),
}
// =============================================================================
// Hook Trait
// =============================================================================
/// Trait for handling Hook events
///
/// Each event type has a different return type, constrained via `HookEventKind`.
#[async_trait]
pub trait Hook<E: HookEventKind>: Send + Sync {
async fn call(&self, input: &mut E::Input) -> Result<E::Output, HookError>;
}
// =============================================================================
// Hook Registry
// =============================================================================
/// Registry holding all Hooks
///
/// Used internally by Worker to manage all Hook types.
pub struct HookRegistry {
/// on_prompt_submit Hook
pub(crate) on_prompt_submit: Vec<Box<dyn Hook<OnPromptSubmit>>>,
/// pre_llm_request Hook
pub(crate) pre_llm_request: Vec<Box<dyn Hook<PreLlmRequest>>>,
/// pre_tool_call Hook
pub(crate) pre_tool_call: Vec<Box<dyn Hook<PreToolCall>>>,
/// post_tool_call Hook
pub(crate) post_tool_call: Vec<Box<dyn Hook<PostToolCall>>>,
/// on_turn_end Hook
pub(crate) on_turn_end: Vec<Box<dyn Hook<OnTurnEnd>>>,
/// on_abort Hook
pub(crate) on_abort: Vec<Box<dyn Hook<OnAbort>>>,
}
impl Default for HookRegistry {
fn default() -> Self {
Self::new()
}
}
impl HookRegistry {
/// Create an empty HookRegistry
pub fn new() -> Self {
Self {
on_prompt_submit: Vec::new(),
pre_llm_request: Vec::new(),
pre_tool_call: Vec::new(),
post_tool_call: Vec::new(),
on_turn_end: Vec::new(),
on_abort: Vec::new(),
}
}
}
-52
View File
@@ -1,52 +0,0 @@
//! llm-worker - LLM Worker Library
//!
//! Provides components for managing interactions with LLMs.
//!
//! # Main Components
//!
//! - [`Worker`] - Central component for managing LLM interactions
//! - [`tool::Tool`] - Tools that can be invoked by the LLM
//! - [`hook::Hook`] - Hooks for intercepting turn progression
//! - [`subscriber::WorkerSubscriber`] - Subscribing to streaming events
//!
//! # Quick Start
//!
//! ```ignore
//! use llm_worker::{Worker, Item};
//!
//! // Create a Worker
//! let mut worker = Worker::new(client)
//! .system_prompt("You are a helpful assistant.");
//!
//! // Register tools (optional)
//! // worker.register_tool(my_tool_definition)?;
//!
//! // Run the interaction
//! let history = worker.run("Hello!").await?;
//! ```
//!
//! # Cache Protection
//!
//! To maximize KV cache hit rate, transition to the locked state
//! with [`Worker::lock()`] before execution.
//!
//! ```ignore
//! let mut locked = worker.lock();
//! locked.run("user input").await?;
//! ```
mod handler;
mod message;
mod worker;
pub mod event;
pub mod hook;
pub mod llm_client;
pub mod state;
pub mod subscriber;
pub mod timeline;
pub mod tool;
pub mod tool_server;
pub use message::{ContentPart, Item, Message, Role};
pub use worker::{ToolRegistryError, Worker, WorkerConfig, WorkerError, WorkerResult};
-86
View File
@@ -1,86 +0,0 @@
//! LLMクライアント共通trait定義
use std::pin::Pin;
use crate::llm_client::{ClientError, Request, RequestConfig, event::Event};
use async_trait::async_trait;
use futures::Stream;
/// 設定に関する警告
///
/// プロバイダがサポートしていない設定を使用した場合に返される。
#[derive(Debug, Clone)]
pub struct ConfigWarning {
/// 設定オプション名
pub option_name: &'static str,
/// 警告メッセージ
pub message: String,
}
impl ConfigWarning {
/// 新しい警告を作成
pub fn unsupported(option_name: &'static str, provider_name: &str) -> Self {
Self {
option_name,
message: format!(
"'{}' is not supported by {} and will be ignored",
option_name, provider_name
),
}
}
}
impl std::fmt::Display for ConfigWarning {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.option_name, self.message)
}
}
/// LLMクライアントのtrait
///
/// 各プロバイダはこのtraitを実装し、統一されたインターフェースを提供する。
#[async_trait]
pub trait LlmClient: Send + Sync {
/// ストリーミングリクエストを送信し、Eventストリームを返す
///
/// # Arguments
/// * `request` - リクエスト情報
///
/// # Returns
/// * `Ok(Stream)` - イベントストリーム
/// * `Err(ClientError)` - エラー
async fn stream(
&self,
request: Request,
) -> Result<Pin<Box<dyn Stream<Item = Result<Event, ClientError>> + Send>>, ClientError>;
/// 設定をバリデーションし、未サポートの設定があれば警告を返す
///
/// # Arguments
/// * `config` - バリデーション対象の設定
///
/// # Returns
/// サポートされていない設定に対する警告のリスト
fn validate_config(&self, config: &RequestConfig) -> Vec<ConfigWarning> {
// デフォルト実装: 全ての設定をサポート
let _ = config;
Vec::new()
}
}
/// `Box<dyn LlmClient>` に対する `LlmClient` の実装
///
/// これにより、動的ディスパッチを使用するクライアントも `Worker` で利用可能になる。
#[async_trait]
impl LlmClient for Box<dyn LlmClient> {
async fn stream(
&self,
request: Request,
) -> Result<Pin<Box<dyn Stream<Item = Result<Event, ClientError>> + Send>>, ClientError> {
(**self).stream(request).await
}
fn validate_config(&self, config: &RequestConfig) -> Vec<ConfigWarning> {
(**self).validate_config(config)
}
}
@@ -1,323 +0,0 @@
//! Anthropic Request Builder
//!
//! Converts Open Responses native Item model to Anthropic Messages API format.
use serde::Serialize;
use crate::llm_client::{
types::{ContentPart, Item, Role, ToolDefinition},
Request,
};
use super::AnthropicScheme;
/// Anthropic API request body
#[derive(Debug, Serialize)]
pub(crate) struct AnthropicRequest {
pub model: String,
pub max_tokens: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub system: Option<String>,
pub messages: Vec<AnthropicMessage>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub tools: Vec<AnthropicTool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_p: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_k: Option<u32>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub stop_sequences: Vec<String>,
pub stream: bool,
}
/// Anthropic message
#[derive(Debug, Serialize)]
pub(crate) struct AnthropicMessage {
pub role: String,
pub content: AnthropicContent,
}
/// Anthropic content
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub(crate) enum AnthropicContent {
Text(String),
Parts(Vec<AnthropicContentPart>),
}
/// Anthropic content part
#[derive(Debug, Serialize)]
#[serde(tag = "type")]
pub(crate) enum AnthropicContentPart {
#[serde(rename = "text")]
Text { text: String },
#[serde(rename = "tool_use")]
ToolUse {
id: String,
name: String,
input: serde_json::Value,
},
#[serde(rename = "tool_result")]
ToolResult { tool_use_id: String, content: String },
}
/// Anthropic tool definition
#[derive(Debug, Serialize)]
pub(crate) struct AnthropicTool {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub input_schema: serde_json::Value,
}
impl AnthropicScheme {
/// Build Anthropic request from Request
pub(crate) fn build_request(&self, model: &str, request: &Request) -> AnthropicRequest {
let messages = self.convert_items_to_messages(&request.items);
let tools = request.tools.iter().map(|t| self.convert_tool(t)).collect();
AnthropicRequest {
model: model.to_string(),
max_tokens: request.config.max_tokens.unwrap_or(4096),
system: request.system_prompt.clone(),
messages,
tools,
temperature: request.config.temperature,
top_p: request.config.top_p,
top_k: request.config.top_k,
stop_sequences: request.config.stop_sequences.clone(),
stream: true,
}
}
/// Convert Open Responses Items to Anthropic Messages
///
/// Anthropic uses a message-based model where:
/// - User messages have role "user"
/// - Assistant messages have role "assistant"
/// - Tool calls are content parts within assistant messages
/// - Tool results are content parts within user messages
fn convert_items_to_messages(&self, items: &[Item]) -> Vec<AnthropicMessage> {
let mut messages = Vec::new();
let mut pending_assistant_parts: Vec<AnthropicContentPart> = Vec::new();
let mut pending_user_parts: Vec<AnthropicContentPart> = Vec::new();
for item in items {
match item {
Item::Message { role, content, .. } => {
// Flush pending parts before a new message
self.flush_pending_parts(
&mut messages,
&mut pending_assistant_parts,
&mut pending_user_parts,
);
let anthropic_role = match role {
Role::User => "user",
Role::Assistant => "assistant",
Role::System => continue, // Skip system role items
};
let parts: Vec<AnthropicContentPart> = content
.iter()
.map(|p| match p {
ContentPart::InputText { text } => {
AnthropicContentPart::Text { text: text.clone() }
}
ContentPart::OutputText { text } => {
AnthropicContentPart::Text { text: text.clone() }
}
ContentPart::Refusal { refusal } => {
AnthropicContentPart::Text {
text: refusal.clone(),
}
}
})
.collect();
if parts.len() == 1 {
if let AnthropicContentPart::Text { text } = &parts[0] {
messages.push(AnthropicMessage {
role: anthropic_role.to_string(),
content: AnthropicContent::Text(text.clone()),
});
} else {
messages.push(AnthropicMessage {
role: anthropic_role.to_string(),
content: AnthropicContent::Parts(parts),
});
}
} else {
messages.push(AnthropicMessage {
role: anthropic_role.to_string(),
content: AnthropicContent::Parts(parts),
});
}
}
Item::FunctionCall {
call_id,
name,
arguments,
..
} => {
// Flush pending user parts first
if !pending_user_parts.is_empty() {
messages.push(AnthropicMessage {
role: "user".to_string(),
content: AnthropicContent::Parts(std::mem::take(
&mut pending_user_parts,
)),
});
}
// Parse arguments JSON string to Value
let input = serde_json::from_str(arguments)
.unwrap_or_else(|_| serde_json::Value::Object(serde_json::Map::new()));
pending_assistant_parts.push(AnthropicContentPart::ToolUse {
id: call_id.clone(),
name: name.clone(),
input,
});
}
Item::FunctionCallOutput { call_id, output, .. } => {
// Flush pending assistant parts first
if !pending_assistant_parts.is_empty() {
messages.push(AnthropicMessage {
role: "assistant".to_string(),
content: AnthropicContent::Parts(std::mem::take(
&mut pending_assistant_parts,
)),
});
}
pending_user_parts.push(AnthropicContentPart::ToolResult {
tool_use_id: call_id.clone(),
content: output.clone(),
});
}
Item::Reasoning { text, .. } => {
// Flush pending user parts first
if !pending_user_parts.is_empty() {
messages.push(AnthropicMessage {
role: "user".to_string(),
content: AnthropicContent::Parts(std::mem::take(
&mut pending_user_parts,
)),
});
}
// Reasoning is treated as assistant text in Anthropic
// (actual thinking blocks are handled differently in streaming)
pending_assistant_parts.push(AnthropicContentPart::Text { text: text.clone() });
}
}
}
// Flush remaining pending parts
self.flush_pending_parts(
&mut messages,
&mut pending_assistant_parts,
&mut pending_user_parts,
);
messages
}
fn flush_pending_parts(
&self,
messages: &mut Vec<AnthropicMessage>,
pending_assistant_parts: &mut Vec<AnthropicContentPart>,
pending_user_parts: &mut Vec<AnthropicContentPart>,
) {
if !pending_assistant_parts.is_empty() {
messages.push(AnthropicMessage {
role: "assistant".to_string(),
content: AnthropicContent::Parts(std::mem::take(pending_assistant_parts)),
});
}
if !pending_user_parts.is_empty() {
messages.push(AnthropicMessage {
role: "user".to_string(),
content: AnthropicContent::Parts(std::mem::take(pending_user_parts)),
});
}
}
fn convert_tool(&self, tool: &ToolDefinition) -> AnthropicTool {
AnthropicTool {
name: tool.name.clone(),
description: tool.description.clone(),
input_schema: tool.input_schema.clone(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_build_simple_request() {
let scheme = AnthropicScheme::new();
let request = Request::new()
.system("You are a helpful assistant.")
.user("Hello!");
let anthropic_req = scheme.build_request("claude-sonnet-4-20250514", &request);
assert_eq!(anthropic_req.model, "claude-sonnet-4-20250514");
assert_eq!(
anthropic_req.system,
Some("You are a helpful assistant.".to_string())
);
assert_eq!(anthropic_req.messages.len(), 1);
assert!(anthropic_req.stream);
}
#[test]
fn test_build_request_with_tool() {
let scheme = AnthropicScheme::new();
let request = Request::new().user("What's the weather?").tool(
ToolDefinition::new("get_weather")
.description("Get current weather")
.input_schema(serde_json::json!({
"type": "object",
"properties": {
"location": { "type": "string" }
},
"required": ["location"]
})),
);
let anthropic_req = scheme.build_request("claude-sonnet-4-20250514", &request);
assert_eq!(anthropic_req.tools.len(), 1);
assert_eq!(anthropic_req.tools[0].name, "get_weather");
}
#[test]
fn test_function_call_and_output() {
let scheme = AnthropicScheme::new();
let request = Request::new()
.user("What's the weather?")
.item(Item::function_call(
"call_123",
"get_weather",
r#"{"city":"Tokyo"}"#,
))
.item(Item::function_call_output("call_123", "Sunny, 25°C"));
let anthropic_req = scheme.build_request("claude-sonnet-4-20250514", &request);
assert_eq!(anthropic_req.messages.len(), 3);
assert_eq!(anthropic_req.messages[0].role, "user");
assert_eq!(anthropic_req.messages[1].role, "assistant");
assert_eq!(anthropic_req.messages[2].role, "user");
}
}
@@ -1,494 +0,0 @@
//! Open Responses Event Parser
//!
//! Parses SSE events from the Open Responses API into internal Event types.
use serde::Deserialize;
use crate::llm_client::{
event::{
BlockMetadata, BlockStart, BlockStop, DeltaContent, ErrorEvent, Event, ResponseStatus,
StatusEvent, StopReason, UsageEvent,
},
ClientError,
};
// =============================================================================
// Open Responses SSE Event Types
// =============================================================================
/// Response created event
#[derive(Debug, Deserialize)]
pub struct ResponseCreatedEvent {
pub response: ResponseObject,
}
/// Response object
#[derive(Debug, Deserialize)]
pub struct ResponseObject {
pub id: String,
pub status: String,
#[serde(default)]
pub output: Vec<OutputItem>,
pub usage: Option<UsageObject>,
}
/// Output item in response
#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum OutputItem {
Message {
id: String,
role: String,
#[serde(default)]
content: Vec<ContentPartObject>,
},
FunctionCall {
id: String,
call_id: String,
name: String,
arguments: String,
},
Reasoning {
id: String,
#[serde(default)]
text: String,
},
}
/// Content part object
#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentPartObject {
OutputText { text: String },
InputText { text: String },
Refusal { refusal: String },
}
/// Usage object
#[derive(Debug, Deserialize)]
pub struct UsageObject {
pub input_tokens: Option<u64>,
pub output_tokens: Option<u64>,
pub total_tokens: Option<u64>,
}
/// Output item added event
#[derive(Debug, Deserialize)]
pub struct OutputItemAddedEvent {
pub output_index: usize,
pub item: OutputItem,
}
/// Text delta event
#[derive(Debug, Deserialize)]
pub struct TextDeltaEvent {
pub output_index: usize,
pub content_index: usize,
pub delta: String,
}
/// Text done event
#[derive(Debug, Deserialize)]
pub struct TextDoneEvent {
pub output_index: usize,
pub content_index: usize,
pub text: String,
}
/// Function call arguments delta event
#[derive(Debug, Deserialize)]
pub struct FunctionCallArgumentsDeltaEvent {
pub output_index: usize,
pub call_id: String,
pub delta: String,
}
/// Function call arguments done event
#[derive(Debug, Deserialize)]
pub struct FunctionCallArgumentsDoneEvent {
pub output_index: usize,
pub call_id: String,
pub arguments: String,
}
/// Reasoning delta event
#[derive(Debug, Deserialize)]
pub struct ReasoningDeltaEvent {
pub output_index: usize,
pub delta: String,
}
/// Reasoning done event
#[derive(Debug, Deserialize)]
pub struct ReasoningDoneEvent {
pub output_index: usize,
pub text: String,
}
/// Content part done event
#[derive(Debug, Deserialize)]
pub struct ContentPartDoneEvent {
pub output_index: usize,
pub content_index: usize,
pub part: ContentPartObject,
}
/// Output item done event
#[derive(Debug, Deserialize)]
pub struct OutputItemDoneEvent {
pub output_index: usize,
pub item: OutputItem,
}
/// Response done event
#[derive(Debug, Deserialize)]
pub struct ResponseDoneEvent {
pub response: ResponseObject,
}
/// Error event from API
#[derive(Debug, Deserialize)]
pub struct ApiErrorEvent {
pub error: ApiError,
}
/// API error details
#[derive(Debug, Deserialize)]
pub struct ApiError {
pub code: Option<String>,
pub message: String,
}
// =============================================================================
// Event Parsing
// =============================================================================
/// Parse SSE event into internal Event(s)
///
/// Returns `Ok(None)` for events that should be ignored (e.g., heartbeats)
/// Returns `Ok(Some(vec))` for events that produce one or more internal Events
pub fn parse_event(event_type: &str, data: &str) -> Result<Option<Vec<Event>>, ClientError> {
// Skip empty data
if data.is_empty() || data == "[DONE]" {
return Ok(None);
}
let events = match event_type {
// Response lifecycle
"response.created" => {
let _event: ResponseCreatedEvent = parse_json(data)?;
Some(vec![Event::Status(StatusEvent {
status: ResponseStatus::Started,
})])
}
"response.in_progress" => {
// Just a status update, no action needed
None
}
"response.completed" | "response.done" => {
let event: ResponseDoneEvent = parse_json(data)?;
let mut events = Vec::new();
// Emit usage if present
if let Some(usage) = event.response.usage {
events.push(Event::Usage(UsageEvent {
input_tokens: usage.input_tokens,
output_tokens: usage.output_tokens,
total_tokens: usage.total_tokens,
cache_read_input_tokens: None,
cache_creation_input_tokens: None,
}));
}
events.push(Event::Status(StatusEvent {
status: ResponseStatus::Completed,
}));
Some(events)
}
"response.failed" => {
// Try to parse error
if let Ok(error_event) = parse_json::<ApiErrorEvent>(data) {
Some(vec![
Event::Error(ErrorEvent {
code: error_event.error.code,
message: error_event.error.message,
}),
Event::Status(StatusEvent {
status: ResponseStatus::Failed,
}),
])
} else {
Some(vec![Event::Status(StatusEvent {
status: ResponseStatus::Failed,
})])
}
}
// Output item events
"response.output_item.added" => {
let event: OutputItemAddedEvent = parse_json(data)?;
Some(vec![convert_item_added(&event)])
}
"response.output_item.done" => {
let event: OutputItemDoneEvent = parse_json(data)?;
Some(vec![convert_item_done(&event)])
}
// Text content events
"response.output_text.delta" => {
let event: TextDeltaEvent = parse_json(data)?;
Some(vec![Event::text_delta(event.output_index, &event.delta)])
}
"response.output_text.done" => {
// Text done - we'll handle stop in output_item.done
let _event: TextDoneEvent = parse_json(data)?;
None
}
// Content part events
"response.content_part.added" => {
// Content part added - we handle this via output_item.added
None
}
"response.content_part.done" => {
// Content part done - we handle stop in output_item.done
None
}
// Function call events
"response.function_call_arguments.delta" => {
let event: FunctionCallArgumentsDeltaEvent = parse_json(data)?;
Some(vec![Event::BlockDelta(crate::llm_client::event::BlockDelta {
index: event.output_index,
delta: DeltaContent::InputJson(event.delta),
})])
}
"response.function_call_arguments.done" => {
// Arguments done - we handle stop in output_item.done
let _event: FunctionCallArgumentsDoneEvent = parse_json(data)?;
None
}
// Reasoning events
"response.reasoning.delta" | "response.reasoning_summary_text.delta" => {
let event: ReasoningDeltaEvent = parse_json(data)?;
Some(vec![Event::BlockDelta(crate::llm_client::event::BlockDelta {
index: event.output_index,
delta: DeltaContent::Thinking(event.delta),
})])
}
"response.reasoning.done" | "response.reasoning_summary_text.done" => {
// Reasoning done - we handle stop in output_item.done
let _event: ReasoningDoneEvent = parse_json(data)?;
None
}
// Error event
"error" => {
let event: ApiErrorEvent = parse_json(data)?;
Some(vec![Event::Error(ErrorEvent {
code: event.error.code,
message: event.error.message,
})])
}
// Unknown event type - ignore
_ => {
tracing::debug!(event_type = event_type, "Unknown Open Responses event type");
None
}
};
Ok(events)
}
fn parse_json<T: serde::de::DeserializeOwned>(data: &str) -> Result<T, ClientError> {
serde_json::from_str(data).map_err(|e| ClientError::Parse(e.to_string()))
}
fn convert_item_added(event: &OutputItemAddedEvent) -> Event {
match &event.item {
OutputItem::Message { id, role: _, content: _ } => Event::BlockStart(BlockStart {
index: event.output_index,
block_type: crate::llm_client::event::BlockType::Text,
metadata: BlockMetadata::Text,
}),
OutputItem::FunctionCall {
id,
call_id,
name,
arguments: _,
} => Event::BlockStart(BlockStart {
index: event.output_index,
block_type: crate::llm_client::event::BlockType::ToolUse,
metadata: BlockMetadata::ToolUse {
id: call_id.clone(),
name: name.clone(),
},
}),
OutputItem::Reasoning { id, text: _ } => Event::BlockStart(BlockStart {
index: event.output_index,
block_type: crate::llm_client::event::BlockType::Thinking,
metadata: BlockMetadata::Thinking,
}),
}
}
fn convert_item_done(event: &OutputItemDoneEvent) -> Event {
let stop_reason = match &event.item {
OutputItem::FunctionCall { .. } => Some(StopReason::ToolUse),
_ => Some(StopReason::EndTurn),
};
Event::BlockStop(BlockStop {
index: event.output_index,
stop_reason,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_response_created() {
let data = r#"{"response":{"id":"resp_123","status":"in_progress","output":[]}}"#;
let events = parse_event("response.created", data).unwrap().unwrap();
assert_eq!(events.len(), 1);
assert!(matches!(
events[0],
Event::Status(StatusEvent {
status: ResponseStatus::Started
})
));
}
#[test]
fn test_parse_text_delta() {
let data = r#"{"output_index":0,"content_index":0,"delta":"Hello"}"#;
let events = parse_event("response.output_text.delta", data)
.unwrap()
.unwrap();
assert_eq!(events.len(), 1);
if let Event::BlockDelta(delta) = &events[0] {
assert_eq!(delta.index, 0);
assert!(matches!(&delta.delta, DeltaContent::Text(t) if t == "Hello"));
} else {
panic!("Expected BlockDelta");
}
}
#[test]
fn test_parse_output_item_added_message() {
let data = r#"{"output_index":0,"item":{"type":"message","id":"msg_123","role":"assistant","content":[]}}"#;
let events = parse_event("response.output_item.added", data)
.unwrap()
.unwrap();
assert_eq!(events.len(), 1);
if let Event::BlockStart(start) = &events[0] {
assert_eq!(start.index, 0);
assert!(matches!(
start.block_type,
crate::llm_client::event::BlockType::Text
));
} else {
panic!("Expected BlockStart");
}
}
#[test]
fn test_parse_output_item_added_function_call() {
let data = r#"{"output_index":1,"item":{"type":"function_call","id":"fc_123","call_id":"call_456","name":"get_weather","arguments":""}}"#;
let events = parse_event("response.output_item.added", data)
.unwrap()
.unwrap();
assert_eq!(events.len(), 1);
if let Event::BlockStart(start) = &events[0] {
assert_eq!(start.index, 1);
assert!(matches!(
start.block_type,
crate::llm_client::event::BlockType::ToolUse
));
if let BlockMetadata::ToolUse { id, name } = &start.metadata {
assert_eq!(id, "call_456");
assert_eq!(name, "get_weather");
} else {
panic!("Expected ToolUse metadata");
}
} else {
panic!("Expected BlockStart");
}
}
#[test]
fn test_parse_function_call_arguments_delta() {
let data = r#"{"output_index":1,"call_id":"call_456","delta":"{\"city\":"}"#;
let events = parse_event("response.function_call_arguments.delta", data)
.unwrap()
.unwrap();
assert_eq!(events.len(), 1);
if let Event::BlockDelta(delta) = &events[0] {
assert_eq!(delta.index, 1);
assert!(matches!(
&delta.delta,
DeltaContent::InputJson(s) if s == "{\"city\":"
));
} else {
panic!("Expected BlockDelta");
}
}
#[test]
fn test_parse_response_completed() {
let data = r#"{"response":{"id":"resp_123","status":"completed","output":[],"usage":{"input_tokens":10,"output_tokens":20,"total_tokens":30}}}"#;
let events = parse_event("response.completed", data).unwrap().unwrap();
assert_eq!(events.len(), 2);
// First event should be usage
if let Event::Usage(usage) = &events[0] {
assert_eq!(usage.input_tokens, Some(10));
assert_eq!(usage.output_tokens, Some(20));
assert_eq!(usage.total_tokens, Some(30));
} else {
panic!("Expected Usage event");
}
// Second event should be status
assert!(matches!(
events[1],
Event::Status(StatusEvent {
status: ResponseStatus::Completed
})
));
}
#[test]
fn test_parse_error() {
let data = r#"{"error":{"code":"rate_limit","message":"Too many requests"}}"#;
let events = parse_event("error", data).unwrap().unwrap();
assert_eq!(events.len(), 1);
if let Event::Error(err) = &events[0] {
assert_eq!(err.code, Some("rate_limit".to_string()));
assert_eq!(err.message, "Too many requests");
} else {
panic!("Expected Error event");
}
}
#[test]
fn test_parse_unknown_event() {
let data = r#"{}"#;
let events = parse_event("some.unknown.event", data).unwrap();
assert!(events.is_none());
}
}
@@ -1,49 +0,0 @@
//! Open Responses Scheme
//!
//! Handles request/response conversion for the Open Responses API.
//! Since our internal types are already Open Responses native, this scheme
//! primarily passes through data with minimal transformation.
mod events;
mod request;
use crate::llm_client::{ClientError, Request};
pub use events::*;
pub use request::*;
/// Open Responses Scheme
///
/// Handles conversion between internal types and the Open Responses wire format.
#[derive(Debug, Clone, Default)]
pub struct OpenResponsesScheme {
/// Optional model override
pub model: Option<String>,
}
impl OpenResponsesScheme {
/// Create a new OpenResponsesScheme
pub fn new() -> Self {
Self::default()
}
/// Set the model
pub fn with_model(mut self, model: impl Into<String>) -> Self {
self.model = Some(model.into());
self
}
/// Build Open Responses request from internal Request
pub fn build_request(&self, model: &str, request: &Request) -> OpenResponsesRequest {
build_request(model, request)
}
/// Parse SSE event data into internal Event(s)
pub fn parse_event(
&self,
event_type: &str,
data: &str,
) -> Result<Option<Vec<crate::llm_client::Event>>, ClientError> {
parse_event(event_type, data)
}
}
@@ -1,285 +0,0 @@
//! Open Responses Request Builder
//!
//! Converts internal Request/Item types to Open Responses API format.
//! Since our internal types are already Open Responses native, this is
//! mostly a direct serialization with some field renaming.
use serde::Serialize;
use serde_json::Value;
use crate::llm_client::{types::Item, Request, ToolDefinition};
/// Open Responses API request body
#[derive(Debug, Serialize)]
pub struct OpenResponsesRequest {
/// Model identifier
pub model: String,
/// Input items (conversation history)
pub input: Vec<OpenResponsesItem>,
/// System instructions
#[serde(skip_serializing_if = "Option::is_none")]
pub instructions: Option<String>,
/// Tool definitions
#[serde(skip_serializing_if = "Vec::is_empty")]
pub tools: Vec<OpenResponsesTool>,
/// Enable streaming
pub stream: bool,
/// Maximum output tokens
#[serde(skip_serializing_if = "Option::is_none")]
pub max_output_tokens: Option<u32>,
/// Temperature
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f32>,
/// Top P (nucleus sampling)
#[serde(skip_serializing_if = "Option::is_none")]
pub top_p: Option<f32>,
}
/// Open Responses input item
#[derive(Debug, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum OpenResponsesItem {
/// Message item
Message {
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<String>,
role: String,
content: Vec<OpenResponsesContentPart>,
},
/// Function call item
FunctionCall {
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<String>,
call_id: String,
name: String,
arguments: String,
},
/// Function call output item
FunctionCallOutput {
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<String>,
call_id: String,
output: String,
},
/// Reasoning item
Reasoning {
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<String>,
text: String,
},
}
/// Open Responses content part
#[derive(Debug, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum OpenResponsesContentPart {
/// Input text (for user messages)
InputText { text: String },
/// Output text (for assistant messages)
OutputText { text: String },
/// Refusal
Refusal { refusal: String },
}
/// Open Responses tool definition
#[derive(Debug, Serialize)]
pub struct OpenResponsesTool {
/// Tool type (always "function")
pub r#type: String,
/// Function definition
pub name: String,
/// Description
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// Parameters schema
pub parameters: Value,
}
/// Build Open Responses request from internal Request
pub fn build_request(model: &str, request: &Request) -> OpenResponsesRequest {
let input = request.items.iter().map(convert_item).collect();
let tools = request.tools.iter().map(convert_tool).collect();
OpenResponsesRequest {
model: model.to_string(),
input,
instructions: request.system_prompt.clone(),
tools,
stream: true,
max_output_tokens: request.config.max_tokens,
temperature: request.config.temperature,
top_p: request.config.top_p,
}
}
fn convert_item(item: &Item) -> OpenResponsesItem {
match item {
Item::Message {
id,
role,
content,
status: _,
} => {
let role_str = match role {
crate::llm_client::types::Role::User => "user",
crate::llm_client::types::Role::Assistant => "assistant",
crate::llm_client::types::Role::System => "system",
};
let parts = content
.iter()
.map(|p| match p {
crate::llm_client::types::ContentPart::InputText { text } => {
OpenResponsesContentPart::InputText { text: text.clone() }
}
crate::llm_client::types::ContentPart::OutputText { text } => {
OpenResponsesContentPart::OutputText { text: text.clone() }
}
crate::llm_client::types::ContentPart::Refusal { refusal } => {
OpenResponsesContentPart::Refusal {
refusal: refusal.clone(),
}
}
})
.collect();
OpenResponsesItem::Message {
id: id.clone(),
role: role_str.to_string(),
content: parts,
}
}
Item::FunctionCall {
id,
call_id,
name,
arguments,
status: _,
} => OpenResponsesItem::FunctionCall {
id: id.clone(),
call_id: call_id.clone(),
name: name.clone(),
arguments: arguments.clone(),
},
Item::FunctionCallOutput {
id,
call_id,
output,
} => OpenResponsesItem::FunctionCallOutput {
id: id.clone(),
call_id: call_id.clone(),
output: output.clone(),
},
Item::Reasoning {
id,
text,
status: _,
} => OpenResponsesItem::Reasoning {
id: id.clone(),
text: text.clone(),
},
}
}
fn convert_tool(tool: &ToolDefinition) -> OpenResponsesTool {
OpenResponsesTool {
r#type: "function".to_string(),
name: tool.name.clone(),
description: tool.description.clone(),
parameters: tool.input_schema.clone(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::llm_client::types::Item;
#[test]
fn test_build_simple_request() {
let request = Request::new()
.system("You are a helpful assistant.")
.user("Hello!");
let or_req = build_request("gpt-4o", &request);
assert_eq!(or_req.model, "gpt-4o");
assert_eq!(
or_req.instructions,
Some("You are a helpful assistant.".to_string())
);
assert_eq!(or_req.input.len(), 1);
assert!(or_req.stream);
}
#[test]
fn test_build_request_with_tool() {
let request = Request::new().user("What's the weather?").tool(
ToolDefinition::new("get_weather")
.description("Get current weather")
.input_schema(serde_json::json!({
"type": "object",
"properties": {
"location": { "type": "string" }
},
"required": ["location"]
})),
);
let or_req = build_request("gpt-4o", &request);
assert_eq!(or_req.tools.len(), 1);
assert_eq!(or_req.tools[0].name, "get_weather");
assert_eq!(or_req.tools[0].r#type, "function");
}
#[test]
fn test_function_call_and_output() {
let request = Request::new()
.user("What's the weather?")
.item(Item::function_call(
"call_123",
"get_weather",
r#"{"city":"Tokyo"}"#,
))
.item(Item::function_call_output("call_123", "Sunny, 25°C"));
let or_req = build_request("gpt-4o", &request);
assert_eq!(or_req.input.len(), 3);
// Check function call
if let OpenResponsesItem::FunctionCall { call_id, name, .. } = &or_req.input[1] {
assert_eq!(call_id, "call_123");
assert_eq!(name, "get_weather");
} else {
panic!("Expected FunctionCall");
}
// Check function call output
if let OpenResponsesItem::FunctionCallOutput { call_id, output, .. } = &or_req.input[2] {
assert_eq!(call_id, "call_123");
assert_eq!(output, "Sunny, 25°C");
} else {
panic!("Expected FunctionCallOutput");
}
}
}
-567
View File
@@ -1,567 +0,0 @@
//! LLM Client Common Types - Open Responses Native
//!
//! This module defines types that are natively aligned with the Open Responses specification.
//! The core abstraction is `Item` which represents different types of conversation elements:
//! - Message items (user/assistant messages with content parts)
//! - FunctionCall items (tool invocations)
//! - FunctionCallOutput items (tool results)
//! - Reasoning items (extended thinking)
use serde::{Deserialize, Serialize};
// ============================================================================
// Item - The core unit of conversation
// ============================================================================
/// Item ID type for tracking items in a conversation
pub type ItemId = String;
/// Call ID type for linking function calls to their outputs
pub type CallId = String;
/// Conversation item - the primary unit in Open Responses
///
/// Items represent discrete elements in a conversation. Unlike traditional
/// message-based APIs, Open Responses treats tool calls and reasoning as
/// first-class items rather than parts of messages.
///
/// # Examples
///
/// ```ignore
/// use llm_worker::Item;
///
/// // User message
/// let user_item = Item::user_message("Hello!");
///
/// // Assistant message
/// let assistant_item = Item::assistant_message("Hi there!");
///
/// // Function call
/// let call = Item::function_call("call_123", "get_weather", json!({"city": "Tokyo"}));
///
/// // Function call output
/// let result = Item::function_call_output("call_123", "Sunny, 25°C");
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Item {
/// User or assistant message with content parts
Message {
/// Optional item ID
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<ItemId>,
/// Message role
role: Role,
/// Content parts
content: Vec<ContentPart>,
/// Item status
#[serde(skip_serializing_if = "Option::is_none")]
status: Option<ItemStatus>,
},
/// Function (tool) call from the assistant
FunctionCall {
/// Optional item ID
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<ItemId>,
/// Call ID for linking to output
call_id: CallId,
/// Function name
name: String,
/// Function arguments as JSON string
arguments: String,
/// Item status
#[serde(skip_serializing_if = "Option::is_none")]
status: Option<ItemStatus>,
},
/// Function (tool) call output/result
FunctionCallOutput {
/// Optional item ID
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<ItemId>,
/// Call ID linking to the function call
call_id: CallId,
/// Output content
output: String,
},
/// Reasoning/thinking item
Reasoning {
/// Optional item ID
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<ItemId>,
/// Reasoning text
text: String,
/// Item status
#[serde(skip_serializing_if = "Option::is_none")]
status: Option<ItemStatus>,
},
}
impl Item {
// ========================================================================
// Message constructors
// ========================================================================
/// Create a user message item with text content
pub fn user_message(text: impl Into<String>) -> Self {
Self::Message {
id: None,
role: Role::User,
content: vec![ContentPart::InputText {
text: text.into(),
}],
status: None,
}
}
/// Create a user message item with multiple content parts
pub fn user_message_parts(parts: Vec<ContentPart>) -> Self {
Self::Message {
id: None,
role: Role::User,
content: parts,
status: None,
}
}
/// Create an assistant message item with text content
pub fn assistant_message(text: impl Into<String>) -> Self {
Self::Message {
id: None,
role: Role::Assistant,
content: vec![ContentPart::OutputText {
text: text.into(),
}],
status: None,
}
}
/// Create an assistant message item with multiple content parts
pub fn assistant_message_parts(parts: Vec<ContentPart>) -> Self {
Self::Message {
id: None,
role: Role::Assistant,
content: parts,
status: None,
}
}
// ========================================================================
// Function call constructors
// ========================================================================
/// Create a function call item
pub fn function_call(
call_id: impl Into<String>,
name: impl Into<String>,
arguments: impl Into<String>,
) -> Self {
Self::FunctionCall {
id: None,
call_id: call_id.into(),
name: name.into(),
arguments: arguments.into(),
status: None,
}
}
/// Create a function call item from a JSON value
pub fn function_call_json(
call_id: impl Into<String>,
name: impl Into<String>,
arguments: serde_json::Value,
) -> Self {
Self::function_call(call_id, name, arguments.to_string())
}
/// Create a function call output item
pub fn function_call_output(call_id: impl Into<String>, output: impl Into<String>) -> Self {
Self::FunctionCallOutput {
id: None,
call_id: call_id.into(),
output: output.into(),
}
}
// ========================================================================
// Reasoning constructors
// ========================================================================
/// Create a reasoning item
pub fn reasoning(text: impl Into<String>) -> Self {
Self::Reasoning {
id: None,
text: text.into(),
status: None,
}
}
// ========================================================================
// Builder methods
// ========================================================================
/// Set the item ID
pub fn with_id(mut self, id: impl Into<String>) -> Self {
match &mut self {
Self::Message { id: item_id, .. } => *item_id = Some(id.into()),
Self::FunctionCall { id: item_id, .. } => *item_id = Some(id.into()),
Self::FunctionCallOutput { id: item_id, .. } => *item_id = Some(id.into()),
Self::Reasoning { id: item_id, .. } => *item_id = Some(id.into()),
}
self
}
/// Set the item status
pub fn with_status(mut self, new_status: ItemStatus) -> Self {
match &mut self {
Self::Message { status, .. } => *status = Some(new_status),
Self::FunctionCall { status, .. } => *status = Some(new_status),
Self::FunctionCallOutput { .. } => {} // Output items don't have status
Self::Reasoning { status, .. } => *status = Some(new_status),
}
self
}
// ========================================================================
// Accessors
// ========================================================================
/// Get the item ID if set
pub fn id(&self) -> Option<&str> {
match self {
Self::Message { id, .. } => id.as_deref(),
Self::FunctionCall { id, .. } => id.as_deref(),
Self::FunctionCallOutput { id, .. } => id.as_deref(),
Self::Reasoning { id, .. } => id.as_deref(),
}
}
/// Get the item type as a string
pub fn item_type(&self) -> &'static str {
match self {
Self::Message { .. } => "message",
Self::FunctionCall { .. } => "function_call",
Self::FunctionCallOutput { .. } => "function_call_output",
Self::Reasoning { .. } => "reasoning",
}
}
/// Check if this is a user message
pub fn is_user_message(&self) -> bool {
matches!(self, Self::Message { role: Role::User, .. })
}
/// Check if this is an assistant message
pub fn is_assistant_message(&self) -> bool {
matches!(self, Self::Message { role: Role::Assistant, .. })
}
/// Check if this is a function call
pub fn is_function_call(&self) -> bool {
matches!(self, Self::FunctionCall { .. })
}
/// Check if this is a function call output
pub fn is_function_call_output(&self) -> bool {
matches!(self, Self::FunctionCallOutput { .. })
}
/// Check if this is a reasoning item
pub fn is_reasoning(&self) -> bool {
matches!(self, Self::Reasoning { .. })
}
/// Get text content if this is a simple text message
pub fn as_text(&self) -> Option<&str> {
match self {
Self::Message { content, .. } if content.len() == 1 => match &content[0] {
ContentPart::InputText { text } => Some(text),
ContentPart::OutputText { text } => Some(text),
_ => None,
},
_ => None,
}
}
}
// ============================================================================
// Content Parts - Components within message items
// ============================================================================
/// Content part within a message item
///
/// Open Responses distinguishes between input and output content types.
/// Input types are used in user messages, output types in assistant messages.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentPart {
/// Input text (for user messages)
InputText {
/// The text content
text: String,
},
/// Output text (for assistant messages)
OutputText {
/// The text content
text: String,
},
/// Refusal content (for assistant messages)
Refusal {
/// The refusal message
refusal: String,
},
// Future: InputAudio, OutputAudio, etc.
}
impl ContentPart {
/// Create an input text part
pub fn input_text(text: impl Into<String>) -> Self {
Self::InputText { text: text.into() }
}
/// Create an output text part
pub fn output_text(text: impl Into<String>) -> Self {
Self::OutputText { text: text.into() }
}
/// Create a refusal part
pub fn refusal(refusal: impl Into<String>) -> Self {
Self::Refusal {
refusal: refusal.into(),
}
}
/// Get the text content regardless of type
pub fn as_text(&self) -> &str {
match self {
Self::InputText { text } => text,
Self::OutputText { text } => text,
Self::Refusal { refusal } => refusal,
}
}
}
// ============================================================================
// Role and Status
// ============================================================================
/// Message role
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
/// User
User,
/// Assistant
Assistant,
/// System (for system prompts, not typically used in items)
System,
}
/// Item status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ItemStatus {
/// Item is being generated
InProgress,
/// Item completed successfully
Completed,
/// Item was truncated (e.g., max tokens)
Incomplete,
}
// ============================================================================
// Request Types
// ============================================================================
/// LLM Request
#[derive(Debug, Clone, Default)]
pub struct Request {
/// System prompt (instructions)
pub system_prompt: Option<String>,
/// Input items (conversation history)
pub items: Vec<Item>,
/// Tool definitions
pub tools: Vec<ToolDefinition>,
/// Request configuration
pub config: RequestConfig,
}
impl Request {
/// Create a new empty request
pub fn new() -> Self {
Self::default()
}
/// Set the system prompt
pub fn system(mut self, prompt: impl Into<String>) -> Self {
self.system_prompt = Some(prompt.into());
self
}
/// Add a user message
pub fn user(mut self, content: impl Into<String>) -> Self {
self.items.push(Item::user_message(content));
self
}
/// Add an assistant message
pub fn assistant(mut self, content: impl Into<String>) -> Self {
self.items.push(Item::assistant_message(content));
self
}
/// Add an item
pub fn item(mut self, item: Item) -> Self {
self.items.push(item);
self
}
/// Add multiple items
pub fn items(mut self, items: impl IntoIterator<Item = Item>) -> Self {
self.items.extend(items);
self
}
/// Add a tool definition
pub fn tool(mut self, tool: ToolDefinition) -> Self {
self.tools.push(tool);
self
}
/// Set the request config
pub fn config(mut self, config: RequestConfig) -> Self {
self.config = config;
self
}
/// Set max tokens
pub fn max_tokens(mut self, max_tokens: u32) -> Self {
self.config.max_tokens = Some(max_tokens);
self
}
/// Set temperature
pub fn temperature(mut self, temperature: f32) -> Self {
self.config.temperature = Some(temperature);
self
}
/// Set top_p
pub fn top_p(mut self, top_p: f32) -> Self {
self.config.top_p = Some(top_p);
self
}
/// Set top_k
pub fn top_k(mut self, top_k: u32) -> Self {
self.config.top_k = Some(top_k);
self
}
/// Add a stop sequence
pub fn stop_sequence(mut self, sequence: impl Into<String>) -> Self {
self.config.stop_sequences.push(sequence.into());
self
}
}
// ============================================================================
// Tool Definition
// ============================================================================
/// Tool (function) definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolDefinition {
/// Tool name
pub name: String,
/// Tool description
pub description: Option<String>,
/// Input schema (JSON Schema)
pub input_schema: serde_json::Value,
}
impl ToolDefinition {
/// Create a new tool definition
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
description: None,
input_schema: serde_json::json!({
"type": "object",
"properties": {}
}),
}
}
/// Set the description
pub fn description(mut self, desc: impl Into<String>) -> Self {
self.description = Some(desc.into());
self
}
/// Set the input schema
pub fn input_schema(mut self, schema: serde_json::Value) -> Self {
self.input_schema = schema;
self
}
}
// ============================================================================
// Request Config
// ============================================================================
/// Request configuration
#[derive(Debug, Clone, Default)]
pub struct RequestConfig {
/// Maximum tokens to generate
pub max_tokens: Option<u32>,
/// Temperature (randomness)
pub temperature: Option<f32>,
/// Top P (nucleus sampling)
pub top_p: Option<f32>,
/// Top K
pub top_k: Option<u32>,
/// Stop sequences
pub stop_sequences: Vec<String>,
}
impl RequestConfig {
/// Create a new default config
pub fn new() -> Self {
Self::default()
}
/// Set max tokens
pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
self.max_tokens = Some(max_tokens);
self
}
/// Set temperature
pub fn with_temperature(mut self, temperature: f32) -> Self {
self.temperature = Some(temperature);
self
}
/// Set top_p
pub fn with_top_p(mut self, top_p: f32) -> Self {
self.top_p = Some(top_p);
self
}
/// Set top_k
pub fn with_top_k(mut self, top_k: u32) -> Self {
self.top_k = Some(top_k);
self
}
/// Add a stop sequence
pub fn with_stop_sequence(mut self, sequence: impl Into<String>) -> Self {
self.stop_sequences.push(sequence.into());
self
}
}
-16
View File
@@ -1,16 +0,0 @@
//! Message and Item Types
//!
//! This module provides the core types for representing conversation items
//! in the Open Responses format.
//!
//! The primary type is [`Item`], which represents different kinds of conversation
//! elements: messages, function calls, function call outputs, and reasoning.
// Re-export all types from llm_client::types
pub use crate::llm_client::types::{ContentPart, Item, Role};
/// Convenience alias for backward compatibility
///
/// In the Open Responses model, messages are just one type of Item.
/// This alias allows code that expects a "Message" type to continue working.
pub type Message = Item;
-60
View File
@@ -1,60 +0,0 @@
//! Worker State
//!
//! State marker types for cache protection using the Type-state pattern.
//! Worker has state transitions from `Mutable` → `CacheLocked`.
/// Marker trait representing Worker state
///
/// This trait is sealed and cannot be implemented externally.
pub trait WorkerState: private::Sealed + Send + Sync + 'static {}
mod private {
pub trait Sealed {}
}
/// Mutable state (editable)
///
/// In this state, the following operations are available:
/// - Setting/changing system prompt
/// - Editing message history (add, delete, clear)
/// - Registering tools and hooks
///
/// Can transition to [`CacheLocked`] state via `Worker::lock()`.
///
/// # Examples
///
/// ```ignore
/// use llm_worker::Worker;
///
/// let mut worker = Worker::new(client)
/// .system_prompt("You are helpful.");
///
/// // History can be edited
/// worker.push_message(Message::user("Hello"));
/// worker.clear_history();
///
/// // Lock to protected state
/// let locked = worker.lock();
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct Mutable;
impl private::Sealed for Mutable {}
impl WorkerState for Mutable {}
/// Cache locked state (cache protected)
///
/// In this state, the following restrictions apply:
/// - System prompt cannot be changed
/// - Existing message history cannot be modified (only appending to the end)
///
/// To ensure LLM API KV cache hits,
/// using this state during execution is recommended.
///
/// Can return to [`Mutable`] state via `Worker::unlock()`,
/// but note that cache protection will be released.
#[derive(Debug, Clone, Copy, Default)]
pub struct CacheLocked;
impl private::Sealed for CacheLocked {}
impl WorkerState for CacheLocked {}
-448
View File
@@ -1,448 +0,0 @@
//! Timeline層のイベント型
//!
//! Timelineが受け取り、各Handlerへディスパッチするイベント表現。
use serde::{Deserialize, Serialize};
// =============================================================================
// Core Event Types (from llm_client layer)
// =============================================================================
/// LLMからのストリーミングイベント
///
/// 各LLMプロバイダからのレスポンスは、この`Event`のストリームとして
/// 統一的に処理されます。
///
/// # イベントの種類
///
/// - **メタイベント**: `Ping`, `Usage`, `Status`, `Error`
/// - **ブロックイベント**: `BlockStart`, `BlockDelta`, `BlockStop`, `BlockAbort`
///
/// # ブロックのライフサイクル
///
/// テキストやツール呼び出しは、`BlockStart` → `BlockDelta`(複数) → `BlockStop`
/// の順序でイベントが発生します。
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Event {
/// ハートビート
Ping(PingEvent),
/// トークン使用量
Usage(UsageEvent),
/// ストリームのステータス変化
Status(StatusEvent),
/// エラー発生
Error(ErrorEvent),
/// ブロック開始(テキスト、ツール使用等)
BlockStart(BlockStart),
/// ブロックの差分データ
BlockDelta(BlockDelta),
/// ブロック正常終了
BlockStop(BlockStop),
/// ブロック中断
BlockAbort(BlockAbort),
}
// =============================================================================
// Meta Events
// =============================================================================
/// Pingイベント(ハートビート)
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct PingEvent {
pub timestamp: Option<u64>,
}
/// 使用量イベント
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct UsageEvent {
/// 入力トークン数
pub input_tokens: Option<u64>,
/// 出力トークン数
pub output_tokens: Option<u64>,
/// 合計トークン数
pub total_tokens: Option<u64>,
/// キャッシュ読み込みトークン数
pub cache_read_input_tokens: Option<u64>,
/// キャッシュ作成トークン数
pub cache_creation_input_tokens: Option<u64>,
}
/// ステータスイベント
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StatusEvent {
pub status: ResponseStatus,
}
/// レスポンスステータス
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ResponseStatus {
/// ストリーム開始
Started,
/// 正常完了
Completed,
/// キャンセルされた
Cancelled,
/// エラー発生
Failed,
}
/// エラーイベント
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ErrorEvent {
pub code: Option<String>,
pub message: String,
}
// =============================================================================
// Block Types
// =============================================================================
/// ブロックの種別
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BlockType {
/// テキスト生成
Text,
/// 思考 (Claude Extended Thinking等)
Thinking,
/// ツール呼び出し
ToolUse,
/// ツール結果
ToolResult,
}
/// ブロック開始イベント
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BlockStart {
/// ブロックのインデックス
pub index: usize,
/// ブロックの種別
pub block_type: BlockType,
/// ブロック固有のメタデータ
pub metadata: BlockMetadata,
}
impl BlockStart {
pub fn block_type(&self) -> BlockType {
self.block_type
}
}
/// ブロックのメタデータ
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BlockMetadata {
Text,
Thinking,
ToolUse { id: String, name: String },
ToolResult { tool_use_id: String },
}
/// ブロックデルタイベント
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BlockDelta {
/// ブロックのインデックス
pub index: usize,
/// デルタの内容
pub delta: DeltaContent,
}
/// デルタの内容
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum DeltaContent {
/// テキストデルタ
Text(String),
/// 思考デルタ
Thinking(String),
/// ツール引数のJSON部分文字列
InputJson(String),
}
impl DeltaContent {
/// デルタのブロック種別を取得
pub fn block_type(&self) -> BlockType {
match self {
DeltaContent::Text(_) => BlockType::Text,
DeltaContent::Thinking(_) => BlockType::Thinking,
DeltaContent::InputJson(_) => BlockType::ToolUse,
}
}
}
/// ブロック停止イベント
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BlockStop {
/// ブロックのインデックス
pub index: usize,
/// ブロックの種別
pub block_type: BlockType,
/// 停止理由
pub stop_reason: Option<StopReason>,
}
impl BlockStop {
pub fn block_type(&self) -> BlockType {
self.block_type
}
}
/// ブロック中断イベント
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BlockAbort {
/// ブロックのインデックス
pub index: usize,
/// ブロックの種別
pub block_type: BlockType,
/// 中断理由
pub reason: String,
}
impl BlockAbort {
pub fn block_type(&self) -> BlockType {
self.block_type
}
}
/// 停止理由
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum StopReason {
/// 自然終了
EndTurn,
/// 最大トークン数到達
MaxTokens,
/// ストップシーケンス到達
StopSequence,
/// ツール使用
ToolUse,
}
// =============================================================================
// Builder / Factory helpers
// =============================================================================
impl Event {
/// テキストブロック開始イベントを作成
pub fn text_block_start(index: usize) -> Self {
Event::BlockStart(BlockStart {
index,
block_type: BlockType::Text,
metadata: BlockMetadata::Text,
})
}
/// テキストデルタイベントを作成
pub fn text_delta(index: usize, text: impl Into<String>) -> Self {
Event::BlockDelta(BlockDelta {
index,
delta: DeltaContent::Text(text.into()),
})
}
/// テキストブロック停止イベントを作成
pub fn text_block_stop(index: usize, stop_reason: Option<StopReason>) -> Self {
Event::BlockStop(BlockStop {
index,
block_type: BlockType::Text,
stop_reason,
})
}
/// ツール使用ブロック開始イベントを作成
pub fn tool_use_start(index: usize, id: impl Into<String>, name: impl Into<String>) -> Self {
Event::BlockStart(BlockStart {
index,
block_type: BlockType::ToolUse,
metadata: BlockMetadata::ToolUse {
id: id.into(),
name: name.into(),
},
})
}
/// ツール引数デルタイベントを作成
pub fn tool_input_delta(index: usize, json: impl Into<String>) -> Self {
Event::BlockDelta(BlockDelta {
index,
delta: DeltaContent::InputJson(json.into()),
})
}
/// ツール使用ブロック停止イベントを作成
pub fn tool_use_stop(index: usize) -> Self {
Event::BlockStop(BlockStop {
index,
block_type: BlockType::ToolUse,
stop_reason: Some(StopReason::ToolUse),
})
}
/// 使用量イベントを作成
pub fn usage(input_tokens: u64, output_tokens: u64) -> Self {
Event::Usage(UsageEvent {
input_tokens: Some(input_tokens),
output_tokens: Some(output_tokens),
total_tokens: Some(input_tokens + output_tokens),
cache_read_input_tokens: None,
cache_creation_input_tokens: None,
})
}
/// Pingイベントを作成
pub fn ping() -> Self {
Event::Ping(PingEvent { timestamp: None })
}
}
// =============================================================================
// Conversions: llm_client::event -> timeline::event
// =============================================================================
impl From<crate::llm_client::event::ResponseStatus> for ResponseStatus {
fn from(value: crate::llm_client::event::ResponseStatus) -> Self {
match value {
crate::llm_client::event::ResponseStatus::Started => ResponseStatus::Started,
crate::llm_client::event::ResponseStatus::Completed => ResponseStatus::Completed,
crate::llm_client::event::ResponseStatus::Cancelled => ResponseStatus::Cancelled,
crate::llm_client::event::ResponseStatus::Failed => ResponseStatus::Failed,
}
}
}
impl From<crate::llm_client::event::BlockType> for BlockType {
fn from(value: crate::llm_client::event::BlockType) -> Self {
match value {
crate::llm_client::event::BlockType::Text => BlockType::Text,
crate::llm_client::event::BlockType::Thinking => BlockType::Thinking,
crate::llm_client::event::BlockType::ToolUse => BlockType::ToolUse,
crate::llm_client::event::BlockType::ToolResult => BlockType::ToolResult,
}
}
}
impl From<crate::llm_client::event::BlockMetadata> for BlockMetadata {
fn from(value: crate::llm_client::event::BlockMetadata) -> Self {
match value {
crate::llm_client::event::BlockMetadata::Text => BlockMetadata::Text,
crate::llm_client::event::BlockMetadata::Thinking => BlockMetadata::Thinking,
crate::llm_client::event::BlockMetadata::ToolUse { id, name } => {
BlockMetadata::ToolUse { id, name }
}
crate::llm_client::event::BlockMetadata::ToolResult { tool_use_id } => {
BlockMetadata::ToolResult { tool_use_id }
}
}
}
}
impl From<crate::llm_client::event::DeltaContent> for DeltaContent {
fn from(value: crate::llm_client::event::DeltaContent) -> Self {
match value {
crate::llm_client::event::DeltaContent::Text(text) => DeltaContent::Text(text),
crate::llm_client::event::DeltaContent::Thinking(text) => DeltaContent::Thinking(text),
crate::llm_client::event::DeltaContent::InputJson(json) => {
DeltaContent::InputJson(json)
}
}
}
}
impl From<crate::llm_client::event::StopReason> for StopReason {
fn from(value: crate::llm_client::event::StopReason) -> Self {
match value {
crate::llm_client::event::StopReason::EndTurn => StopReason::EndTurn,
crate::llm_client::event::StopReason::MaxTokens => StopReason::MaxTokens,
crate::llm_client::event::StopReason::StopSequence => StopReason::StopSequence,
crate::llm_client::event::StopReason::ToolUse => StopReason::ToolUse,
}
}
}
impl From<crate::llm_client::event::PingEvent> for PingEvent {
fn from(value: crate::llm_client::event::PingEvent) -> Self {
PingEvent {
timestamp: value.timestamp,
}
}
}
impl From<crate::llm_client::event::UsageEvent> for UsageEvent {
fn from(value: crate::llm_client::event::UsageEvent) -> Self {
UsageEvent {
input_tokens: value.input_tokens,
output_tokens: value.output_tokens,
total_tokens: value.total_tokens,
cache_read_input_tokens: value.cache_read_input_tokens,
cache_creation_input_tokens: value.cache_creation_input_tokens,
}
}
}
impl From<crate::llm_client::event::StatusEvent> for StatusEvent {
fn from(value: crate::llm_client::event::StatusEvent) -> Self {
StatusEvent {
status: value.status.into(),
}
}
}
impl From<crate::llm_client::event::ErrorEvent> for ErrorEvent {
fn from(value: crate::llm_client::event::ErrorEvent) -> Self {
ErrorEvent {
code: value.code,
message: value.message,
}
}
}
impl From<crate::llm_client::event::BlockStart> for BlockStart {
fn from(value: crate::llm_client::event::BlockStart) -> Self {
BlockStart {
index: value.index,
block_type: value.block_type.into(),
metadata: value.metadata.into(),
}
}
}
impl From<crate::llm_client::event::BlockDelta> for BlockDelta {
fn from(value: crate::llm_client::event::BlockDelta) -> Self {
BlockDelta {
index: value.index,
delta: value.delta.into(),
}
}
}
impl From<crate::llm_client::event::BlockStop> for BlockStop {
fn from(value: crate::llm_client::event::BlockStop) -> Self {
BlockStop {
index: value.index,
block_type: value.block_type.into(),
stop_reason: value.stop_reason.map(Into::into),
}
}
}
impl From<crate::llm_client::event::BlockAbort> for BlockAbort {
fn from(value: crate::llm_client::event::BlockAbort) -> Self {
BlockAbort {
index: value.index,
block_type: value.block_type.into(),
reason: value.reason,
}
}
}
impl From<crate::llm_client::event::Event> for Event {
fn from(value: crate::llm_client::event::Event) -> Self {
match value {
crate::llm_client::event::Event::Ping(p) => Event::Ping(p.into()),
crate::llm_client::event::Event::Usage(u) => Event::Usage(u.into()),
crate::llm_client::event::Event::Status(s) => Event::Status(s.into()),
crate::llm_client::event::Event::Error(e) => Event::Error(e.into()),
crate::llm_client::event::Event::BlockStart(s) => Event::BlockStart(s.into()),
crate::llm_client::event::Event::BlockDelta(d) => Event::BlockDelta(d.into()),
crate::llm_client::event::Event::BlockStop(s) => Event::BlockStop(s.into()),
crate::llm_client::event::Event::BlockAbort(a) => Event::BlockAbort(a.into()),
}
}
}
-154
View File
@@ -1,154 +0,0 @@
//! Tool Definition
//!
//! Traits for defining tools callable by LLM.
//! Usually auto-implemented using the `#[tool]` macro.
use std::sync::Arc;
use async_trait::async_trait;
use serde_json::Value;
use thiserror::Error;
/// Error during tool execution
#[derive(Debug, Error)]
pub enum ToolError {
/// Invalid argument
#[error("Invalid argument: {0}")]
InvalidArgument(String),
/// Execution failed
#[error("Execution failed: {0}")]
ExecutionFailed(String),
/// Internal error
#[error("Internal error: {0}")]
Internal(String),
}
// =============================================================================
// ToolMeta - Immutable Meta Information
// =============================================================================
/// Tool meta information (fixed at registration, immutable)
///
/// Generated from `ToolDefinition` factory and does not change after registration with Worker.
/// Used for sending tool definitions to LLM.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToolMeta {
/// Tool name (used by LLM for identification)
pub name: String,
/// Tool description (included in prompt to LLM)
pub description: String,
/// JSON Schema for arguments
pub input_schema: Value,
}
impl ToolMeta {
/// Create a new ToolMeta
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
description: String::new(),
input_schema: Value::Object(Default::default()),
}
}
/// Set the description
pub fn description(mut self, desc: impl Into<String>) -> Self {
self.description = desc.into();
self
}
/// Set the argument schema
pub fn input_schema(mut self, schema: Value) -> Self {
self.input_schema = schema;
self
}
}
// =============================================================================
// ToolDefinition - Factory Type
// =============================================================================
/// Tool definition factory
///
/// When called, returns `(ToolMeta, Arc<dyn Tool>)`.
/// Called once during Worker registration, and the meta information and instance
/// are cached at session scope.
///
/// # Examples
///
/// ```ignore
/// let def: ToolDefinition = Arc::new(|| {
/// (
/// ToolMeta::new("my_tool")
/// .description("My tool description")
/// .input_schema(json!({"type": "object"})),
/// Arc::new(MyToolImpl { state: 0 }) as Arc<dyn Tool>,
/// )
/// });
/// worker.register_tool(def)?;
/// ```
pub type ToolDefinition = Arc<dyn Fn() -> (ToolMeta, Arc<dyn Tool>) + Send + Sync>;
// =============================================================================
// Tool trait
// =============================================================================
/// Trait for defining tools callable by LLM
///
/// Tools are used by LLM to access external resources
/// or execute computations.
/// Can maintain state during the session.
///
/// # How to Implement
///
/// Usually auto-implemented using the `#[tool_registry]` macro:
///
/// ```ignore
/// #[tool_registry]
/// impl MyApp {
/// #[tool]
/// async fn search(&self, query: String) -> String {
/// format!("Results for: {}", query)
/// }
/// }
///
/// // Register
/// worker.register_tool(app.search_definition())?;
/// ```
///
/// # Manual Implementation
///
/// ```ignore
/// use llm_worker::tool::{Tool, ToolError, ToolMeta, ToolDefinition};
/// use std::sync::Arc;
///
/// struct MyTool { counter: std::sync::atomic::AtomicUsize }
///
/// #[async_trait::async_trait]
/// impl Tool for MyTool {
/// async fn execute(&self, input: &str) -> Result<String, ToolError> {
/// self.counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
/// Ok("result".to_string())
/// }
/// }
///
/// let def: ToolDefinition = Arc::new(|| {
/// (
/// ToolMeta::new("my_tool")
/// .description("My custom tool")
/// .input_schema(serde_json::json!({"type": "object"})),
/// Arc::new(MyTool { counter: Default::default() }) as Arc<dyn Tool>,
/// )
/// });
/// ```
#[async_trait]
pub trait Tool: Send + Sync {
/// Execute the tool
///
/// # Arguments
/// * `input_json` - JSON-formatted arguments generated by LLM
///
/// # Returns
/// Result string from execution. This content is returned to LLM.
async fn execute(&self, input_json: &str) -> Result<String, ToolError>;
}
-182
View File
@@ -1,182 +0,0 @@
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use thiserror::Error;
use crate::llm_client::ToolDefinition as LlmToolDefinition;
use crate::tool::{Tool, ToolDefinition as WorkerToolDefinition, ToolMeta};
type ToolMap = HashMap<String, (ToolMeta, Arc<dyn Tool>)>;
/// Errors produced by ToolServer operations.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum ToolServerError {
/// A tool with the same name already exists.
#[error("Tool with name '{0}' already registered")]
DuplicateName(String),
/// Requested tool was not found.
#[error("Tool '{0}' not found")]
ToolNotFound(String),
/// Tool execution failed.
#[error("Tool execution failed: {0}")]
ToolExecution(String),
}
/// In-memory tool server.
#[derive(Clone, Default)]
pub struct ToolServer {
tools: Arc<Mutex<ToolMap>>,
}
impl ToolServer {
/// Create a new empty tool server.
pub fn new() -> Self {
Self::default()
}
/// Create a handle for shared access.
pub fn handle(&self) -> ToolServerHandle {
ToolServerHandle {
tools: Arc::clone(&self.tools),
}
}
}
/// Shareable handle to a tool server.
#[derive(Clone, Default)]
pub struct ToolServerHandle {
tools: Arc<Mutex<ToolMap>>,
}
impl ToolServerHandle {
/// Register one tool.
pub(crate) fn register_tool(
&self,
factory: WorkerToolDefinition,
) -> Result<(), ToolServerError> {
let (meta, instance) = factory();
let mut guard = self.tools.lock().unwrap_or_else(|e| e.into_inner());
if guard.contains_key(&meta.name) {
return Err(ToolServerError::DuplicateName(meta.name));
}
guard.insert(meta.name.clone(), (meta, instance));
Ok(())
}
/// Register many tools.
pub(crate) fn register_tools(
&self,
factories: impl IntoIterator<Item = WorkerToolDefinition>,
) -> Result<(), ToolServerError> {
for factory in factories {
self.register_tool(factory)?;
}
Ok(())
}
/// Get a tool by name for hook contexts.
pub fn get_tool(&self, name: &str) -> Option<(ToolMeta, Arc<dyn Tool>)> {
let guard = self.tools.lock().unwrap_or_else(|e| e.into_inner());
guard.get(name).map(|(meta, tool)| (meta.clone(), Arc::clone(tool)))
}
/// Execute a tool by name.
pub async fn call_tool(&self, name: &str, input_json: &str) -> Result<String, ToolServerError> {
let tool = {
let guard = self.tools.lock().unwrap_or_else(|e| e.into_inner());
let (_, tool) = guard
.get(name)
.ok_or_else(|| ToolServerError::ToolNotFound(name.to_string()))?;
Arc::clone(tool)
};
tool.execute(input_json)
.await
.map_err(|e| ToolServerError::ToolExecution(e.to_string()))
}
/// Build deterministic tool definitions sorted by tool name.
pub fn tool_definitions_sorted(&self) -> Vec<LlmToolDefinition> {
let guard = self.tools.lock().unwrap_or_else(|e| e.into_inner());
let mut defs: Vec<_> = guard
.values()
.map(|(meta, _)| {
LlmToolDefinition::new(&meta.name)
.description(&meta.description)
.input_schema(meta.input_schema.clone())
})
.collect();
defs.sort_by(|a, b| a.name.cmp(&b.name));
defs
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use async_trait::async_trait;
use serde_json::json;
use super::*;
use crate::tool::{Tool, ToolDefinition, ToolError, ToolMeta};
struct EchoTool;
#[async_trait]
impl Tool for EchoTool {
async fn execute(&self, input_json: &str) -> Result<String, ToolError> {
Ok(input_json.to_string())
}
}
fn def(name: &'static str) -> ToolDefinition {
Arc::new(move || {
(
ToolMeta::new(name)
.description(format!("desc-{name}"))
.input_schema(json!({"type":"object"})),
Arc::new(EchoTool) as Arc<dyn Tool>,
)
})
}
#[test]
fn register_duplicate_name_fails() {
let handle = ToolServer::new().handle();
handle.register_tool(def("alpha")).expect("first register");
let err = handle
.register_tool(def("alpha"))
.expect_err("duplicate should fail");
assert_eq!(err, ToolServerError::DuplicateName("alpha".to_string()));
}
#[tokio::test]
async fn call_tool_success_and_not_found() {
let handle = ToolServer::new().handle();
handle.register_tool(def("echo")).expect("register");
let out = handle.call_tool("echo", r#"{"x":1}"#).await.expect("call");
assert_eq!(out, r#"{"x":1}"#);
let err = handle
.call_tool("missing", "{}")
.await
.expect_err("missing tool");
assert_eq!(err, ToolServerError::ToolNotFound("missing".to_string()));
}
#[test]
fn tool_definitions_are_sorted() {
let handle = ToolServer::new().handle();
handle.register_tool(def("zeta")).expect("register zeta");
handle.register_tool(def("alpha")).expect("register alpha");
handle.register_tool(def("beta")).expect("register beta");
let names: Vec<_> = handle
.tool_definitions_sorted()
.into_iter()
.map(|d| d.name)
.collect();
assert_eq!(names, vec!["alpha", "beta", "zeta"]);
}
}
File diff suppressed because it is too large Load Diff
-6
View File
@@ -1,6 +0,0 @@
#[test]
fn compile_fail_state_constraints() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/cache_locked_register_tool.rs");
t.compile_fail("tests/ui/tool_server_handle_register_tool.rs");
}
@@ -1,11 +0,0 @@
use llm_worker::Worker;
use llm_worker::llm_client::providers::ollama::OllamaClient;
use std::sync::Arc;
fn main() {
let client = OllamaClient::new("dummy-model");
let worker = Worker::new(client);
let mut locked = worker.lock();
let def: llm_worker::tool::ToolDefinition = Arc::new(|| panic!("unused"));
let _ = locked.register_tool(def);
}
@@ -1,8 +0,0 @@
error[E0599]: no method named `register_tool` found for struct `Worker<OllamaClient, CacheLocked>` in the current scope
--> tests/ui/cache_locked_register_tool.rs:10:20
|
10 | let _ = locked.register_tool(def);
| ^^^^^^^^^^^^^ method not found in `Worker<OllamaClient, CacheLocked>`
|
= note: the method was found for
- `Worker<C>`
@@ -1,11 +0,0 @@
use llm_worker::Worker;
use llm_worker::llm_client::providers::ollama::OllamaClient;
use std::sync::Arc;
fn main() {
let client = OllamaClient::new("dummy-model");
let worker = Worker::new(client);
let handle = worker.tool_server_handle();
let def: llm_worker::tool::ToolDefinition = Arc::new(|| panic!("unused"));
let _ = handle.register_tool(def);
}
@@ -1,13 +0,0 @@
error[E0624]: method `register_tool` is private
--> tests/ui/tool_server_handle_register_tool.rs:10:20
|
10 | let _ = handle.register_tool(def);
| ^^^^^^^^^^^^^ private method
|
::: src/tool_server.rs
|
| / pub(crate) fn register_tool(
| | &self,
| | factory: WorkerToolDefinition,
| | ) -> Result<(), ToolServerError> {
| |____________________________________- private method defined here
-39
View File
@@ -1,39 +0,0 @@
use llm_worker::llm_client::providers::openai::OpenAIClient;
use llm_worker::{Worker, WorkerError};
#[test]
fn test_openai_top_k_warning() {
// Create client with dummy key (validate_config doesn't make network calls, so safe)
let client = OpenAIClient::new("dummy-key", "gpt-4o");
// Create Worker with top_k set (OpenAI doesn't support top_k)
let worker = Worker::new(client).top_k(50);
// Run validate()
let result = worker.validate();
// Verify error is returned and ConfigWarnings is included
match result {
Err(WorkerError::ConfigWarnings(warnings)) => {
assert_eq!(warnings.len(), 1);
assert_eq!(warnings[0].option_name, "top_k");
println!("Got expected warning: {}", warnings[0]);
}
Ok(_) => panic!("Should have returned validation error"),
Err(e) => panic!("Unexpected error type: {:?}", e),
}
}
#[test]
fn test_openai_valid_config() {
let client = OpenAIClient::new("dummy-key", "gpt-4o");
// Valid configuration (temperature only)
let worker = Worker::new(client).temperature(0.7);
// Run validate()
let result = worker.validate();
// Verify success
assert!(result.is_ok());
}
+14
View File
@@ -0,0 +1,14 @@
[package]
name = "worker-macros"
version = "0.1.0"
edition = "2024"
publish = false
[lib]
proc-macro = true
[dependencies]
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full"] }
worker-types = { path = "../worker-types" }
@@ -1,7 +1,7 @@
//! llm-worker-macros - Procedural macros for Tool generation //! worker-macros - Tool生成用手続きマクロ
//! //!
//! Provides `#[tool_registry]` and `#[tool]` macros to //! `#[tool_registry]` `#[tool]` マクロを提供し、
//! automatically generate `Tool` trait implementations from user-defined methods. //! ユーザー定義のメソッドから `Tool` トレイト実装を自動生成する。
use proc_macro::TokenStream; use proc_macro::TokenStream;
use quote::{format_ident, quote}; use quote::{format_ident, quote};
@@ -9,22 +9,22 @@ use syn::{
Attribute, FnArg, ImplItem, ItemImpl, Lit, Meta, Pat, ReturnType, Type, parse_macro_input, Attribute, FnArg, ImplItem, ItemImpl, Lit, Meta, Pat, ReturnType, Type, parse_macro_input,
}; };
/// Macro applied to an `impl` block that generates tools from methods marked with `#[tool]`. /// `impl` ブロックに付与し、内部の `#[tool]` 属性がついたメソッドからツールを生成するマクロ。
/// ///
/// # Example /// # Example
/// ```ignore /// ```ignore
/// #[tool_registry] /// #[tool_registry]
/// impl MyApp { /// impl MyApp {
/// /// Get user information /// /// ユーザー情報を取得する
/// /// Retrieves a user from the database by their ID. /// /// 指定されたIDのユーザーをDBから検索します。
/// #[tool] /// #[tool]
/// async fn get_user(&self, user_id: String) -> Result<User, Error> { ... } /// async fn get_user(&self, user_id: String) -> Result<User, Error> { ... }
/// } /// }
/// ``` /// ```
/// ///
/// This generates: /// これにより以下が生成されます:
/// - `GetUserArgs` struct (for arguments) /// - `GetUserArgs` 構造体(引数用)
/// - `Tool_get_user` struct (Tool wrapper) /// - `Tool_get_user` 構造体(Toolラッパー)
/// - `impl Tool for Tool_get_user` /// - `impl Tool for Tool_get_user`
/// - `impl MyApp { fn get_user_tool(&self) -> Tool_get_user }` /// - `impl MyApp { fn get_user_tool(&self) -> Tool_get_user }`
#[proc_macro_attribute] #[proc_macro_attribute]
@@ -36,14 +36,14 @@ pub fn tool_registry(_attr: TokenStream, item: TokenStream) -> TokenStream {
for item in &mut impl_block.items { for item in &mut impl_block.items {
if let ImplItem::Fn(method) = item { if let ImplItem::Fn(method) = item {
// Look for #[tool] attribute // #[tool] 属性を探す
let mut is_tool = false; let mut is_tool = false;
// Iterate through attributes to check for tool and remove it // 属性を走査してtoolがあるか確認し、削除する
method.attrs.retain(|attr| { method.attrs.retain(|attr| {
if attr.path().is_ident("tool") { if attr.path().is_ident("tool") {
is_tool = true; is_tool = true;
false // Remove the attribute false // 属性を削除
} else { } else {
true true
} }
@@ -65,7 +65,7 @@ pub fn tool_registry(_attr: TokenStream, item: TokenStream) -> TokenStream {
TokenStream::from(expanded) TokenStream::from(expanded)
} }
/// Extract description from doc comments /// ドキュメントコメントから説明文を抽出
fn extract_doc_comment(attrs: &[Attribute]) -> String { fn extract_doc_comment(attrs: &[Attribute]) -> String {
let mut lines = Vec::new(); let mut lines = Vec::new();
@@ -75,7 +75,7 @@ fn extract_doc_comment(attrs: &[Attribute]) -> String {
if let syn::Expr::Lit(expr_lit) = &meta.value { if let syn::Expr::Lit(expr_lit) = &meta.value {
if let Lit::Str(lit_str) = &expr_lit.lit { if let Lit::Str(lit_str) = &expr_lit.lit {
let line = lit_str.value(); let line = lit_str.value();
// Remove only the leading space (after ///) // 先頭の空白を1つだけ除去(/// の後のスペース)
let trimmed = line.strip_prefix(' ').unwrap_or(&line); let trimmed = line.strip_prefix(' ').unwrap_or(&line);
lines.push(trimmed.to_string()); lines.push(trimmed.to_string());
} }
@@ -87,7 +87,7 @@ fn extract_doc_comment(attrs: &[Attribute]) -> String {
lines.join("\n") lines.join("\n")
} }
/// Extract description from #[description = "..."] attribute /// #[description = "..."] 属性から説明を抽出
fn extract_description_attr(attrs: &[syn::Attribute]) -> Option<String> { fn extract_description_attr(attrs: &[syn::Attribute]) -> Option<String> {
for attr in attrs { for attr in attrs {
if attr.path().is_ident("description") { if attr.path().is_ident("description") {
@@ -103,19 +103,19 @@ fn extract_description_attr(attrs: &[syn::Attribute]) -> Option<String> {
None None
} }
/// Generate Tool implementation from a method /// メソッドからTool実装を生成
fn generate_tool_impl(self_ty: &Type, method: &syn::ImplItemFn) -> proc_macro2::TokenStream { fn generate_tool_impl(self_ty: &Type, method: &syn::ImplItemFn) -> proc_macro2::TokenStream {
let sig = &method.sig; let sig = &method.sig;
let method_name = &sig.ident; let method_name = &sig.ident;
let tool_name = method_name.to_string(); let tool_name = method_name.to_string();
// Generate struct names (convert to PascalCase) // 構造体名を生成(PascalCase変換)
let pascal_name = to_pascal_case(&method_name.to_string()); let pascal_name = to_pascal_case(&method_name.to_string());
let tool_struct_name = format_ident!("Tool{}", pascal_name); let tool_struct_name = format_ident!("Tool{}", pascal_name);
let args_struct_name = format_ident!("{}Args", pascal_name); let args_struct_name = format_ident!("{}Args", pascal_name);
let definition_name = format_ident!("{}_definition", method_name); let factory_name = format_ident!("{}_tool", method_name);
// Get description from doc comments // ドキュメントコメントから説明を取得
let description = extract_doc_comment(&method.attrs); let description = extract_doc_comment(&method.attrs);
let description = if description.is_empty() { let description = if description.is_empty() {
format!("Tool: {}", tool_name) format!("Tool: {}", tool_name)
@@ -123,7 +123,7 @@ fn generate_tool_impl(self_ty: &Type, method: &syn::ImplItemFn) -> proc_macro2::
description description
}; };
// Parse arguments (excluding self) // 引数を解析(selfを除く)
let args: Vec<_> = sig let args: Vec<_> = sig
.inputs .inputs
.iter() .iter()
@@ -131,12 +131,12 @@ fn generate_tool_impl(self_ty: &Type, method: &syn::ImplItemFn) -> proc_macro2::
if let FnArg::Typed(pat_type) = arg { if let FnArg::Typed(pat_type) = arg {
Some(pat_type) Some(pat_type)
} else { } else {
None // Exclude self None // selfを除外
} }
}) })
.collect(); .collect();
// Generate argument struct fields // 引数構造体のフィールドを生成
let arg_fields: Vec<_> = args let arg_fields: Vec<_> = args
.iter() .iter()
.map(|pat_type| { .map(|pat_type| {
@@ -144,14 +144,14 @@ fn generate_tool_impl(self_ty: &Type, method: &syn::ImplItemFn) -> proc_macro2::
let ty = &pat_type.ty; let ty = &pat_type.ty;
let desc = extract_description_attr(&pat_type.attrs); let desc = extract_description_attr(&pat_type.attrs);
// Extract identifier from pattern // パターンから識別子を抽出
let field_name = if let Pat::Ident(pat_ident) = pat.as_ref() { let field_name = if let Pat::Ident(pat_ident) = pat.as_ref() {
&pat_ident.ident &pat_ident.ident
} else { } else {
panic!("Only simple identifiers are supported for tool arguments"); panic!("Only simple identifiers are supported for tool arguments");
}; };
// Convert #[description] to schemars doc if present // #[description] があればschemarsdocに変換
if let Some(desc_str) = desc { if let Some(desc_str) = desc {
quote! { quote! {
#[schemars(description = #desc_str)] #[schemars(description = #desc_str)]
@@ -165,7 +165,7 @@ fn generate_tool_impl(self_ty: &Type, method: &syn::ImplItemFn) -> proc_macro2::
}) })
.collect(); .collect();
// Code to expand arguments in execute // execute内で引数を展開するコード
let arg_names: Vec<_> = args let arg_names: Vec<_> = args
.iter() .iter()
.map(|pat_type| { .map(|pat_type| {
@@ -178,22 +178,22 @@ fn generate_tool_impl(self_ty: &Type, method: &syn::ImplItemFn) -> proc_macro2::
}) })
.collect(); .collect();
// Check if method is async // メソッドが非同期かどうか
let is_async = sig.asyncness.is_some(); let is_async = sig.asyncness.is_some();
// Parse return type and determine if Result // 戻り値の型を解析してResult判定
let awaiter = if is_async { let awaiter = if is_async {
quote! { .await } quote! { .await }
} else { } else {
quote! {} quote! {}
}; };
// Determine if return type is Result // 戻り値がResultかどうかを判定
let result_handling = if is_result_type(&sig.output) { let result_handling = if is_result_type(&sig.output) {
quote! { quote! {
match result { match result {
Ok(val) => Ok(format!("{:?}", val)), Ok(val) => Ok(format!("{:?}", val)),
Err(e) => Err(::llm_worker::tool::ToolError::ExecutionFailed(format!("{}", e))), Err(e) => Err(worker_types::ToolError::ExecutionFailed(format!("{}", e))),
} }
} }
} else { } else {
@@ -202,7 +202,7 @@ fn generate_tool_impl(self_ty: &Type, method: &syn::ImplItemFn) -> proc_macro2::
} }
}; };
// Create empty Args struct if no arguments // 引数がない場合は空のArgs構造体を作成
let args_struct_def = if arg_fields.is_empty() { let args_struct_def = if arg_fields.is_empty() {
quote! { quote! {
#[derive(serde::Deserialize, schemars::JsonSchema)] #[derive(serde::Deserialize, schemars::JsonSchema)]
@@ -217,10 +217,10 @@ fn generate_tool_impl(self_ty: &Type, method: &syn::ImplItemFn) -> proc_macro2::
} }
}; };
// Execute body handling for no arguments case // 引数がない場合のexecute処理
let execute_body = if args.is_empty() { let execute_body = if args.is_empty() {
quote! { quote! {
// Allow empty JSON object even with no arguments // 引数なしでも空のJSONオブジェクトを許容
let _: #args_struct_name = serde_json::from_str(input_json) let _: #args_struct_name = serde_json::from_str(input_json)
.unwrap_or(#args_struct_name {}); .unwrap_or(#args_struct_name {});
@@ -230,7 +230,7 @@ fn generate_tool_impl(self_ty: &Type, method: &syn::ImplItemFn) -> proc_macro2::
} else { } else {
quote! { quote! {
let args: #args_struct_name = serde_json::from_str(input_json) let args: #args_struct_name = serde_json::from_str(input_json)
.map_err(|e| ::llm_worker::tool::ToolError::InvalidArgument(e.to_string()))?; .map_err(|e| worker_types::ToolError::InvalidArgument(e.to_string()))?;
let result = self.ctx.#method_name(#(#arg_names),*)#awaiter; let result = self.ctx.#method_name(#(#arg_names),*)#awaiter;
#result_handling #result_handling
@@ -246,36 +246,41 @@ fn generate_tool_impl(self_ty: &Type, method: &syn::ImplItemFn) -> proc_macro2::
} }
#[async_trait::async_trait] #[async_trait::async_trait]
impl ::llm_worker::tool::Tool for #tool_struct_name { impl worker_types::Tool for #tool_struct_name {
async fn execute(&self, input_json: &str) -> Result<String, ::llm_worker::tool::ToolError> { fn name(&self) -> &str {
#tool_name
}
fn description(&self) -> &str {
#description
}
fn input_schema(&self) -> serde_json::Value {
let schema = schemars::schema_for!(#args_struct_name);
serde_json::to_value(schema).unwrap_or(serde_json::json!({}))
}
async fn execute(&self, input_json: &str) -> Result<String, worker_types::ToolError> {
#execute_body #execute_body
} }
} }
impl #self_ty { impl #self_ty {
/// Get ToolDefinition (for registering with Worker) pub fn #factory_name(&self) -> #tool_struct_name {
pub fn #definition_name(&self) -> ::llm_worker::tool::ToolDefinition { #tool_struct_name {
let ctx = self.clone(); ctx: self.clone()
::std::sync::Arc::new(move || { }
let schema = schemars::schema_for!(#args_struct_name);
let meta = ::llm_worker::tool::ToolMeta::new(#tool_name)
.description(#description)
.input_schema(serde_json::to_value(schema).unwrap_or(serde_json::json!({})));
let tool: ::std::sync::Arc<dyn ::llm_worker::tool::Tool> =
::std::sync::Arc::new(#tool_struct_name { ctx: ctx.clone() });
(meta, tool)
})
} }
} }
} }
} }
/// Determine if return type is Result /// 戻り値の型がResultかどうかを判定
fn is_result_type(return_type: &ReturnType) -> bool { fn is_result_type(return_type: &ReturnType) -> bool {
match return_type { match return_type {
ReturnType::Default => false, ReturnType::Default => false,
ReturnType::Type(_, ty) => { ReturnType::Type(_, ty) => {
// For Type::Path, check if last segment is "Result" // Type::Pathの場合、最後のセグメントが"Result"かチェック
if let Type::Path(type_path) = ty.as_ref() { if let Type::Path(type_path) = ty.as_ref() {
if let Some(segment) = type_path.path.segments.last() { if let Some(segment) = type_path.path.segments.last() {
return segment.ident == "Result"; return segment.ident == "Result";
@@ -286,7 +291,7 @@ fn is_result_type(return_type: &ReturnType) -> bool {
} }
} }
/// Convert snake_case to PascalCase /// snake_case PascalCase に変換
fn to_pascal_case(s: &str) -> String { fn to_pascal_case(s: &str) -> String {
s.split('_') s.split('_')
.map(|part| { .map(|part| {
@@ -299,20 +304,20 @@ fn to_pascal_case(s: &str) -> String {
.collect() .collect()
} }
/// Marker attribute. Does nothing here as it's processed by `tool_registry`. /// マーカー属性。`tool_registry` によって処理されるため、ここでは何もしない。
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn tool(_attr: TokenStream, item: TokenStream) -> TokenStream { pub fn tool(_attr: TokenStream, item: TokenStream) -> TokenStream {
item item
} }
/// Marker for argument attributes. Interpreted by `tool_registry` during parsing. /// 引数属性用のマーカー。パース時に`tool_registry`で解釈される。
/// ///
/// # Example /// # Example
/// ```ignore /// ```ignore
/// #[tool] /// #[tool]
/// async fn get_user( /// async fn get_user(
/// &self, /// &self,
/// #[description = "The ID of the user to retrieve"] user_id: String /// #[description = "取得したいユーザーのID"] user_id: String
/// ) -> Result<User, Error> { ... } /// ) -> Result<User, Error> { ... }
/// ``` /// ```
#[proc_macro_attribute] #[proc_macro_attribute]
+12
View File
@@ -0,0 +1,12 @@
[package]
name = "worker-types"
version = "0.1.0"
edition = "2024"
publish = false
[dependencies]
async-trait = "0.1.89"
schemars = "1.2.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "2.0.17"
@@ -1,6 +1,7 @@
//! LLMクライアント層のイベント型 //! イベント型
//! //!
//! LLMプロバイダからのストリーミングレスポンスを表現するイベント型。 //! LLMからのストリーミングレスポンスを表現するイベント型。
//! Timeline層がこのイベントを受信し、ハンドラにディスパッチします。
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -1,22 +1,22 @@
//! Handler/Kind Types //! Handler/Kind
//! //!
//! Traits for processing events in the Timeline layer. //! Timeline層でイベントを処理するためのトレイト。
//! By implementing custom handlers and registering them with Timeline, //! カスタムハンドラを実装してTimelineに登録することで、
//! you can receive stream events. //! ストリームイベントを受信できます。
use crate::timeline::event::*; use crate::event::*;
// ============================================================================= // =============================================================================
// Kind Trait // Kind Trait
// ============================================================================= // =============================================================================
/// Marker trait defining event types /// イベント種別を定義するマーカートレイト
/// ///
/// Each Kind specifies its corresponding event type. /// 各Kindは対応するイベント型を指定します。
/// Handlers are implemented for this Kind, and multiple Handlers /// HandlerはこのKindに対して実装され、同じKindに対して
/// with different Scope types can be registered for the same Kind. /// 異なるScope型を持つ複数のHandlerを登録できます。
pub trait Kind { pub trait Kind {
/// Event type corresponding to this Kind /// このKindに対応するイベント型
type Event; type Event;
} }
@@ -24,22 +24,22 @@ pub trait Kind {
// Handler Trait // Handler Trait
// ============================================================================= // =============================================================================
/// Handler trait for processing events /// イベントを処理するハンドラトレイト
/// ///
/// Defines event processing for a specific `Kind`. /// 特定の`Kind`に対するイベント処理を定義します。
/// `Scope` is state held during the block's lifecycle. /// `Scope`はブロックのライフサイクル中に保持される状態です。
/// ///
/// # Examples /// # Examples
/// ///
/// ```ignore /// ```ignore
/// use llm_worker::timeline::{Handler, TextBlockEvent, TextBlockKind}; /// use worker::{Handler, TextBlockKind, TextBlockEvent};
/// ///
/// struct TextCollector { /// struct TextCollector {
/// texts: Vec<String>, /// texts: Vec<String>,
/// } /// }
/// ///
/// impl Handler<TextBlockKind> for TextCollector { /// impl Handler<TextBlockKind> for TextCollector {
/// type Scope = String; // Buffer per block /// type Scope = String; // ブロックごとのバッファ
/// ///
/// fn on_event(&mut self, buffer: &mut String, event: &TextBlockEvent) { /// fn on_event(&mut self, buffer: &mut String, event: &TextBlockEvent) {
/// match event { /// match event {
@@ -53,13 +53,13 @@ pub trait Kind {
/// } /// }
/// ``` /// ```
pub trait Handler<K: Kind> { pub trait Handler<K: Kind> {
/// Handler-specific scope type /// Handler固有のスコープ型
/// ///
/// Generated with `Default::default()` at block start, /// ブロック開始時に`Default::default()`で生成され、
/// and destroyed at block end. /// ブロック終了時に破棄されます。
type Scope: Default; type Scope: Default;
/// Process the event /// イベントを処理する
fn on_event(&mut self, scope: &mut Self::Scope, event: &K::Event); fn on_event(&mut self, scope: &mut Self::Scope, event: &K::Event);
} }
@@ -67,25 +67,25 @@ pub trait Handler<K: Kind> {
// Meta Kind Definitions // Meta Kind Definitions
// ============================================================================= // =============================================================================
/// Usage Kind - for usage events /// Usage Kind - 使用量イベント用
pub struct UsageKind; pub struct UsageKind;
impl Kind for UsageKind { impl Kind for UsageKind {
type Event = UsageEvent; type Event = UsageEvent;
} }
/// Ping Kind - for ping events /// Ping Kind - Pingイベント用
pub struct PingKind; pub struct PingKind;
impl Kind for PingKind { impl Kind for PingKind {
type Event = PingEvent; type Event = PingEvent;
} }
/// Status Kind - for status events /// Status Kind - ステータスイベント用
pub struct StatusKind; pub struct StatusKind;
impl Kind for StatusKind { impl Kind for StatusKind {
type Event = StatusEvent; type Event = StatusEvent;
} }
/// Error Kind - for error events /// Error Kind - エラーイベント用
pub struct ErrorKind; pub struct ErrorKind;
impl Kind for ErrorKind { impl Kind for ErrorKind {
type Event = ErrorEvent; type Event = ErrorEvent;
@@ -95,13 +95,13 @@ impl Kind for ErrorKind {
// Block Kind Definitions // Block Kind Definitions
// ============================================================================= // =============================================================================
/// TextBlock Kind - for text blocks /// TextBlock Kind - テキストブロック用
pub struct TextBlockKind; pub struct TextBlockKind;
impl Kind for TextBlockKind { impl Kind for TextBlockKind {
type Event = TextBlockEvent; type Event = TextBlockEvent;
} }
/// Text block events /// テキストブロックのイベント
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum TextBlockEvent { pub enum TextBlockEvent {
Start(TextBlockStart), Start(TextBlockStart),
@@ -120,13 +120,13 @@ pub struct TextBlockStop {
pub stop_reason: Option<StopReason>, pub stop_reason: Option<StopReason>,
} }
/// ThinkingBlock Kind - for thinking blocks /// ThinkingBlock Kind - 思考ブロック用
pub struct ThinkingBlockKind; pub struct ThinkingBlockKind;
impl Kind for ThinkingBlockKind { impl Kind for ThinkingBlockKind {
type Event = ThinkingBlockEvent; type Event = ThinkingBlockEvent;
} }
/// Thinking block events /// 思考ブロックのイベント
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum ThinkingBlockEvent { pub enum ThinkingBlockEvent {
Start(ThinkingBlockStart), Start(ThinkingBlockStart),
@@ -144,17 +144,17 @@ pub struct ThinkingBlockStop {
pub index: usize, pub index: usize,
} }
/// ToolUseBlock Kind - for tool use blocks /// ToolUseBlock Kind - ツール使用ブロック用
pub struct ToolUseBlockKind; pub struct ToolUseBlockKind;
impl Kind for ToolUseBlockKind { impl Kind for ToolUseBlockKind {
type Event = ToolUseBlockEvent; type Event = ToolUseBlockEvent;
} }
/// Tool use block events /// ツール使用ブロックのイベント
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum ToolUseBlockEvent { pub enum ToolUseBlockEvent {
Start(ToolUseBlockStart), Start(ToolUseBlockStart),
/// JSON substring of tool arguments /// ツール引数のJSON部分文字列
InputJsonDelta(String), InputJsonDelta(String),
Stop(ToolUseBlockStop), Stop(ToolUseBlockStop),
} }
+181
View File
@@ -0,0 +1,181 @@
//! Hook関連の型定義
//!
//! Worker層でのターン制御・介入に使用される型
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;
// =============================================================================
// Control Flow Types
// =============================================================================
/// Hook処理の制御フロー
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ControlFlow {
/// 処理を続行
Continue,
/// 現在の処理をスキップ(Tool実行など)
Skip,
/// 処理を中断
Abort(String),
}
/// ターン終了時の判定結果
#[derive(Debug, Clone)]
pub enum TurnResult {
/// ターンを終了
Finish,
/// メッセージを追加してターン継続(自己修正など)
ContinueWithMessages(Vec<crate::Message>),
}
// =============================================================================
// Tool Call / Result Types
// =============================================================================
/// ツール呼び出し情報
///
/// LLMからのToolUseブロックを表現し、Hook処理で改変可能
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
/// ツール呼び出しID(レスポンスとの紐付けに使用)
pub id: String,
/// ツール名
pub name: String,
/// 入力引数(JSON
pub input: Value,
}
/// ツール実行結果
///
/// ツール実行後の結果を表現し、Hook処理で改変可能
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResult {
/// 対応するツール呼び出しID
pub tool_use_id: String,
/// 結果コンテンツ
pub content: String,
/// エラーかどうか
#[serde(default)]
pub is_error: bool,
}
impl ToolResult {
/// 成功結果を作成
pub fn success(tool_use_id: impl Into<String>, content: impl Into<String>) -> Self {
Self {
tool_use_id: tool_use_id.into(),
content: content.into(),
is_error: false,
}
}
/// エラー結果を作成
pub fn error(tool_use_id: impl Into<String>, content: impl Into<String>) -> Self {
Self {
tool_use_id: tool_use_id.into(),
content: content.into(),
is_error: true,
}
}
}
// =============================================================================
// Hook Error
// =============================================================================
/// Hookエラー
#[derive(Debug, Error)]
pub enum HookError {
/// 処理が中断された
#[error("Aborted: {0}")]
Aborted(String),
/// 内部エラー
#[error("Hook error: {0}")]
Internal(String),
}
// =============================================================================
// WorkerHook Trait
// =============================================================================
/// ターンの進行・ツール実行に介入するためのトレイト
///
/// Hookを使うと、メッセージ送信前、ツール実行前後、ターン終了時に
/// 処理を挟んだり、実行をキャンセルしたりできます。
///
/// # Examples
///
/// ```ignore
/// use worker::{WorkerHook, ControlFlow, HookError, ToolCall, TurnResult, Message};
///
/// struct ValidationHook;
///
/// #[async_trait::async_trait]
/// impl WorkerHook for ValidationHook {
/// async fn before_tool_call(&self, call: &mut ToolCall) -> Result<ControlFlow, HookError> {
/// // 危険なツールをブロック
/// if call.name == "delete_all" {
/// return Ok(ControlFlow::Skip);
/// }
/// Ok(ControlFlow::Continue)
/// }
///
/// async fn on_turn_end(&self, messages: &[Message]) -> Result<TurnResult, HookError> {
/// // 条件を満たさなければ追加メッセージで継続
/// if messages.len() < 3 {
/// return Ok(TurnResult::ContinueWithMessages(vec![
/// Message::user("Please elaborate.")
/// ]));
/// }
/// Ok(TurnResult::Finish)
/// }
/// }
/// ```
///
/// # デフォルト実装
///
/// すべてのメソッドにはデフォルト実装があり、何も行わず`Continue`を返します。
/// 必要なメソッドのみオーバーライドしてください。
#[async_trait]
pub trait WorkerHook: Send + Sync {
/// メッセージ送信前に呼ばれる
///
/// リクエストに含まれるメッセージリストを参照・改変できます。
/// `ControlFlow::Abort`を返すとターンが中断されます。
async fn on_message_send(
&self,
_context: &mut Vec<crate::Message>,
) -> Result<ControlFlow, HookError> {
Ok(ControlFlow::Continue)
}
/// ツール実行前に呼ばれる
///
/// ツール呼び出しの引数を書き換えたり、実行をスキップしたりできます。
/// `ControlFlow::Skip`を返すとこのツールの実行がスキップされます。
async fn before_tool_call(&self, _tool_call: &mut ToolCall) -> Result<ControlFlow, HookError> {
Ok(ControlFlow::Continue)
}
/// ツール実行後に呼ばれる
///
/// ツールの実行結果を書き換えたり、隠蔽したりできます。
async fn after_tool_call(
&self,
_tool_result: &mut ToolResult,
) -> Result<ControlFlow, HookError> {
Ok(ControlFlow::Continue)
}
/// ターン終了時に呼ばれる
///
/// 生成されたメッセージを検査し、必要なら追加メッセージで継続を指示できます。
/// `TurnResult::ContinueWithMessages`を返すと、指定したメッセージを追加して
/// 次のターンに進みます。
async fn on_turn_end(&self, _messages: &[crate::Message]) -> Result<TurnResult, HookError> {
Ok(TurnResult::Finish)
}
}
+24
View File
@@ -0,0 +1,24 @@
//! worker-types - LLMワーカーの型定義
//!
//! このクレートは`worker`クレートで使用される型を提供します。
//! 通常は直接使用せず、`worker`クレート経由で利用してください。
//!
//! ```ignore
//! use worker::{Event, Message, Tool, WorkerHook};
//! ```
mod event;
mod handler;
mod hook;
mod message;
mod state;
mod subscriber;
mod tool;
pub use event::*;
pub use handler::*;
pub use hook::*;
pub use message::*;
pub use state::*;
pub use subscriber::*;
pub use tool::*;
+116
View File
@@ -0,0 +1,116 @@
//! メッセージ型
//!
//! LLMとの会話で使用されるメッセージ構造。
//! [`Message::user`]や[`Message::assistant`]で簡単に作成できます。
use serde::{Deserialize, Serialize};
/// メッセージのロール
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
/// ユーザー
User,
/// アシスタント
Assistant,
}
/// 会話のメッセージ
///
/// # Examples
///
/// ```ignore
/// use worker::Message;
///
/// // ユーザーメッセージ
/// let user_msg = Message::user("Hello!");
///
/// // アシスタントメッセージ
/// let assistant_msg = Message::assistant("Hi there!");
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
/// ロール
pub role: Role,
/// コンテンツ
pub content: MessageContent,
}
/// メッセージコンテンツ
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum MessageContent {
/// テキストコンテンツ
Text(String),
/// ツール結果
ToolResult {
tool_use_id: String,
content: String,
},
/// 複合コンテンツ (テキスト + ツール使用等)
Parts(Vec<ContentPart>),
}
/// コンテンツパーツ
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ContentPart {
/// テキスト
#[serde(rename = "text")]
Text { text: String },
/// ツール使用
#[serde(rename = "tool_use")]
ToolUse {
id: String,
name: String,
input: serde_json::Value,
},
/// ツール結果
#[serde(rename = "tool_result")]
ToolResult {
tool_use_id: String,
content: String,
},
}
impl Message {
/// ユーザーメッセージを作成
///
/// # Examples
///
/// ```ignore
/// use worker::Message;
/// let msg = Message::user("こんにちは");
/// ```
pub fn user(content: impl Into<String>) -> Self {
Self {
role: Role::User,
content: MessageContent::Text(content.into()),
}
}
/// アシスタントメッセージを作成
///
/// 通常はWorker内部で自動生成されますが、
/// 履歴の初期化などで手動作成も可能です。
pub fn assistant(content: impl Into<String>) -> Self {
Self {
role: Role::Assistant,
content: MessageContent::Text(content.into()),
}
}
/// ツール結果メッセージを作成
///
/// Worker内部でツール実行後に自動生成されます。
/// 通常は直接作成する必要はありません。
pub fn tool_result(tool_use_id: impl Into<String>, content: impl Into<String>) -> Self {
Self {
role: Role::User,
content: MessageContent::ToolResult {
tool_use_id: tool_use_id.into(),
content: content.into(),
},
}
}
}
+60
View File
@@ -0,0 +1,60 @@
//! Worker状態
//!
//! Type-stateパターンによるキャッシュ保護のための状態マーカー型。
//! Workerは`Mutable` → `Locked`の状態遷移を持ちます。
/// Worker状態を表すマーカートレイト
///
/// このトレイトはシールされており、外部から実装することはできません。
pub trait WorkerState: private::Sealed + Send + Sync + 'static {}
mod private {
pub trait Sealed {}
}
/// 編集可能状態
///
/// この状態では以下の操作が可能です:
/// - システムプロンプトの設定・変更
/// - メッセージ履歴の編集(追加、削除、クリア)
/// - ツール・Hookの登録
///
/// `Worker::lock()`により[`Locked`]状態へ遷移できます。
///
/// # Examples
///
/// ```ignore
/// use worker::Worker;
///
/// let mut worker = Worker::new(client)
/// .system_prompt("You are helpful.");
///
/// // 履歴を編集可能
/// worker.push_message(Message::user("Hello"));
/// worker.clear_history();
///
/// // ロックして保護状態へ
/// let locked = worker.lock();
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct Mutable;
impl private::Sealed for Mutable {}
impl WorkerState for Mutable {}
/// ロック状態(キャッシュ保護)
///
/// この状態では以下の制限があります:
/// - システムプロンプトの変更不可
/// - 既存メッセージ履歴の変更不可(末尾への追記のみ)
///
/// LLM APIのKVキャッシュヒットを保証するため、
/// 実行時にはこの状態の使用が推奨されます。
///
/// `Worker::unlock()`により[`Mutable`]状態へ戻せますが、
/// キャッシュ保護が解除されることに注意してください。
#[derive(Debug, Clone, Copy, Default)]
pub struct Locked;
impl private::Sealed for Locked {}
impl WorkerState for Locked {}
+131
View File
@@ -0,0 +1,131 @@
//! イベント購読
//!
//! LLMからのストリーミングイベントをリアルタイムで受信するためのトレイト。
//! UIへのストリーム表示やプログレス表示に使用します。
use crate::{ErrorEvent, StatusEvent, TextBlockEvent, ToolCall, ToolUseBlockEvent, UsageEvent};
// =============================================================================
// WorkerSubscriber Trait
// =============================================================================
/// LLMからのストリーミングイベントを購読するトレイト
///
/// Workerに登録すると、テキスト生成やツール呼び出しのイベントを
/// リアルタイムで受信できます。UIへのストリーム表示に最適です。
///
/// # 受信できるイベント
///
/// - **ブロックイベント**: テキスト、ツール使用(スコープ付き)
/// - **メタイベント**: 使用量、ステータス、エラー
/// - **完了イベント**: テキスト完了、ツール呼び出し完了
/// - **ターン制御**: ターン開始、ターン終了
///
/// # Examples
///
/// ```ignore
/// use worker::{WorkerSubscriber, TextBlockEvent};
///
/// struct StreamPrinter;
///
/// impl WorkerSubscriber for StreamPrinter {
/// type TextBlockScope = ();
/// type ToolUseBlockScope = ();
///
/// fn on_text_block(&mut self, _: &mut (), event: &TextBlockEvent) {
/// if let TextBlockEvent::Delta(text) = event {
/// print!("{}", text); // リアルタイム出力
/// }
/// }
///
/// fn on_text_complete(&mut self, text: &str) {
/// println!("\n--- Complete: {} chars ---", text.len());
/// }
/// }
///
/// // Workerに登録
/// worker.subscribe(StreamPrinter);
/// ```
pub trait WorkerSubscriber: Send {
// =========================================================================
// スコープ型(ブロックイベント用)
// =========================================================================
/// テキストブロック処理用のスコープ型
///
/// ブロック開始時にDefault::default()で生成され、
/// ブロック終了時に破棄される。
type TextBlockScope: Default + Send;
/// ツール使用ブロック処理用のスコープ型
type ToolUseBlockScope: Default + Send;
// =========================================================================
// ブロックイベント(スコープ管理あり)
// =========================================================================
/// テキストブロックイベント
///
/// Start/Delta/Stopのライフサイクルを持つ。
/// scopeはブロック開始時に生成され、終了時に破棄される。
#[allow(unused_variables)]
fn on_text_block(&mut self, scope: &mut Self::TextBlockScope, event: &TextBlockEvent) {}
/// ツール使用ブロックイベント
///
/// Start/InputJsonDelta/Stopのライフサイクルを持つ。
#[allow(unused_variables)]
fn on_tool_use_block(
&mut self,
scope: &mut Self::ToolUseBlockScope,
event: &ToolUseBlockEvent,
) {
}
// =========================================================================
// 単発イベント(スコープ不要)
// =========================================================================
/// 使用量イベント
#[allow(unused_variables)]
fn on_usage(&mut self, event: &UsageEvent) {}
/// ステータスイベント
#[allow(unused_variables)]
fn on_status(&mut self, event: &StatusEvent) {}
/// エラーイベント
#[allow(unused_variables)]
fn on_error(&mut self, event: &ErrorEvent) {}
// =========================================================================
// 累積イベント(Worker層で追加)
// =========================================================================
/// テキスト完了イベント
///
/// テキストブロックが完了した時点で、累積されたテキスト全体が渡される。
/// ブロック処理後の最終結果を受け取るのに便利。
#[allow(unused_variables)]
fn on_text_complete(&mut self, text: &str) {}
/// ツール呼び出し完了イベント
///
/// ツール使用ブロックが完了した時点で、完全なToolCallが渡される。
#[allow(unused_variables)]
fn on_tool_call_complete(&mut self, call: &ToolCall) {}
// =========================================================================
// ターン制御
// =========================================================================
/// ターン開始時
///
/// `turn`は0から始まるターン番号。
#[allow(unused_variables)]
fn on_turn_start(&mut self, turn: usize) {}
/// ターン終了時
#[allow(unused_variables)]
fn on_turn_end(&mut self, turn: usize) {}
}
+90
View File
@@ -0,0 +1,90 @@
//! ツール定義
//!
//! LLMから呼び出し可能なツールを定義するためのトレイト。
//! 通常は`#[tool]`マクロを使用して自動実装します。
use async_trait::async_trait;
use serde_json::Value;
use thiserror::Error;
/// ツール実行時のエラー
#[derive(Debug, Error)]
pub enum ToolError {
/// 引数が不正
#[error("Invalid argument: {0}")]
InvalidArgument(String),
/// 実行に失敗
#[error("Execution failed: {0}")]
ExecutionFailed(String),
/// 内部エラー
#[error("Internal error: {0}")]
Internal(String),
}
/// LLMから呼び出し可能なツールを定義するトレイト
///
/// ツールはLLMが外部リソースにアクセスしたり、
/// 計算を実行したりするために使用します。
///
/// # 実装方法
///
/// 通常は`#[tool]`マクロを使用して自動実装します:
///
/// ```ignore
/// use worker::tool;
///
/// #[tool(description = "Search the web for information")]
/// async fn search(query: String) -> String {
/// // 検索処理
/// format!("Results for: {}", query)
/// }
/// ```
///
/// # 手動実装
///
/// ```ignore
/// use worker::{Tool, ToolError};
/// use serde_json::{json, Value};
///
/// struct MyTool;
///
/// #[async_trait::async_trait]
/// impl Tool for MyTool {
/// fn name(&self) -> &str { "my_tool" }
/// fn description(&self) -> &str { "My custom tool" }
/// fn input_schema(&self) -> Value {
/// json!({
/// "type": "object",
/// "properties": {
/// "query": { "type": "string" }
/// },
/// "required": ["query"]
/// })
/// }
/// async fn execute(&self, input: &str) -> Result<String, ToolError> {
/// Ok("result".to_string())
/// }
/// }
/// ```
#[async_trait]
pub trait Tool: Send + Sync {
/// ツール名(LLMが識別に使用)
fn name(&self) -> &str;
/// ツールの説明(LLMへのプロンプトに含まれる)
fn description(&self) -> &str;
/// 引数のJSON Schema
///
/// LLMはこのスキーマに従って引数を生成します。
fn input_schema(&self) -> Value;
/// ツールを実行する
///
/// # Arguments
/// * `input_json` - LLMが生成したJSON形式の引数
///
/// # Returns
/// 実行結果の文字列。この内容がLLMに返されます。
async fn execute(&self, input_json: &str) -> Result<String, ToolError>;
}
+24
View File
@@ -0,0 +1,24 @@
[package]
name = "worker"
version = "0.1.0"
edition = "2024"
[dependencies]
async-trait = "0.1.89"
eventsource-stream = "0.2.3"
futures = "0.3.31"
reqwest = { version = "0.13.1", features = ["stream", "json"] }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"
tokio = { version = "1.49.0", features = ["macros", "rt-multi-thread"] }
tracing = "0.1"
worker-macros = { path = "../worker-macros" }
worker-types = { path = "../worker-types" }
[dev-dependencies]
clap = { version = "4.5.54", features = ["derive", "env"] }
schemars = "1.2.0"
tempfile = "3.24.0"
dotenv = "0.15.0"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
@@ -1,18 +1,18 @@
//! Test fixture recording tool //! テストフィクスチャ記録ツール
//! //!
//! Records API responses for defined scenarios. //! 定義されたシナリオのAPIレスポンスを記録する。
//! //!
//! ## Usage //! ## 使用方法
//! //!
//! ```bash //! ```bash
//! # Show available scenarios //! # 利用可能なシナリオを表示
//! cargo run --example record_test_fixtures //! cargo run --example record_test_fixtures
//! //!
//! # Record specific scenario //! # 特定のシナリオを記録
//! ANTHROPIC_API_KEY=your-key cargo run --example record_test_fixtures -- simple_text //! ANTHROPIC_API_KEY=your-key cargo run --example record_test_fixtures -- simple_text
//! ANTHROPIC_API_KEY=your-key cargo run --example record_test_fixtures -- tool_call //! ANTHROPIC_API_KEY=your-key cargo run --example record_test_fixtures -- tool_call
//! //!
//! # Record all scenarios //! # 全シナリオを記録
//! ANTHROPIC_API_KEY=your-key cargo run --example record_test_fixtures -- --all //! ANTHROPIC_API_KEY=your-key cargo run --example record_test_fixtures -- --all
//! ``` //! ```
@@ -20,9 +20,9 @@ mod recorder;
mod scenarios; mod scenarios;
use clap::{Parser, ValueEnum}; use clap::{Parser, ValueEnum};
use llm_worker::llm_client::providers::anthropic::AnthropicClient; use worker::llm_client::providers::anthropic::AnthropicClient;
use llm_worker::llm_client::providers::gemini::GeminiClient; use worker::llm_client::providers::gemini::GeminiClient;
use llm_worker::llm_client::providers::openai::OpenAIClient; use worker::llm_client::providers::openai::OpenAIClient;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
@@ -101,7 +101,7 @@ async fn run_scenario_with_ollama(
subdir: &str, subdir: &str,
model: Option<String>, model: Option<String>,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
use llm_worker::llm_client::providers::ollama::OllamaClient; use worker::llm_client::providers::ollama::OllamaClient;
// Ollama typically runs local, no key needed or placeholder // Ollama typically runs local, no key needed or placeholder
let model = model.as_deref().unwrap_or("llama3"); // default example let model = model.as_deref().unwrap_or("llama3"); // default example
let client = OllamaClient::new(model); // base_url placeholder, handled by client default let client = OllamaClient::new(model); // base_url placeholder, handled by client default
@@ -193,8 +193,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
ClientType::Ollama => "ollama", ClientType::Ollama => "ollama",
}; };
// Scenario filtering is already done in main.rs logic // シナリオのフィルタリングは main.rs のロジックで実行済み
// Here we just execute in a simple loop // ここでは単純なループで実行
for scenario in scenarios_to_run { for scenario in scenarios_to_run {
match args.client { match args.client {
ClientType::Anthropic => { ClientType::Anthropic => {
@@ -1,6 +1,6 @@
//! Test fixture recording mechanism //! テストフィクスチャ記録機構
//! //!
//! Saves events to files in JSONL format //! イベントをJSONLフォーマットでファイルに保存する
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::{BufWriter, Write}; use std::io::{BufWriter, Write};
@@ -8,9 +8,9 @@ use std::path::Path;
use std::time::{Instant, SystemTime, UNIX_EPOCH}; use std::time::{Instant, SystemTime, UNIX_EPOCH};
use futures::StreamExt; use futures::StreamExt;
use llm_worker::llm_client::{LlmClient, Request}; use worker::llm_client::{LlmClient, Request};
/// Recorded event /// 記録されたイベント
#[derive(Debug, serde::Serialize, serde::Deserialize)] #[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct RecordedEvent { pub struct RecordedEvent {
pub elapsed_ms: u64, pub elapsed_ms: u64,
@@ -18,7 +18,7 @@ pub struct RecordedEvent {
pub data: String, pub data: String,
} }
/// Session metadata /// セッションメタデータ
#[derive(Debug, serde::Serialize, serde::Deserialize)] #[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct SessionMetadata { pub struct SessionMetadata {
pub timestamp: u64, pub timestamp: u64,
@@ -26,7 +26,7 @@ pub struct SessionMetadata {
pub description: String, pub description: String,
} }
/// Save event sequence to file /// イベントシーケンスをファイルに保存
pub fn save_fixture( pub fn save_fixture(
path: impl AsRef<Path>, path: impl AsRef<Path>,
metadata: &SessionMetadata, metadata: &SessionMetadata,
@@ -43,7 +43,7 @@ pub fn save_fixture(
Ok(()) Ok(())
} }
/// Send request and record events /// リクエストを送信してイベントを記録
pub async fn record_request<C: LlmClient>( pub async fn record_request<C: LlmClient>(
client: &C, client: &C,
request: Request, request: Request,
@@ -78,7 +78,7 @@ pub async fn record_request<C: LlmClient>(
} }
} }
// Save // 保存
let fixtures_dir = Path::new("worker/tests/fixtures").join(subdir); let fixtures_dir = Path::new("worker/tests/fixtures").join(subdir);
fs::create_dir_all(&fixtures_dir)?; fs::create_dir_all(&fixtures_dir)?;
@@ -1,20 +1,20 @@
//! Test fixture request definitions //! テストフィクスチャ用リクエスト定義
//! //!
//! Defines requests and output file names for each scenario //! 各シナリオのリクエストと出力ファイル名を定義
use llm_worker::llm_client::{Request, ToolDefinition}; use worker::llm_client::{Request, ToolDefinition};
/// Test scenario /// テストシナリオ
pub struct TestScenario { pub struct TestScenario {
/// Scenario name (description) /// シナリオ名(説明)
pub name: &'static str, pub name: &'static str,
/// Output file name (without extension) /// 出力ファイル名(拡張子なし)
pub output_name: &'static str, pub output_name: &'static str,
/// Request /// リクエスト
pub request: Request, pub request: Request,
} }
/// Get all test scenarios /// 全てのテストシナリオを取得
pub fn scenarios() -> Vec<TestScenario> { pub fn scenarios() -> Vec<TestScenario> {
vec![ vec![
simple_text_scenario(), simple_text_scenario(),
@@ -23,7 +23,7 @@ pub fn scenarios() -> Vec<TestScenario> {
] ]
} }
/// Simple text response /// シンプルなテキストレスポンス
fn simple_text_scenario() -> TestScenario { fn simple_text_scenario() -> TestScenario {
TestScenario { TestScenario {
name: "Simple text response", name: "Simple text response",
@@ -35,7 +35,7 @@ fn simple_text_scenario() -> TestScenario {
} }
} }
/// Response with tool call /// ツール呼び出しを含むレスポンス
fn tool_call_scenario() -> TestScenario { fn tool_call_scenario() -> TestScenario {
let get_weather_tool = ToolDefinition::new("get_weather") let get_weather_tool = ToolDefinition::new("get_weather")
.description("Get the current weather for a city") .description("Get the current weather for a city")
@@ -61,7 +61,7 @@ fn tool_call_scenario() -> TestScenario {
} }
} }
/// Long text generation scenario /// 長文生成シナリオ
fn long_text_scenario() -> TestScenario { fn long_text_scenario() -> TestScenario {
TestScenario { TestScenario {
name: "Long text response", name: "Long text response",
@@ -1,17 +1,17 @@
//! Interactive CLI client using Worker //! Worker を用いた対話型 CLI クライアント
//! //!
//! A CLI application for interacting with multiple LLM providers (Anthropic, Gemini, OpenAI, Ollama). //! 複数のLLMプロバイダ(Anthropic, Gemini, OpenAI, Ollama)と対話するCLIアプリケーション。
//! Demonstrates tool registration and execution, and streaming response display. //! ツールの登録と実行、ストリーミングレスポンスの表示をデモする。
//! //!
//! ## Usage //! ## 使用方法
//! //!
//! ```bash //! ```bash
//! # Set API keys in .env file //! # .envファイルにAPIキーを設定
//! echo "ANTHROPIC_API_KEY=your-api-key" > .env //! echo "ANTHROPIC_API_KEY=your-api-key" > .env
//! echo "GEMINI_API_KEY=your-api-key" >> .env //! echo "GEMINI_API_KEY=your-api-key" >> .env
//! echo "OPENAI_API_KEY=your-api-key" >> .env //! echo "OPENAI_API_KEY=your-api-key" >> .env
//! //!
//! # Anthropic (default) //! # Anthropic (デフォルト)
//! cargo run --example worker_cli //! cargo run --example worker_cli
//! //!
//! # Gemini //! # Gemini
@@ -20,13 +20,13 @@
//! # OpenAI //! # OpenAI
//! cargo run --example worker_cli -- --provider openai --model gpt-4o //! cargo run --example worker_cli -- --provider openai --model gpt-4o
//! //!
//! # Ollama (local) //! # Ollama (ローカル)
//! cargo run --example worker_cli -- --provider ollama --model llama3.2 //! cargo run --example worker_cli -- --provider ollama --model llama3.2
//! //!
//! # With options //! # オプション指定
//! cargo run --example worker_cli -- --provider anthropic --model claude-3-haiku-20240307 --system "You are a helpful assistant." //! cargo run --example worker_cli -- --provider anthropic --model claude-3-haiku-20240307 --system "You are a helpful assistant."
//! //!
//! # Show help //! # ヘルプ表示
//! cargo run --example worker_cli -- --help //! cargo run --example worker_cli -- --help
//! ``` //! ```
@@ -39,9 +39,9 @@ use tracing::info;
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
use clap::{Parser, ValueEnum}; use clap::{Parser, ValueEnum};
use llm_worker::{ use worker::{
Worker, Worker,
hook::{Hook, HookError, PostToolCall, PostToolCallContext, PostToolCallResult}, hook::{ControlFlow, HookError, ToolResult, WorkerHook},
llm_client::{ llm_client::{
LlmClient, LlmClient,
providers::{ providers::{
@@ -51,17 +51,17 @@ use llm_worker::{
}, },
timeline::{Handler, TextBlockEvent, TextBlockKind, ToolUseBlockEvent, ToolUseBlockKind}, timeline::{Handler, TextBlockEvent, TextBlockKind, ToolUseBlockEvent, ToolUseBlockKind},
}; };
use llm_worker_macros::tool_registry; use worker_macros::tool_registry;
// Required imports for macro expansion // 必要なマクロ展開用インポート
use schemars; use schemars;
use serde; use serde;
// ============================================================================= // =============================================================================
// Provider Definition // プロバイダ定義
// ============================================================================= // =============================================================================
/// Available LLM providers /// 利用可能なLLMプロバイダ
#[derive(Debug, Clone, Copy, ValueEnum, Default)] #[derive(Debug, Clone, Copy, ValueEnum, Default)]
enum Provider { enum Provider {
/// Anthropic Claude /// Anthropic Claude
@@ -71,12 +71,12 @@ enum Provider {
Gemini, Gemini,
/// OpenAI GPT /// OpenAI GPT
Openai, Openai,
/// Ollama (local) /// Ollama (ローカル)
Ollama, Ollama,
} }
impl Provider { impl Provider {
/// Default model for the provider /// プロバイダのデフォルトモデル
fn default_model(&self) -> &'static str { fn default_model(&self) -> &'static str {
match self { match self {
Provider::Anthropic => "claude-sonnet-4-20250514", Provider::Anthropic => "claude-sonnet-4-20250514",
@@ -86,7 +86,7 @@ impl Provider {
} }
} }
/// Display name for the provider /// プロバイダの表示名
fn display_name(&self) -> &'static str { fn display_name(&self) -> &'static str {
match self { match self {
Provider::Anthropic => "Anthropic Claude", Provider::Anthropic => "Anthropic Claude",
@@ -96,78 +96,78 @@ impl Provider {
} }
} }
/// Environment variable name for API key /// APIキーの環境変数名
fn env_var_name(&self) -> Option<&'static str> { fn env_var_name(&self) -> Option<&'static str> {
match self { match self {
Provider::Anthropic => Some("ANTHROPIC_API_KEY"), Provider::Anthropic => Some("ANTHROPIC_API_KEY"),
Provider::Gemini => Some("GEMINI_API_KEY"), Provider::Gemini => Some("GEMINI_API_KEY"),
Provider::Openai => Some("OPENAI_API_KEY"), Provider::Openai => Some("OPENAI_API_KEY"),
Provider::Ollama => None, // Ollama is local, no key needed Provider::Ollama => None, // Ollamaはローカルなので不要
} }
} }
} }
// ============================================================================= // =============================================================================
// CLI Argument Definition // CLI引数定義
// ============================================================================= // =============================================================================
/// Interactive CLI client supporting multiple LLM providers /// 複数のLLMプロバイダに対応した対話型CLIクライアント
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(name = "worker-cli")] #[command(name = "worker-cli")]
#[command(about = "Interactive CLI client for multiple LLM providers using Worker")] #[command(about = "Interactive CLI client for multiple LLM providers using Worker")]
#[command(version)] #[command(version)]
struct Args { struct Args {
/// Provider to use /// 使用するプロバイダ
#[arg(long, value_enum, default_value_t = Provider::Anthropic)] #[arg(long, value_enum, default_value_t = Provider::Anthropic)]
provider: Provider, provider: Provider,
/// Model name to use (defaults to provider's default if not specified) /// 使用するモデル名(未指定時はプロバイダのデフォルト)
#[arg(short, long)] #[arg(short, long)]
model: Option<String>, model: Option<String>,
/// System prompt /// システムプロンプト
#[arg(short, long)] #[arg(short, long)]
system: Option<String>, system: Option<String>,
/// Disable tools /// ツールを無効化
#[arg(long, default_value = "false")] #[arg(long, default_value = "false")]
no_tools: bool, no_tools: bool,
/// Initial message (if specified, sends it and exits) /// 最初のメッセージ(指定するとそれを送信して終了)
#[arg(short = 'p', long)] #[arg(short = 'p', long)]
prompt: Option<String>, prompt: Option<String>,
/// API key (takes precedence over environment variable) /// APIキー(環境変数より優先)
#[arg(long)] #[arg(long)]
api_key: Option<String>, api_key: Option<String>,
} }
// ============================================================================= // =============================================================================
// Tool Definition // ツール定義
// ============================================================================= // =============================================================================
/// Application context /// アプリケーションコンテキスト
#[derive(Clone)] #[derive(Clone)]
struct AppContext; struct AppContext;
#[tool_registry] #[tool_registry]
impl AppContext { impl AppContext {
/// Get the current date and time /// 現在の日時を取得する
/// ///
/// Returns the system's current date and time. /// システムの現在の日付と時刻を返します。
#[tool] #[tool]
fn get_current_time(&self) -> String { fn get_current_time(&self) -> String {
let now = std::time::SystemTime::now() let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH) .duration_since(std::time::UNIX_EPOCH)
.unwrap() .unwrap()
.as_secs(); .as_secs();
// Simple conversion from Unix timestamp // シンプルなUnixタイムスタンプからの変換
format!("Current Unix timestamp: {}", now) format!("Current Unix timestamp: {}", now)
} }
/// Perform a simple calculation /// 簡単な計算を行う
/// ///
/// Executes arithmetic operations on two numbers. /// 2つの数値の四則演算を実行します。
#[tool] #[tool]
fn calculate(&self, a: f64, b: f64, operation: String) -> Result<String, String> { fn calculate(&self, a: f64, b: f64, operation: String) -> Result<String, String> {
let result = match operation.as_str() { let result = match operation.as_str() {
@@ -187,10 +187,10 @@ impl AppContext {
} }
// ============================================================================= // =============================================================================
// Streaming Display Handlers // ストリーミング表示用ハンドラー
// ============================================================================= // =============================================================================
/// Handler that outputs text in real-time /// テキストをリアルタイムで出力するハンドラー
struct StreamingPrinter { struct StreamingPrinter {
is_first_delta: Arc<Mutex<bool>>, is_first_delta: Arc<Mutex<bool>>,
} }
@@ -226,7 +226,7 @@ impl Handler<TextBlockKind> for StreamingPrinter {
} }
} }
/// Handler that displays tool calls /// ツール呼び出しを表示するハンドラー
struct ToolCallPrinter { struct ToolCallPrinter {
call_names: Arc<Mutex<HashMap<String, String>>>, call_names: Arc<Mutex<HashMap<String, String>>>,
} }
@@ -270,7 +270,7 @@ impl Handler<ToolUseBlockKind> for ToolCallPrinter {
} }
} }
/// Hook that displays tool execution results /// ツール実行結果を表示するHook
struct ToolResultPrinterHook { struct ToolResultPrinterHook {
call_names: Arc<Mutex<HashMap<String, String>>>, call_names: Arc<Mutex<HashMap<String, String>>>,
} }
@@ -282,37 +282,40 @@ impl ToolResultPrinterHook {
} }
#[async_trait] #[async_trait]
impl Hook<PostToolCall> for ToolResultPrinterHook { impl WorkerHook for ToolResultPrinterHook {
async fn call(&self, ctx: &mut PostToolCallContext) -> Result<PostToolCallResult, HookError> { async fn after_tool_call(
&self,
tool_result: &mut ToolResult,
) -> Result<ControlFlow, HookError> {
let name = self let name = self
.call_names .call_names
.lock() .lock()
.unwrap() .unwrap()
.remove(&ctx.result.tool_use_id) .remove(&tool_result.tool_use_id)
.unwrap_or_else(|| ctx.result.tool_use_id.clone()); .unwrap_or_else(|| tool_result.tool_use_id.clone());
if ctx.result.is_error { if tool_result.is_error {
println!(" Result ({}): ❌ {}", name, ctx.result.content); println!(" Result ({}): ❌ {}", name, tool_result.content);
} else { } else {
println!(" Result ({}): ✅ {}", name, ctx.result.content); println!(" Result ({}): ✅ {}", name, tool_result.content);
} }
Ok(PostToolCallResult::Continue) Ok(ControlFlow::Continue)
} }
} }
// ============================================================================= // =============================================================================
// Client Creation // クライアント作成
// ============================================================================= // =============================================================================
/// Get API key based on provider /// プロバイダに応じたAPIキーを取得
fn get_api_key(args: &Args) -> Result<String, String> { fn get_api_key(args: &Args) -> Result<String, String> {
// CLI argument API key takes precedence // CLI引数のAPIキーが優先
if let Some(ref key) = args.api_key { if let Some(ref key) = args.api_key {
return Ok(key.clone()); return Ok(key.clone());
} }
// Check environment variable based on provider // プロバイダに応じた環境変数を確認
if let Some(env_var) = args.provider.env_var_name() { if let Some(env_var) = args.provider.env_var_name() {
std::env::var(env_var).map_err(|_| { std::env::var(env_var).map_err(|_| {
format!( format!(
@@ -321,12 +324,12 @@ fn get_api_key(args: &Args) -> Result<String, String> {
) )
}) })
} else { } else {
// Ollama etc. don't need a key // Ollamaなどはキー不要
Ok(String::new()) Ok(String::new())
} }
} }
/// Create client based on provider /// プロバイダに応じたクライアントを作成
fn create_client(args: &Args) -> Result<Box<dyn LlmClient>, String> { fn create_client(args: &Args) -> Result<Box<dyn LlmClient>, String> {
let model = args let model = args
.model .model
@@ -356,17 +359,17 @@ fn create_client(args: &Args) -> Result<Box<dyn LlmClient>, String> {
} }
// ============================================================================= // =============================================================================
// Main // メイン
// ============================================================================= // =============================================================================
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load .env file // .envファイルを読み込む
dotenv::dotenv().ok(); dotenv::dotenv().ok();
// Initialize logging // ロギング初期化
// Use RUST_LOG=debug cargo run --example worker_cli ... for detailed logs // RUST_LOG=debug cargo run --example worker_cli ... で詳細ログ表示
// Default is warn level, can be overridden with RUST_LOG environment variable // デフォルトは warn レベル、RUST_LOG 環境変数で上書き可能
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn")); let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn"));
tracing_subscriber::fmt() tracing_subscriber::fmt()
@@ -374,7 +377,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_target(true) .with_target(true)
.init(); .init();
// Parse CLI arguments // CLI引数をパース
let args = Args::parse(); let args = Args::parse();
info!( info!(
@@ -383,10 +386,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
"Starting worker CLI" "Starting worker CLI"
); );
// Interactive mode or one-shot mode // 対話モードかワンショットモードか
let is_interactive = args.prompt.is_none(); let is_interactive = args.prompt.is_none();
// Model name (for display) // モデル名(表示用)
let model_name = args let model_name = args
.model .model
.clone() .clone()
@@ -416,7 +419,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("─────────────────────────────────────────────────"); println!("─────────────────────────────────────────────────");
} }
// Create client // クライアント作成
let client = match create_client(&args) { let client = match create_client(&args) {
Ok(c) => c, Ok(c) => c,
Err(e) => { Err(e) => {
@@ -425,34 +428,32 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
}; };
// Create Worker // Worker作成
let mut worker = Worker::new(client); let mut worker = Worker::new(client);
let tool_call_names = Arc::new(Mutex::new(HashMap::new())); let tool_call_names = Arc::new(Mutex::new(HashMap::new()));
// Set system prompt // システムプロンプトを設定
if let Some(ref system_prompt) = args.system { if let Some(ref system_prompt) = args.system {
worker.set_system_prompt(system_prompt); worker.set_system_prompt(system_prompt);
} }
// Register tools (unless --no-tools) // ツール登録(--no-tools でなければ)
if !args.no_tools { if !args.no_tools {
let app = AppContext; let app = AppContext;
worker worker.register_tool(app.get_current_time_tool());
.register_tool(app.get_current_time_definition()) worker.register_tool(app.calculate_tool());
.unwrap();
worker.register_tool(app.calculate_definition()).unwrap();
} }
// Register streaming display handlers // ストリーミング表示用ハンドラーを登録
worker worker
.timeline_mut() .timeline_mut()
.on_text_block(StreamingPrinter::new()) .on_text_block(StreamingPrinter::new())
.on_tool_use_block(ToolCallPrinter::new(tool_call_names.clone())); .on_tool_use_block(ToolCallPrinter::new(tool_call_names.clone()));
worker.add_post_tool_call_hook(ToolResultPrinterHook::new(tool_call_names)); worker.add_hook(ToolResultPrinterHook::new(tool_call_names));
// One-shot mode // ワンショットモード
if let Some(prompt) = args.prompt { if let Some(prompt) = args.prompt {
match worker.run(&prompt).await { match worker.run(&prompt).await {
Ok(_) => {} Ok(_) => {}
@@ -465,7 +466,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
return Ok(()); return Ok(());
} }
// Interactive loop // 対話ループ
loop { loop {
print!("\n👤 You: "); print!("\n👤 You: ");
io::stdout().flush()?; io::stdout().flush()?;
@@ -483,7 +484,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
break; break;
} }
// Run Worker (Worker manages history) // Workerを実行(Workerが履歴を管理)
match worker.run(input).await { match worker.run(input).await {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
+93
View File
@@ -0,0 +1,93 @@
//! worker - LLMワーカーライブラリ
//!
//! LLMとの対話を管理するコンポーネントを提供します。
//!
//! # 主要なコンポーネント
//!
//! - [`Worker`] - LLMとの対話を管理する中心コンポーネント
//! - [`tool::Tool`] - LLMから呼び出し可能なツール
//! - [`hook::WorkerHook`] - ターン進行への介入
//! - [`subscriber::WorkerSubscriber`] - ストリーミングイベントの購読
//!
//! # Quick Start
//!
//! ```ignore
//! use worker::{Worker, Message};
//!
//! // Workerを作成
//! let mut worker = Worker::new(client)
//! .system_prompt("You are a helpful assistant.");
//!
//! // ツールを登録(オプション)
//! use worker::tool::Tool;
//! worker.register_tool(my_tool);
//!
//! // 対話を実行
//! let history = worker.run("Hello!").await?;
//! ```
//!
//! # キャッシュ保護
//!
//! KVキャッシュのヒット率を最大化するには、[`Worker::lock()`]で
//! ロック状態に遷移してから実行してください。
//!
//! ```ignore
//! let mut locked = worker.lock();
//! locked.run("user input").await?;
//! ```
pub mod llm_client;
pub mod timeline;
mod subscriber_adapter;
mod worker;
// =============================================================================
// トップレベル公開(最も頻繁に使う型)
// =============================================================================
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};
}
+41
View File
@@ -0,0 +1,41 @@
//! LLMクライアント共通trait定義
use std::pin::Pin;
use async_trait::async_trait;
use futures::Stream;
use worker_types::Event;
use crate::llm_client::{ClientError, Request};
/// LLMクライアントのtrait
///
/// 各プロバイダはこのtraitを実装し、統一されたインターフェースを提供する。
#[async_trait]
pub trait LlmClient: Send + Sync {
/// ストリーミングリクエストを送信し、Eventストリームを返す
///
/// # Arguments
/// * `request` - リクエスト情報
///
/// # Returns
/// * `Ok(Stream)` - イベントストリーム
/// * `Err(ClientError)` - エラー
async fn stream(
&self,
request: Request,
) -> Result<Pin<Box<dyn Stream<Item = Result<Event, ClientError>> + Send>>, ClientError>;
}
/// `Box<dyn LlmClient>` に対する `LlmClient` の実装
///
/// これにより、動的ディスパッチを使用するクライアントも `Worker` で利用可能になる。
#[async_trait]
impl LlmClient for Box<dyn LlmClient> {
async fn stream(
&self,
request: Request,
) -> Result<Pin<Box<dyn Stream<Item = Result<Event, ClientError>> + Send>>, ClientError> {
(**self).stream(request).await
}
}
@@ -1,7 +1,6 @@
//! LLMクライアント層 //! LLMクライアント層
//! //!
//! 各LLMプロバイダと通信し、統一された[`Event`] //! 各LLMプロバイダと通信し、統一された[`Event`](crate::event::Event)ストリームを出力します。
//! ストリームを出力します。
//! //!
//! # サポートするプロバイダ //! # サポートするプロバイダ
//! //!
@@ -18,7 +17,6 @@
pub mod client; pub mod client;
pub mod error; pub mod error;
pub mod event;
pub mod types; pub mod types;
pub mod providers; pub mod providers;
@@ -26,5 +24,4 @@ pub mod scheme;
pub use client::*; pub use client::*;
pub use error::*; pub use error::*;
pub use event::*;
pub use types::*; pub use types::*;
@@ -4,13 +4,13 @@
use std::pin::Pin; use std::pin::Pin;
use crate::llm_client::{
ClientError, LlmClient, Request, event::Event, scheme::anthropic::AnthropicScheme,
};
use async_trait::async_trait; use async_trait::async_trait;
use eventsource_stream::Eventsource; use eventsource_stream::Eventsource;
use futures::{Stream, StreamExt, TryStreamExt, future::ready}; use futures::{Stream, StreamExt, TryStreamExt, future::ready};
use reqwest::header::{CONTENT_TYPE, HeaderMap, HeaderValue}; use reqwest::header::{CONTENT_TYPE, HeaderMap, HeaderValue};
use worker_types::Event;
use crate::llm_client::{ClientError, LlmClient, Request, scheme::anthropic::AnthropicScheme};
/// Anthropic クライアント /// Anthropic クライアント
pub struct AnthropicClient { pub struct AnthropicClient {
@@ -156,8 +156,7 @@ impl LlmClient for AnthropicClient {
if let Some(block_type) = current_block_type.take() { if let Some(block_type) = current_block_type.take() {
// 正しいブロックタイプで上書き // 正しいブロックタイプで上書き
// (Event::BlockStopの中身を置換) // (Event::BlockStopの中身を置換)
evt = evt = Event::BlockStop(worker_types::BlockStop {
Event::BlockStop(crate::llm_client::event::BlockStop {
block_type, block_type,
..stop.clone() ..stop.clone()
}); });
@@ -4,13 +4,13 @@
use std::pin::Pin; use std::pin::Pin;
use crate::llm_client::{
ClientError, LlmClient, Request, event::Event, scheme::gemini::GeminiScheme,
};
use async_trait::async_trait; use async_trait::async_trait;
use eventsource_stream::Eventsource; use eventsource_stream::Eventsource;
use futures::{Stream, StreamExt, TryStreamExt}; use futures::{Stream, StreamExt, TryStreamExt};
use reqwest::header::{CONTENT_TYPE, HeaderMap, HeaderValue}; use reqwest::header::{CONTENT_TYPE, HeaderMap, HeaderValue};
use worker_types::Event;
use crate::llm_client::{ClientError, LlmClient, Request, scheme::gemini::GeminiScheme};
/// Gemini クライアント /// Gemini クライアント
pub struct GeminiClient { pub struct GeminiClient {
@@ -5,12 +5,13 @@
use std::pin::Pin; use std::pin::Pin;
use crate::llm_client::{
ClientError, LlmClient, Request, event::Event, providers::openai::OpenAIClient,
scheme::openai::OpenAIScheme,
};
use async_trait::async_trait; use async_trait::async_trait;
use futures::Stream; use futures::Stream;
use worker_types::Event;
use crate::llm_client::{
ClientError, LlmClient, Request, providers::openai::OpenAIClient, scheme::openai::OpenAIScheme,
};
/// Ollama クライアント /// Ollama クライアント
/// ///
@@ -4,14 +4,13 @@
use std::pin::Pin; use std::pin::Pin;
use crate::llm_client::{
ClientError, ConfigWarning, LlmClient, Request, RequestConfig, event::Event,
scheme::openai::OpenAIScheme,
};
use async_trait::async_trait; use async_trait::async_trait;
use eventsource_stream::Eventsource; use eventsource_stream::Eventsource;
use futures::{Stream, StreamExt, TryStreamExt}; use futures::{Stream, StreamExt, TryStreamExt};
use reqwest::header::{CONTENT_TYPE, HeaderMap, HeaderValue}; use reqwest::header::{CONTENT_TYPE, HeaderMap, HeaderValue};
use worker_types::Event;
use crate::llm_client::{ClientError, LlmClient, Request, scheme::openai::OpenAIScheme};
/// OpenAI クライアント /// OpenAI クライアント
pub struct OpenAIClient { pub struct OpenAIClient {
@@ -198,15 +197,4 @@ impl LlmClient for OpenAIClient {
Ok(Box::pin(stream)) Ok(Box::pin(stream))
} }
fn validate_config(&self, config: &RequestConfig) -> Vec<ConfigWarning> {
let mut warnings = Vec::new();
// OpenAI does not support top_k
if config.top_k.is_some() {
warnings.push(ConfigWarning::unsupported("top_k", "OpenAI"));
}
warnings
}
} }
@@ -2,14 +2,13 @@
//! //!
//! Anthropic Messages APIのSSEイベントをパースし、統一Event型に変換 //! Anthropic Messages APIのSSEイベントをパースし、統一Event型に変換
use crate::llm_client::{
ClientError,
event::{
BlockDelta, BlockMetadata, BlockStart, BlockStop, BlockType, DeltaContent, ErrorEvent,
Event, PingEvent, ResponseStatus, StatusEvent, UsageEvent,
},
};
use serde::Deserialize; use serde::Deserialize;
use worker_types::{
BlockDelta, BlockMetadata, BlockStart, BlockStop, BlockType, DeltaContent, ErrorEvent, Event,
PingEvent, ResponseStatus, StatusEvent, UsageEvent,
};
use crate::llm_client::ClientError;
use super::AnthropicScheme; use super::AnthropicScheme;
@@ -0,0 +1,195 @@
//! Anthropic リクエスト生成
use serde::Serialize;
use crate::llm_client::{
Request,
types::{ContentPart, Message, MessageContent, Role, ToolDefinition},
};
use super::AnthropicScheme;
/// Anthropic APIへのリクエストボディ
#[derive(Debug, Serialize)]
pub(crate) struct AnthropicRequest {
pub model: String,
pub max_tokens: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub system: Option<String>,
pub messages: Vec<AnthropicMessage>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub tools: Vec<AnthropicTool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_p: Option<f32>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub stop_sequences: Vec<String>,
pub stream: bool,
}
/// Anthropic メッセージ
#[derive(Debug, Serialize)]
pub(crate) struct AnthropicMessage {
pub role: String,
pub content: AnthropicContent,
}
/// Anthropic コンテンツ
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub(crate) enum AnthropicContent {
Text(String),
Parts(Vec<AnthropicContentPart>),
}
/// Anthropic コンテンツパーツ
#[derive(Debug, Serialize)]
#[serde(tag = "type")]
pub(crate) enum AnthropicContentPart {
#[serde(rename = "text")]
Text { text: String },
#[serde(rename = "tool_use")]
ToolUse {
id: String,
name: String,
input: serde_json::Value,
},
#[serde(rename = "tool_result")]
ToolResult {
tool_use_id: String,
content: String,
},
}
/// Anthropic ツール定義
#[derive(Debug, Serialize)]
pub(crate) struct AnthropicTool {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub input_schema: serde_json::Value,
}
impl AnthropicScheme {
/// RequestからAnthropicのリクエストボディを構築
pub(crate) fn build_request(&self, model: &str, request: &Request) -> AnthropicRequest {
let messages = request
.messages
.iter()
.map(|m| self.convert_message(m))
.collect();
let tools = request.tools.iter().map(|t| self.convert_tool(t)).collect();
AnthropicRequest {
model: model.to_string(),
max_tokens: request.config.max_tokens.unwrap_or(4096),
system: request.system_prompt.clone(),
messages,
tools,
temperature: request.config.temperature,
top_p: request.config.top_p,
stop_sequences: request.config.stop_sequences.clone(),
stream: true,
}
}
fn convert_message(&self, message: &Message) -> AnthropicMessage {
let role = match message.role {
Role::User => "user",
Role::Assistant => "assistant",
};
let content = match &message.content {
MessageContent::Text(text) => AnthropicContent::Text(text.clone()),
MessageContent::ToolResult {
tool_use_id,
content,
} => AnthropicContent::Parts(vec![AnthropicContentPart::ToolResult {
tool_use_id: tool_use_id.clone(),
content: content.clone(),
}]),
MessageContent::Parts(parts) => {
let converted: Vec<_> = parts
.iter()
.map(|p| match p {
ContentPart::Text { text } => {
AnthropicContentPart::Text { text: text.clone() }
}
ContentPart::ToolUse { id, name, input } => AnthropicContentPart::ToolUse {
id: id.clone(),
name: name.clone(),
input: input.clone(),
},
ContentPart::ToolResult {
tool_use_id,
content,
} => AnthropicContentPart::ToolResult {
tool_use_id: tool_use_id.clone(),
content: content.clone(),
},
})
.collect();
AnthropicContent::Parts(converted)
}
};
AnthropicMessage {
role: role.to_string(),
content,
}
}
fn convert_tool(&self, tool: &ToolDefinition) -> AnthropicTool {
AnthropicTool {
name: tool.name.clone(),
description: tool.description.clone(),
input_schema: tool.input_schema.clone(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_build_simple_request() {
let scheme = AnthropicScheme::new();
let request = Request::new()
.system("You are a helpful assistant.")
.user("Hello!");
let anthropic_req = scheme.build_request("claude-sonnet-4-20250514", &request);
assert_eq!(anthropic_req.model, "claude-sonnet-4-20250514");
assert_eq!(
anthropic_req.system,
Some("You are a helpful assistant.".to_string())
);
assert_eq!(anthropic_req.messages.len(), 1);
assert!(anthropic_req.stream);
}
#[test]
fn test_build_request_with_tool() {
let scheme = AnthropicScheme::new();
let request = Request::new().user("What's the weather?").tool(
ToolDefinition::new("get_weather")
.description("Get current weather")
.input_schema(serde_json::json!({
"type": "object",
"properties": {
"location": { "type": "string" }
},
"required": ["location"]
})),
);
let anthropic_req = scheme.build_request("claude-sonnet-4-20250514", &request);
assert_eq!(anthropic_req.tools.len(), 1);
assert_eq!(anthropic_req.tools[0].name, "get_weather");
}
}
@@ -2,11 +2,12 @@
//! //!
//! Google Gemini APIのSSEイベントをパースし、統一Event型に変換 //! Google Gemini APIのSSEイベントをパースし、統一Event型に変換
use crate::llm_client::{
ClientError,
event::{BlockMetadata, BlockStart, BlockStop, BlockType, Event, StopReason, UsageEvent},
};
use serde::Deserialize; use serde::Deserialize;
use worker_types::{
BlockMetadata, BlockStart, BlockStop, BlockType, Event, StopReason, UsageEvent,
};
use crate::llm_client::ClientError;
use super::GeminiScheme; use super::GeminiScheme;
@@ -230,7 +231,7 @@ impl GeminiScheme {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::llm_client::event::DeltaContent; use worker_types::DeltaContent;
#[test] #[test]
fn test_parse_text_response() { fn test_parse_text_response() {
@@ -1,130 +1,130 @@
//! Gemini Request Builder //! Gemini リクエスト生成
//! //!
//! Converts Open Responses native Item model to Google Gemini API format. //! Google Gemini APIへのリクエストボディを構築
use serde::Serialize; use serde::Serialize;
use serde_json::Value; use serde_json::Value;
use crate::llm_client::{ use crate::llm_client::{
types::{Item, Role, ToolDefinition},
Request, Request,
types::{ContentPart, Message, MessageContent, Role, ToolDefinition},
}; };
use super::GeminiScheme; use super::GeminiScheme;
/// Gemini API request body /// Gemini APIへのリクエストボディ
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub(crate) struct GeminiRequest { pub(crate) struct GeminiRequest {
/// Contents (conversation history) /// コンテンツ(会話履歴)
pub contents: Vec<GeminiContent>, pub contents: Vec<GeminiContent>,
/// System instruction /// システム指示
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub system_instruction: Option<GeminiContent>, pub system_instruction: Option<GeminiContent>,
/// Tool definitions /// ツール定義
#[serde(skip_serializing_if = "Vec::is_empty")] #[serde(skip_serializing_if = "Vec::is_empty")]
pub tools: Vec<GeminiTool>, pub tools: Vec<GeminiTool>,
/// Tool config /// ツール設定
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub tool_config: Option<GeminiToolConfig>, pub tool_config: Option<GeminiToolConfig>,
/// Generation config /// 生成設定
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub generation_config: Option<GeminiGenerationConfig>, pub generation_config: Option<GeminiGenerationConfig>,
} }
/// Gemini content /// Gemini コンテンツ
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub(crate) struct GeminiContent { pub(crate) struct GeminiContent {
/// Role /// ロール
pub role: String, pub role: String,
/// Parts /// パーツ
pub parts: Vec<GeminiPart>, pub parts: Vec<GeminiPart>,
} }
/// Gemini part /// Gemini パーツ
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(untagged)] #[serde(untagged)]
pub(crate) enum GeminiPart { pub(crate) enum GeminiPart {
/// Text part /// テキストパーツ
Text { text: String }, Text { text: String },
/// Function call part /// 関数呼び出しパーツ
FunctionCall { FunctionCall {
#[serde(rename = "functionCall")] #[serde(rename = "functionCall")]
function_call: GeminiFunctionCall, function_call: GeminiFunctionCall,
}, },
/// Function response part /// 関数レスポンスパーツ
FunctionResponse { FunctionResponse {
#[serde(rename = "functionResponse")] #[serde(rename = "functionResponse")]
function_response: GeminiFunctionResponse, function_response: GeminiFunctionResponse,
}, },
} }
/// Gemini function call /// Gemini 関数呼び出し
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub(crate) struct GeminiFunctionCall { pub(crate) struct GeminiFunctionCall {
pub name: String, pub name: String,
pub args: Value, pub args: Value,
} }
/// Gemini function response /// Gemini 関数レスポンス
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub(crate) struct GeminiFunctionResponse { pub(crate) struct GeminiFunctionResponse {
pub name: String, pub name: String,
pub response: GeminiFunctionResponseContent, pub response: GeminiFunctionResponseContent,
} }
/// Gemini function response content /// Gemini 関数レスポンス内容
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub(crate) struct GeminiFunctionResponseContent { pub(crate) struct GeminiFunctionResponseContent {
pub name: String, pub name: String,
pub content: Value, pub content: Value,
} }
/// Gemini tool definition /// Gemini ツール定義
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub(crate) struct GeminiTool { pub(crate) struct GeminiTool {
/// Function declarations /// 関数宣言
pub function_declarations: Vec<GeminiFunctionDeclaration>, pub function_declarations: Vec<GeminiFunctionDeclaration>,
} }
/// Gemini function declaration /// Gemini 関数宣言
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub(crate) struct GeminiFunctionDeclaration { pub(crate) struct GeminiFunctionDeclaration {
/// Function name /// 関数名
pub name: String, pub name: String,
/// Description /// 説明
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>, pub description: Option<String>,
/// Parameter schema /// パラメータスキーマ
pub parameters: Value, pub parameters: Value,
} }
/// Gemini tool config /// Gemini ツール設定
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub(crate) struct GeminiToolConfig { pub(crate) struct GeminiToolConfig {
/// Function calling config /// 関数呼び出し設定
pub function_calling_config: GeminiFunctionCallingConfig, pub function_calling_config: GeminiFunctionCallingConfig,
} }
/// Gemini function calling config /// Gemini 関数呼び出し設定
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub(crate) struct GeminiFunctionCallingConfig { pub(crate) struct GeminiFunctionCallingConfig {
/// Mode: AUTO, ANY, NONE /// モード: AUTO, ANY, NONE
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub mode: Option<String>, pub mode: Option<String>,
/// Enable streaming function call arguments /// ストリーミング関数呼び出し引数を有効にするか
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub stream_function_call_arguments: Option<bool>, pub stream_function_call_arguments: Option<bool>,
} }
/// Gemini generation config /// Gemini 生成設定
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub(crate) struct GeminiGenerationConfig { pub(crate) struct GeminiGenerationConfig {
/// Max output tokens /// 最大出力トークン数
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub max_output_tokens: Option<u32>, pub max_output_tokens: Option<u32>,
/// Temperature /// Temperature
@@ -133,26 +133,27 @@ pub(crate) struct GeminiGenerationConfig {
/// Top P /// Top P
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub top_p: Option<f32>, pub top_p: Option<f32>,
/// Top K /// ストップシーケンス
#[serde(skip_serializing_if = "Option::is_none")]
pub top_k: Option<u32>,
/// Stop sequences
#[serde(skip_serializing_if = "Vec::is_empty")] #[serde(skip_serializing_if = "Vec::is_empty")]
pub stop_sequences: Vec<String>, pub stop_sequences: Vec<String>,
} }
impl GeminiScheme { impl GeminiScheme {
/// Build Gemini request from Request /// RequestからGeminiのリクエストボディを構築
pub(crate) fn build_request(&self, request: &Request) -> GeminiRequest { pub(crate) fn build_request(&self, request: &Request) -> GeminiRequest {
let contents = self.convert_items_to_contents(&request.items); let mut contents = Vec::new();
// System prompt for message in &request.messages {
contents.push(self.convert_message(message));
}
// システムプロンプト
let system_instruction = request.system_prompt.as_ref().map(|s| GeminiContent { let system_instruction = request.system_prompt.as_ref().map(|s| GeminiContent {
role: "user".to_string(), role: "user".to_string(), // system_instructionではroleは"user"か省略
parts: vec![GeminiPart::Text { text: s.clone() }], parts: vec![GeminiPart::Text { text: s.clone() }],
}); });
// Tools // ツール
let tools = if request.tools.is_empty() { let tools = if request.tools.is_empty() {
vec![] vec![]
} else { } else {
@@ -161,7 +162,7 @@ impl GeminiScheme {
}] }]
}; };
// Tool config // ツール設定
let tool_config = if !request.tools.is_empty() { let tool_config = if !request.tools.is_empty() {
Some(GeminiToolConfig { Some(GeminiToolConfig {
function_calling_config: GeminiFunctionCallingConfig { function_calling_config: GeminiFunctionCallingConfig {
@@ -177,12 +178,11 @@ impl GeminiScheme {
None None
}; };
// Generation config // 生成設定
let generation_config = Some(GeminiGenerationConfig { let generation_config = Some(GeminiGenerationConfig {
max_output_tokens: request.config.max_tokens, max_output_tokens: request.config.max_tokens,
temperature: request.config.temperature, temperature: request.config.temperature,
top_p: request.config.top_p, top_p: request.config.top_p,
top_k: request.config.top_k,
stop_sequences: request.config.stop_sequences.clone(), stop_sequences: request.config.stop_sequences.clone(),
}); });
@@ -195,126 +195,58 @@ impl GeminiScheme {
} }
} }
/// Convert Open Responses Items to Gemini Contents fn convert_message(&self, message: &Message) -> GeminiContent {
/// let role = match message.role {
/// Gemini uses:
/// - role "user" for user messages and function responses
/// - role "model" for assistant messages and function calls
fn convert_items_to_contents(&self, items: &[Item]) -> Vec<GeminiContent> {
let mut contents = Vec::new();
let mut pending_model_parts: Vec<GeminiPart> = Vec::new();
let mut pending_user_parts: Vec<GeminiPart> = Vec::new();
for item in items {
match item {
Item::Message { role, content, .. } => {
// Flush pending parts
self.flush_pending_parts(
&mut contents,
&mut pending_model_parts,
&mut pending_user_parts,
);
let gemini_role = match role {
Role::User => "user", Role::User => "user",
Role::Assistant => "model", Role::Assistant => "model",
Role::System => continue, // Skip system role items
}; };
let parts: Vec<GeminiPart> = content let parts = match &message.content {
.iter() MessageContent::Text(text) => vec![GeminiPart::Text { text: text.clone() }],
.map(|p| GeminiPart::Text { MessageContent::ToolResult {
text: p.as_text().to_string(), tool_use_id,
}) content,
.collect();
contents.push(GeminiContent {
role: gemini_role.to_string(),
parts,
});
}
Item::FunctionCall {
name, arguments, ..
} => { } => {
// Flush pending user parts first // Geminiでは関数レスポンスとしてマップ
if !pending_user_parts.is_empty() { vec![GeminiPart::FunctionResponse {
contents.push(GeminiContent { function_response: GeminiFunctionResponse {
role: "user".to_string(), name: tool_use_id.clone(),
parts: std::mem::take(&mut pending_user_parts), response: GeminiFunctionResponseContent {
}); name: tool_use_id.clone(),
content: serde_json::Value::String(content.clone()),
},
},
}]
} }
MessageContent::Parts(parts) => parts
// Parse arguments .iter()
let args = serde_json::from_str(arguments) .map(|p| match p {
.unwrap_or_else(|_| Value::Object(serde_json::Map::new())); ContentPart::Text { text } => GeminiPart::Text { text: text.clone() },
ContentPart::ToolUse { id: _, name, input } => GeminiPart::FunctionCall {
pending_model_parts.push(GeminiPart::FunctionCall {
function_call: GeminiFunctionCall { function_call: GeminiFunctionCall {
name: name.clone(), name: name.clone(),
args, args: input.clone(),
}, },
}); },
} ContentPart::ToolResult {
tool_use_id,
Item::FunctionCallOutput { call_id, output, .. } => { content,
// Flush pending model parts first } => GeminiPart::FunctionResponse {
if !pending_model_parts.is_empty() {
contents.push(GeminiContent {
role: "model".to_string(),
parts: std::mem::take(&mut pending_model_parts),
});
}
pending_user_parts.push(GeminiPart::FunctionResponse {
function_response: GeminiFunctionResponse { function_response: GeminiFunctionResponse {
name: call_id.clone(), name: tool_use_id.clone(),
response: GeminiFunctionResponseContent { response: GeminiFunctionResponseContent {
name: call_id.clone(), name: tool_use_id.clone(),
content: Value::String(output.clone()), content: serde_json::Value::String(content.clone()),
}, },
}, },
}); },
} })
.collect(),
};
Item::Reasoning { text, .. } => { GeminiContent {
// Flush pending user parts first role: role.to_string(),
if !pending_user_parts.is_empty() { parts,
contents.push(GeminiContent {
role: "user".to_string(),
parts: std::mem::take(&mut pending_user_parts),
});
}
// Reasoning is treated as model text in Gemini
pending_model_parts.push(GeminiPart::Text { text: text.clone() });
}
}
}
// Flush remaining pending parts
self.flush_pending_parts(&mut contents, &mut pending_model_parts, &mut pending_user_parts);
contents
}
fn flush_pending_parts(
&self,
contents: &mut Vec<GeminiContent>,
pending_model_parts: &mut Vec<GeminiPart>,
pending_user_parts: &mut Vec<GeminiPart>,
) {
if !pending_model_parts.is_empty() {
contents.push(GeminiContent {
role: "model".to_string(),
parts: std::mem::take(pending_model_parts),
});
}
if !pending_user_parts.is_empty() {
contents.push(GeminiContent {
role: "user".to_string(),
parts: std::mem::take(pending_user_parts),
});
} }
} }
@@ -382,24 +314,4 @@ mod tests {
assert_eq!(gemini_req.contents[0].role, "user"); assert_eq!(gemini_req.contents[0].role, "user");
assert_eq!(gemini_req.contents[1].role, "model"); assert_eq!(gemini_req.contents[1].role, "model");
} }
#[test]
fn test_function_call_and_output() {
let scheme = GeminiScheme::new();
let request = Request::new()
.user("What's the weather?")
.item(Item::function_call(
"call_123",
"get_weather",
r#"{"city":"Tokyo"}"#,
))
.item(Item::function_call_output("call_123", "Sunny, 25°C"));
let gemini_req = scheme.build_request(&request);
assert_eq!(gemini_req.contents.len(), 3);
assert_eq!(gemini_req.contents[0].role, "user");
assert_eq!(gemini_req.contents[1].role, "model");
assert_eq!(gemini_req.contents[2].role, "user");
}
} }
@@ -1,10 +1,9 @@
//! OpenAI SSEイベントパース //! OpenAI SSEイベントパース
use crate::llm_client::{
ClientError,
event::{Event, StopReason, UsageEvent},
};
use serde::Deserialize; use serde::Deserialize;
use worker_types::{Event, StopReason, UsageEvent};
use crate::llm_client::ClientError;
use super::OpenAIScheme; use super::OpenAIScheme;
@@ -156,7 +155,7 @@ impl OpenAIScheme {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::llm_client::event::DeltaContent; use worker_types::DeltaContent;
#[test] #[test]
fn test_parse_text_delta() { fn test_parse_text_delta() {
@@ -189,7 +188,7 @@ mod tests {
assert_eq!(events.len(), 1); assert_eq!(events.len(), 1);
if let Event::BlockStart(start) = &events[0] { if let Event::BlockStart(start) = &events[0] {
assert_eq!(start.index, 0); assert_eq!(start.index, 0);
if let crate::llm_client::event::BlockMetadata::ToolUse { id, name } = &start.metadata { if let worker_types::BlockMetadata::ToolUse { id, name } = &start.metadata {
assert_eq!(id, "call_abc"); assert_eq!(id, "call_abc");
assert_eq!(name, "get_weather"); assert_eq!(name, "get_weather");
} else { } else {
@@ -1,23 +1,21 @@
//! OpenAI Request Builder //! OpenAI リクエスト生成
//!
//! Converts Open Responses native Item model to OpenAI Chat Completions API format.
use serde::Serialize; use serde::Serialize;
use serde_json::Value; use serde_json::Value;
use crate::llm_client::{ use crate::llm_client::{
types::{Item, Role, ToolDefinition},
Request, Request,
types::{ContentPart, Message, MessageContent, Role, ToolDefinition},
}; };
use super::OpenAIScheme; use super::OpenAIScheme;
/// OpenAI API request body /// OpenAI APIへのリクエストボディ
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub(crate) struct OpenAIRequest { pub(crate) struct OpenAIRequest {
pub model: String, pub model: String,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub max_completion_tokens: Option<u32>, pub max_completion_tokens: Option<u32>, // max_tokens is deprecated for newer models, generally max_completion_tokens is preferred
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub max_tokens: Option<u32>, // Legacy field for compatibility (e.g. Ollama) pub max_tokens: Option<u32>, // Legacy field for compatibility (e.g. Ollama)
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
@@ -33,7 +31,7 @@ pub(crate) struct OpenAIRequest {
#[serde(skip_serializing_if = "Vec::is_empty")] #[serde(skip_serializing_if = "Vec::is_empty")]
pub tools: Vec<OpenAITool>, pub tools: Vec<OpenAITool>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub tool_choice: Option<String>, pub tool_choice: Option<String>, // "auto", "none", or specific
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
@@ -41,21 +39,20 @@ pub(crate) struct StreamOptions {
pub include_usage: bool, pub include_usage: bool,
} }
/// OpenAI message /// OpenAI メッセージ
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub(crate) struct OpenAIMessage { pub(crate) struct OpenAIMessage {
pub role: String, pub role: String,
pub content: Option<OpenAIContent>, pub content: Option<OpenAIContent>, // Optional for assistant tool calls
#[serde(skip_serializing_if = "Vec::is_empty")] #[serde(skip_serializing_if = "Vec::is_empty")]
pub tool_calls: Vec<OpenAIToolCall>, pub tool_calls: Vec<OpenAIToolCall>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub tool_call_id: Option<String>, pub tool_call_id: Option<String>, // For tool_result (role: tool)
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>, pub name: Option<String>, // Optional name
} }
/// OpenAI content /// OpenAI コンテンツ
#[allow(dead_code)]
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(untagged)] #[serde(untagged)]
pub(crate) enum OpenAIContent { pub(crate) enum OpenAIContent {
@@ -63,7 +60,7 @@ pub(crate) enum OpenAIContent {
Parts(Vec<OpenAIContentPart>), Parts(Vec<OpenAIContentPart>),
} }
/// OpenAI content part /// OpenAI コンテンツパーツ
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(tag = "type")] #[serde(tag = "type")]
@@ -79,7 +76,7 @@ pub(crate) struct ImageUrl {
pub url: String, pub url: String,
} }
/// OpenAI tool definition /// OpenAI ツール定義
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub(crate) struct OpenAITool { pub(crate) struct OpenAITool {
pub r#type: String, pub r#type: String,
@@ -94,7 +91,7 @@ pub(crate) struct OpenAIToolFunction {
pub parameters: Value, pub parameters: Value,
} }
/// OpenAI tool call in message /// OpenAI ツール呼び出し(メッセージ内)
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub(crate) struct OpenAIToolCall { pub(crate) struct OpenAIToolCall {
pub id: String, pub id: String,
@@ -109,11 +106,10 @@ pub(crate) struct OpenAIToolCallFunction {
} }
impl OpenAIScheme { impl OpenAIScheme {
/// Build OpenAI request from Request /// RequestからOpenAIのリクエストボディを構築
pub(crate) fn build_request(&self, model: &str, request: &Request) -> OpenAIRequest { pub(crate) fn build_request(&self, model: &str, request: &Request) -> OpenAIRequest {
let mut messages = Vec::new(); let mut messages = Vec::new();
// Add system message if present
if let Some(system) = &request.system_prompt { if let Some(system) = &request.system_prompt {
messages.push(OpenAIMessage { messages.push(OpenAIMessage {
role: "system".to_string(), role: "system".to_string(),
@@ -124,8 +120,7 @@ impl OpenAIScheme {
}); });
} }
// Convert items to messages messages.extend(request.messages.iter().map(|m| self.convert_message(m)));
messages.extend(self.convert_items_to_messages(&request.items));
let tools = request.tools.iter().map(|t| self.convert_tool(t)).collect(); let tools = request.tools.iter().map(|t| self.convert_tool(t)).collect();
@@ -148,122 +143,106 @@ impl OpenAIScheme {
}), }),
messages, messages,
tools, tools,
tool_choice: None, tool_choice: None, // Default to auto if tools are present? Or let API decide (which is auto)
} }
} }
/// Convert Open Responses Items to OpenAI Messages fn convert_message(&self, message: &Message) -> OpenAIMessage {
/// match &message.content {
/// OpenAI uses a message-based model where: MessageContent::ToolResult {
/// - User messages have role "user" tool_use_id,
/// - Assistant messages have role "assistant" content,
/// - Tool calls are within assistant messages as tool_calls array } => OpenAIMessage {
/// - Tool results have role "tool" with tool_call_id role: "tool".to_string(),
fn convert_items_to_messages(&self, items: &[Item]) -> Vec<OpenAIMessage> { content: Some(OpenAIContent::Text(content.clone())),
let mut messages = Vec::new(); tool_calls: vec![],
let mut pending_tool_calls: Vec<OpenAIToolCall> = Vec::new(); tool_call_id: Some(tool_use_id.clone()),
let mut pending_assistant_text: Option<String> = None; name: None,
},
for item in items { MessageContent::Text(text) => {
match item { let role = match message.role {
Item::Message { role, content, .. } => {
// Flush pending tool calls
self.flush_pending_assistant(
&mut messages,
&mut pending_tool_calls,
&mut pending_assistant_text,
);
let openai_role = match role {
Role::User => "user", Role::User => "user",
Role::Assistant => "assistant", Role::Assistant => "assistant",
Role::System => "system",
}; };
OpenAIMessage {
let text_content: String = content role: role.to_string(),
.iter() content: Some(OpenAIContent::Text(text.clone())),
.map(|p| p.as_text())
.collect::<Vec<_>>()
.join("");
messages.push(OpenAIMessage {
role: openai_role.to_string(),
content: Some(OpenAIContent::Text(text_content)),
tool_calls: vec![], tool_calls: vec![],
tool_call_id: None, tool_call_id: None,
name: None, name: None,
});
} }
}
MessageContent::Parts(parts) => {
let role = match message.role {
Role::User => "user",
Role::Assistant => "assistant",
};
Item::FunctionCall { let mut content_parts = Vec::new();
call_id, let mut tool_calls = Vec::new();
name, let mut is_tool_result = false;
arguments, let mut tool_result_id = None;
.. let mut tool_result_content = String::new();
} => {
pending_tool_calls.push(OpenAIToolCall { for part in parts {
id: call_id.clone(), match part {
ContentPart::Text { text } => {
content_parts.push(OpenAIContentPart::Text { text: text.clone() });
}
ContentPart::ToolUse { id, name, input } => {
tool_calls.push(OpenAIToolCall {
id: id.clone(),
r#type: "function".to_string(), r#type: "function".to_string(),
function: OpenAIToolCallFunction { function: OpenAIToolCallFunction {
name: name.clone(), name: name.clone(),
arguments: arguments.clone(), arguments: input.to_string(),
}, },
}); });
} }
ContentPart::ToolResult {
tool_use_id,
content,
} => {
// OpenAI doesn't support mixed content with ToolResult in the same message easily if not careful
// But strictly speaking, a Message with ToolResult should be its own message with role "tool"
is_tool_result = true;
tool_result_id = Some(tool_use_id.clone());
tool_result_content = content.clone();
}
}
}
Item::FunctionCallOutput { call_id, output, .. } => { if is_tool_result {
// Flush pending tool calls before tool result OpenAIMessage {
self.flush_pending_assistant(
&mut messages,
&mut pending_tool_calls,
&mut pending_assistant_text,
);
messages.push(OpenAIMessage {
role: "tool".to_string(), role: "tool".to_string(),
content: Some(OpenAIContent::Text(output.clone())), content: Some(OpenAIContent::Text(tool_result_content)),
tool_calls: vec![], tool_calls: vec![],
tool_call_id: Some(call_id.clone()), tool_call_id: tool_result_id,
name: None, name: None,
});
} }
Item::Reasoning { text, .. } => {
// Reasoning is treated as assistant text in OpenAI
// (OpenAI doesn't have native reasoning support like Claude)
if let Some(ref mut existing) = pending_assistant_text {
existing.push_str(text);
} else { } else {
pending_assistant_text = Some(text.clone()); let content = if content_parts.is_empty() {
} None
} } else if content_parts.len() == 1 {
} // Simplify single text part to just Text content if preferred, or keep as Parts
if let OpenAIContentPart::Text { text } = &content_parts[0] {
Some(OpenAIContent::Text(text.clone()))
} else {
Some(OpenAIContent::Parts(content_parts))
} }
} else {
Some(OpenAIContent::Parts(content_parts))
};
// Flush remaining pending items OpenAIMessage {
self.flush_pending_assistant( role: role.to_string(),
&mut messages, content,
&mut pending_tool_calls, tool_calls,
&mut pending_assistant_text,
);
messages
}
fn flush_pending_assistant(
&self,
messages: &mut Vec<OpenAIMessage>,
pending_tool_calls: &mut Vec<OpenAIToolCall>,
pending_assistant_text: &mut Option<String>,
) {
if !pending_tool_calls.is_empty() || pending_assistant_text.is_some() {
messages.push(OpenAIMessage {
role: "assistant".to_string(),
content: pending_assistant_text.take().map(OpenAIContent::Text),
tool_calls: std::mem::take(pending_tool_calls),
tool_call_id: None, tool_call_id: None,
name: None, name: None,
}); }
}
}
} }
} }
@@ -295,6 +274,7 @@ mod tests {
assert_eq!(body.messages[0].role, "system"); assert_eq!(body.messages[0].role, "system");
assert_eq!(body.messages[1].role, "user"); assert_eq!(body.messages[1].role, "user");
// Check system content
if let Some(OpenAIContent::Text(text)) = &body.messages[0].content { if let Some(OpenAIContent::Text(text)) = &body.messages[0].content {
assert_eq!(text, "System prompt"); assert_eq!(text, "System prompt");
} else { } else {
@@ -321,39 +301,20 @@ mod tests {
let body = scheme.build_request("llama3", &request); let body = scheme.build_request("llama3", &request);
// max_tokens should be set, max_completion_tokens should be None
assert_eq!(body.max_tokens, Some(100)); assert_eq!(body.max_tokens, Some(100));
assert!(body.max_completion_tokens.is_none()); assert!(body.max_completion_tokens.is_none());
} }
#[test] #[test]
fn test_build_request_modern_max_tokens() { fn test_build_request_modern_max_tokens() {
let scheme = OpenAIScheme::new(); let scheme = OpenAIScheme::new(); // Default matches modern (legacy=false)
let request = Request::new().user("Hello").max_tokens(100); let request = Request::new().user("Hello").max_tokens(100);
let body = scheme.build_request("gpt-4o", &request); let body = scheme.build_request("gpt-4o", &request);
// max_completion_tokens should be set, max_tokens should be None
assert_eq!(body.max_completion_tokens, Some(100)); assert_eq!(body.max_completion_tokens, Some(100));
assert!(body.max_tokens.is_none()); assert!(body.max_tokens.is_none());
} }
#[test]
fn test_function_call_and_output() {
let scheme = OpenAIScheme::new();
let request = Request::new()
.user("Check weather")
.item(Item::function_call(
"call_123",
"get_weather",
r#"{"city":"Tokyo"}"#,
))
.item(Item::function_call_output("call_123", "Sunny, 25°C"));
let body = scheme.build_request("gpt-4o", &request);
assert_eq!(body.messages.len(), 3);
assert_eq!(body.messages[0].role, "user");
assert_eq!(body.messages[1].role, "assistant");
assert_eq!(body.messages[1].tool_calls.len(), 1);
assert_eq!(body.messages[2].role, "tool");
}
} }
+198
View File
@@ -0,0 +1,198 @@
//! LLMクライアント共通型定義
use serde::{Deserialize, Serialize};
/// リクエスト構造体
#[derive(Debug, Clone, Default)]
pub struct Request {
/// システムプロンプト
pub system_prompt: Option<String>,
/// メッセージ履歴
pub messages: Vec<Message>,
/// ツール定義
pub tools: Vec<ToolDefinition>,
/// リクエスト設定
pub config: RequestConfig,
}
impl Request {
/// 新しいリクエストを作成
pub fn new() -> Self {
Self::default()
}
/// システムプロンプトを設定
pub fn system(mut self, prompt: impl Into<String>) -> Self {
self.system_prompt = Some(prompt.into());
self
}
/// ユーザーメッセージを追加
pub fn user(mut self, content: impl Into<String>) -> Self {
self.messages.push(Message::user(content));
self
}
/// アシスタントメッセージを追加
pub fn assistant(mut self, content: impl Into<String>) -> Self {
self.messages.push(Message::assistant(content));
self
}
/// メッセージを追加
pub fn message(mut self, message: Message) -> Self {
self.messages.push(message);
self
}
/// ツールを追加
pub fn tool(mut self, tool: ToolDefinition) -> Self {
self.tools.push(tool);
self
}
/// 設定を適用
pub fn config(mut self, config: RequestConfig) -> Self {
self.config = config;
self
}
/// max_tokensを設定
pub fn max_tokens(mut self, max_tokens: u32) -> Self {
self.config.max_tokens = Some(max_tokens);
self
}
}
/// メッセージ
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
/// ロール
pub role: Role,
/// コンテンツ
pub content: MessageContent,
}
impl Message {
/// ユーザーメッセージを作成
pub fn user(content: impl Into<String>) -> Self {
Self {
role: Role::User,
content: MessageContent::Text(content.into()),
}
}
/// アシスタントメッセージを作成
pub fn assistant(content: impl Into<String>) -> Self {
Self {
role: Role::Assistant,
content: MessageContent::Text(content.into()),
}
}
/// ツール結果メッセージを作成
pub fn tool_result(tool_use_id: impl Into<String>, content: impl Into<String>) -> Self {
Self {
role: Role::User,
content: MessageContent::ToolResult {
tool_use_id: tool_use_id.into(),
content: content.into(),
},
}
}
}
/// ロール
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
User,
Assistant,
}
/// メッセージコンテンツ
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum MessageContent {
/// テキストコンテンツ
Text(String),
/// ツール結果
ToolResult {
tool_use_id: String,
content: String,
},
/// 複合コンテンツ (テキスト + ツール使用等)
Parts(Vec<ContentPart>),
}
/// コンテンツパーツ
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ContentPart {
/// テキスト
#[serde(rename = "text")]
Text { text: String },
/// ツール使用
#[serde(rename = "tool_use")]
ToolUse {
id: String,
name: String,
input: serde_json::Value,
},
/// ツール結果
#[serde(rename = "tool_result")]
ToolResult {
tool_use_id: String,
content: String,
},
}
/// ツール定義
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolDefinition {
/// ツール名
pub name: String,
/// 説明
pub description: Option<String>,
/// 入力スキーマ (JSON Schema)
pub input_schema: serde_json::Value,
}
impl ToolDefinition {
/// 新しいツール定義を作成
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
description: None,
input_schema: serde_json::json!({
"type": "object",
"properties": {}
}),
}
}
/// 説明を設定
pub fn description(mut self, desc: impl Into<String>) -> Self {
self.description = Some(desc.into());
self
}
/// 入力スキーマを設定
pub fn input_schema(mut self, schema: serde_json::Value) -> Self {
self.input_schema = schema;
self
}
}
/// リクエスト設定
#[derive(Debug, Clone, Default)]
pub struct RequestConfig {
/// 最大トークン数
pub max_tokens: Option<u32>,
/// Temperature
pub temperature: Option<f32>,
/// Top P
pub top_p: Option<f32>,
/// ストップシーケンス
pub stop_sequences: Vec<String>,
}
@@ -1,154 +1,23 @@
//! Event Subscription //! WorkerSubscriber統合
//! //!
//! Trait for receiving streaming events from LLM in real-time. //! WorkerSubscriberをTimeline層のHandlerとしてブリッジする実装
//! Used for stream display to UI and progress display.
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use crate::{ use worker_types::{
handler::{ ErrorEvent, ErrorKind, Handler, StatusEvent, StatusKind, TextBlockEvent, TextBlockKind,
ErrorKind, Handler, StatusKind, TextBlockEvent, TextBlockKind, ToolUseBlockEvent, ToolCall, ToolUseBlockEvent, ToolUseBlockKind, UsageEvent, UsageKind, WorkerSubscriber,
ToolUseBlockKind, UsageKind,
},
hook::ToolCall,
timeline::event::{ErrorEvent, StatusEvent, UsageEvent},
}; };
// ============================================================================= // =============================================================================
// WorkerSubscriber Trait // SubscriberAdapter - WorkerSubscriberをTimelineハンドラにブリッジ
// =============================================================================
/// Trait for subscribing to streaming events from LLM
///
/// When registered with Worker, you can receive events from text generation
/// and tool calls in real-time. Ideal for stream display to UI.
///
/// # Available Events
///
/// - **Block events**: Text, tool use (with scope)
/// - **Meta events**: Usage, status, error
/// - **Completion events**: Text complete, tool call complete
/// - **Turn control**: Turn start, turn end
///
/// # Examples
///
/// ```ignore
/// use llm_worker::subscriber::WorkerSubscriber;
/// use llm_worker::timeline::TextBlockEvent;
///
/// struct StreamPrinter;
///
/// impl WorkerSubscriber for StreamPrinter {
/// type TextBlockScope = ();
/// type ToolUseBlockScope = ();
///
/// fn on_text_block(&mut self, _: &mut (), event: &TextBlockEvent) {
/// if let TextBlockEvent::Delta(text) = event {
/// print!("{}", text); // Real-time output
/// }
/// }
///
/// fn on_text_complete(&mut self, text: &str) {
/// println!("\n--- Complete: {} chars ---", text.len());
/// }
/// }
///
/// // Register with Worker
/// worker.subscribe(StreamPrinter);
/// ```
pub trait WorkerSubscriber: Send {
// =========================================================================
// Scope Types (for block events)
// =========================================================================
/// Scope type for text block processing
///
/// Generated with Default::default() at block start,
/// destroyed at block end.
type TextBlockScope: Default + Send + Sync;
/// Scope type for tool use block processing
type ToolUseBlockScope: Default + Send + Sync;
// =========================================================================
// Block Events (with scope management)
// =========================================================================
/// Text block event
///
/// Has Start/Delta/Stop lifecycle.
/// Scope is generated at block start and destroyed at end.
#[allow(unused_variables)]
fn on_text_block(&mut self, scope: &mut Self::TextBlockScope, event: &TextBlockEvent) {}
/// Tool use block event
///
/// Has Start/InputJsonDelta/Stop lifecycle.
#[allow(unused_variables)]
fn on_tool_use_block(
&mut self,
scope: &mut Self::ToolUseBlockScope,
event: &ToolUseBlockEvent,
) {
}
// =========================================================================
// Single Events (no scope needed)
// =========================================================================
/// Usage event
#[allow(unused_variables)]
fn on_usage(&mut self, event: &UsageEvent) {}
/// Status event
#[allow(unused_variables)]
fn on_status(&mut self, event: &StatusEvent) {}
/// Error event
#[allow(unused_variables)]
fn on_error(&mut self, event: &ErrorEvent) {}
// =========================================================================
// Accumulated Events (added in Worker layer)
// =========================================================================
/// Text complete event
///
/// When a text block completes, the entire accumulated text is passed.
/// Convenient for receiving the final result after block processing.
#[allow(unused_variables)]
fn on_text_complete(&mut self, text: &str) {}
/// Tool call complete event
///
/// When a tool use block completes, the complete ToolCall is passed.
#[allow(unused_variables)]
fn on_tool_call_complete(&mut self, call: &ToolCall) {}
// =========================================================================
// Turn Control
// =========================================================================
/// On turn start
///
/// `turn` is a 0-based turn number.
#[allow(unused_variables)]
fn on_turn_start(&mut self, turn: usize) {}
/// On turn end
#[allow(unused_variables)]
fn on_turn_end(&mut self, turn: usize) {}
}
// =============================================================================
// SubscriberAdapter - Bridge WorkerSubscriber to Timeline handlers
// ============================================================================= // =============================================================================
// ============================================================================= // =============================================================================
// TextBlock Handler Adapter // TextBlock Handler Adapter
// ============================================================================= // =============================================================================
/// Subscriber adapter for TextBlockKind /// TextBlockKind用のSubscriberアダプター
pub(crate) struct TextBlockSubscriberAdapter<S: WorkerSubscriber> { pub(crate) struct TextBlockSubscriberAdapter<S: WorkerSubscriber> {
subscriber: Arc<Mutex<S>>, subscriber: Arc<Mutex<S>>,
} }
@@ -167,10 +36,10 @@ impl<S: WorkerSubscriber> Clone for TextBlockSubscriberAdapter<S> {
} }
} }
/// Wrapper for TextBlock scope /// TextBlockのスコープをラップ
pub struct TextBlockScopeWrapper<S: WorkerSubscriber> { pub struct TextBlockScopeWrapper<S: WorkerSubscriber> {
inner: S::TextBlockScope, inner: S::TextBlockScope,
buffer: String, // Buffer for on_text_complete buffer: String, // on_text_complete用のバッファ
} }
impl<S: WorkerSubscriber> Default for TextBlockScopeWrapper<S> { impl<S: WorkerSubscriber> Default for TextBlockScopeWrapper<S> {
@@ -186,16 +55,16 @@ impl<S: WorkerSubscriber + 'static> Handler<TextBlockKind> for TextBlockSubscrib
type Scope = TextBlockScopeWrapper<S>; type Scope = TextBlockScopeWrapper<S>;
fn on_event(&mut self, scope: &mut Self::Scope, event: &TextBlockEvent) { fn on_event(&mut self, scope: &mut Self::Scope, event: &TextBlockEvent) {
// Accumulate deltas into buffer // Deltaの場合はバッファに蓄積
if let TextBlockEvent::Delta(text) = event { if let TextBlockEvent::Delta(text) = event {
scope.buffer.push_str(text); scope.buffer.push_str(text);
} }
// Call Subscriber's TextBlock event handler // SubscriberTextBlockイベントハンドラを呼び出し
if let Ok(mut subscriber) = self.subscriber.lock() { if let Ok(mut subscriber) = self.subscriber.lock() {
subscriber.on_text_block(&mut scope.inner, event); subscriber.on_text_block(&mut scope.inner, event);
// Also call on_text_complete on Stop // Stopの場合はon_text_completeも呼び出し
if matches!(event, TextBlockEvent::Stop(_)) { if matches!(event, TextBlockEvent::Stop(_)) {
subscriber.on_text_complete(&scope.buffer); subscriber.on_text_complete(&scope.buffer);
} }
@@ -207,7 +76,7 @@ impl<S: WorkerSubscriber + 'static> Handler<TextBlockKind> for TextBlockSubscrib
// ToolUseBlock Handler Adapter // ToolUseBlock Handler Adapter
// ============================================================================= // =============================================================================
/// Subscriber adapter for ToolUseBlockKind /// ToolUseBlockKind用のSubscriberアダプター
pub(crate) struct ToolUseBlockSubscriberAdapter<S: WorkerSubscriber> { pub(crate) struct ToolUseBlockSubscriberAdapter<S: WorkerSubscriber> {
subscriber: Arc<Mutex<S>>, subscriber: Arc<Mutex<S>>,
} }
@@ -226,12 +95,12 @@ impl<S: WorkerSubscriber> Clone for ToolUseBlockSubscriberAdapter<S> {
} }
} }
/// Wrapper for ToolUseBlock scope /// ToolUseBlockのスコープをラップ
pub struct ToolUseBlockScopeWrapper<S: WorkerSubscriber> { pub struct ToolUseBlockScopeWrapper<S: WorkerSubscriber> {
inner: S::ToolUseBlockScope, inner: S::ToolUseBlockScope,
id: String, id: String,
name: String, name: String,
input_json: String, // JSON accumulation input_json: String, // JSON蓄積用
} }
impl<S: WorkerSubscriber> Default for ToolUseBlockScopeWrapper<S> { impl<S: WorkerSubscriber> Default for ToolUseBlockScopeWrapper<S> {
@@ -249,22 +118,22 @@ impl<S: WorkerSubscriber + 'static> Handler<ToolUseBlockKind> for ToolUseBlockSu
type Scope = ToolUseBlockScopeWrapper<S>; type Scope = ToolUseBlockScopeWrapper<S>;
fn on_event(&mut self, scope: &mut Self::Scope, event: &ToolUseBlockEvent) { fn on_event(&mut self, scope: &mut Self::Scope, event: &ToolUseBlockEvent) {
// Save metadata on Start // Start時にメタデータを保存
if let ToolUseBlockEvent::Start(start) = event { if let ToolUseBlockEvent::Start(start) = event {
scope.id = start.id.clone(); scope.id = start.id.clone();
scope.name = start.name.clone(); scope.name = start.name.clone();
} }
// Accumulate InputJsonDelta into buffer // InputJsonDeltaの場合はバッファに蓄積
if let ToolUseBlockEvent::InputJsonDelta(json) = event { if let ToolUseBlockEvent::InputJsonDelta(json) = event {
scope.input_json.push_str(json); scope.input_json.push_str(json);
} }
// Call Subscriber's ToolUseBlock event handler // SubscriberToolUseBlockイベントハンドラを呼び出し
if let Ok(mut subscriber) = self.subscriber.lock() { if let Ok(mut subscriber) = self.subscriber.lock() {
subscriber.on_tool_use_block(&mut scope.inner, event); subscriber.on_tool_use_block(&mut scope.inner, event);
// Also call on_tool_call_complete on Stop // Stopの場合はon_tool_call_completeも呼び出し
if matches!(event, ToolUseBlockEvent::Stop(_)) { if matches!(event, ToolUseBlockEvent::Stop(_)) {
let input: serde_json::Value = let input: serde_json::Value =
serde_json::from_str(&scope.input_json).unwrap_or_default(); serde_json::from_str(&scope.input_json).unwrap_or_default();
@@ -283,7 +152,7 @@ impl<S: WorkerSubscriber + 'static> Handler<ToolUseBlockKind> for ToolUseBlockSu
// Meta Event Handler Adapters // Meta Event Handler Adapters
// ============================================================================= // =============================================================================
/// Subscriber adapter for UsageKind /// UsageKind用のSubscriberアダプター
pub(crate) struct UsageSubscriberAdapter<S: WorkerSubscriber> { pub(crate) struct UsageSubscriberAdapter<S: WorkerSubscriber> {
subscriber: Arc<Mutex<S>>, subscriber: Arc<Mutex<S>>,
} }
@@ -312,7 +181,7 @@ impl<S: WorkerSubscriber + 'static> Handler<UsageKind> for UsageSubscriberAdapte
} }
} }
/// Subscriber adapter for StatusKind /// StatusKind用のSubscriberアダプター
pub(crate) struct StatusSubscriberAdapter<S: WorkerSubscriber> { pub(crate) struct StatusSubscriberAdapter<S: WorkerSubscriber> {
subscriber: Arc<Mutex<S>>, subscriber: Arc<Mutex<S>>,
} }
@@ -341,7 +210,7 @@ impl<S: WorkerSubscriber + 'static> Handler<StatusKind> for StatusSubscriberAdap
} }
} }
/// Subscriber adapter for ErrorKind /// ErrorKind用のSubscriberアダプター
pub(crate) struct ErrorSubscriberAdapter<S: WorkerSubscriber> { pub(crate) struct ErrorSubscriberAdapter<S: WorkerSubscriber> {
subscriber: Arc<Mutex<S>>, subscriber: Arc<Mutex<S>>,
} }
@@ -9,39 +9,25 @@
//! - [`TextBlockCollector`] - テキストブロックを収集するHandler //! - [`TextBlockCollector`] - テキストブロックを収集するHandler
//! - [`ToolCallCollector`] - ツール呼び出しを収集するHandler //! - [`ToolCallCollector`] - ツール呼び出しを収集するHandler
pub mod event;
mod text_block_collector; mod text_block_collector;
mod timeline; mod timeline;
mod tool_call_collector; mod tool_call_collector;
// 公開API // 公開API
pub use event::*;
pub use text_block_collector::TextBlockCollector; pub use text_block_collector::TextBlockCollector;
pub use timeline::{ErasedHandler, HandlerWrapper, Timeline}; pub use timeline::{ErasedHandler, HandlerWrapper, Timeline};
pub use tool_call_collector::ToolCallCollector; pub use tool_call_collector::ToolCallCollector;
// 型定義からのre-export // worker-typesからのre-export
pub use crate::handler::{ pub use worker_types::{
// Meta Kinds
ErrorKind,
// Core traits // Core traits
Handler, Handler, Kind,
Kind,
PingKind,
StatusKind,
// Block Events
TextBlockEvent,
// Block Kinds // Block Kinds
TextBlockKind, TextBlockKind, ThinkingBlockKind, ToolUseBlockKind,
TextBlockStart, // Block Events
TextBlockStop, TextBlockEvent, TextBlockStart, TextBlockStop,
ThinkingBlockEvent, ThinkingBlockEvent, ThinkingBlockStart, ThinkingBlockStop,
ThinkingBlockKind, ToolUseBlockEvent, ToolUseBlockStart, ToolUseBlockStop,
ThinkingBlockStart, // Meta Kinds
ThinkingBlockStop, ErrorKind, PingKind, StatusKind, UsageKind,
ToolUseBlockEvent,
ToolUseBlockKind,
ToolUseBlockStart,
ToolUseBlockStop,
UsageKind,
}; };
@@ -3,8 +3,8 @@
//! TimelineのTextBlockHandler として登録され、 //! TimelineのTextBlockHandler として登録され、
//! ストリーム中のテキストブロックを収集する。 //! ストリーム中のテキストブロックを収集する。
use crate::handler::{Handler, TextBlockEvent, TextBlockKind};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use worker_types::{Handler, TextBlockEvent, TextBlockKind};
/// TextBlockから収集したテキスト情報を保持 /// TextBlockから収集したテキスト情報を保持
#[derive(Debug, Default)] #[derive(Debug, Default)]
@@ -85,7 +85,7 @@ impl Handler<TextBlockKind> for TextBlockCollector {
mod tests { mod tests {
use super::*; use super::*;
use crate::timeline::Timeline; use crate::timeline::Timeline;
use crate::timeline::event::Event; use worker_types::Event;
/// TextBlockCollectorが単一のテキストブロックを正しく収集することを確認 /// TextBlockCollectorが単一のテキストブロックを正しく収集することを確認
#[test] #[test]
@@ -5,8 +5,7 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use super::event::*; use worker_types::*;
use crate::handler::*;
// ============================================================================= // =============================================================================
// Type-erased Handler // Type-erased Handler
@@ -17,7 +16,7 @@ use crate::handler::*;
/// 各Handlerは独自のScope型を持つため、Timelineで保持するには型消去が必要です。 /// 各Handlerは独自のScope型を持つため、Timelineで保持するには型消去が必要です。
/// 通常は直接使用せず、`Timeline::on_text_block()`などのメソッド経由で /// 通常は直接使用せず、`Timeline::on_text_block()`などのメソッド経由で
/// 自動的にラップされます。 /// 自動的にラップされます。
pub trait ErasedHandler<K: Kind>: Send + Sync { pub trait ErasedHandler<K: Kind>: Send {
/// イベントをディスパッチ /// イベントをディスパッチ
fn dispatch(&mut self, event: &K::Event); fn dispatch(&mut self, event: &K::Event);
/// スコープを開始(Block開始時) /// スコープを開始(Block開始時)
@@ -54,9 +53,9 @@ where
impl<H, K> ErasedHandler<K> for HandlerWrapper<H, K> impl<H, K> ErasedHandler<K> for HandlerWrapper<H, K>
where where
H: Handler<K> + Send + Sync, H: Handler<K> + Send,
K: Kind, K: Kind,
H::Scope: Send + Sync, H::Scope: Send,
{ {
fn dispatch(&mut self, event: &K::Event) { fn dispatch(&mut self, event: &K::Event) {
if let Some(scope) = &mut self.scope { if let Some(scope) = &mut self.scope {
@@ -78,7 +77,7 @@ where
// ============================================================================= // =============================================================================
/// ブロックハンドラーの型消去trait /// ブロックハンドラーの型消去trait
trait ErasedBlockHandler: Send + Sync { trait ErasedBlockHandler: Send {
fn dispatch_start(&mut self, start: &BlockStart); fn dispatch_start(&mut self, start: &BlockStart);
fn dispatch_delta(&mut self, delta: &BlockDelta); fn dispatch_delta(&mut self, delta: &BlockDelta);
fn dispatch_stop(&mut self, stop: &BlockStop); fn dispatch_stop(&mut self, stop: &BlockStop);
@@ -112,8 +111,8 @@ where
impl<H> ErasedBlockHandler for TextBlockHandlerWrapper<H> impl<H> ErasedBlockHandler for TextBlockHandlerWrapper<H>
where where
H: Handler<TextBlockKind> + Send + Sync, H: Handler<TextBlockKind> + Send,
H::Scope: Send + Sync, H::Scope: Send,
{ {
fn dispatch_start(&mut self, start: &BlockStart) { fn dispatch_start(&mut self, start: &BlockStart) {
if let Some(scope) = &mut self.scope { if let Some(scope) = &mut self.scope {
@@ -185,8 +184,8 @@ where
impl<H> ErasedBlockHandler for ThinkingBlockHandlerWrapper<H> impl<H> ErasedBlockHandler for ThinkingBlockHandlerWrapper<H>
where where
H: Handler<ThinkingBlockKind> + Send + Sync, H: Handler<ThinkingBlockKind> + Send,
H::Scope: Send + Sync, H::Scope: Send,
{ {
fn dispatch_start(&mut self, start: &BlockStart) { fn dispatch_start(&mut self, start: &BlockStart) {
if let Some(scope) = &mut self.scope { if let Some(scope) = &mut self.scope {
@@ -255,8 +254,8 @@ where
impl<H> ErasedBlockHandler for ToolUseBlockHandlerWrapper<H> impl<H> ErasedBlockHandler for ToolUseBlockHandlerWrapper<H>
where where
H: Handler<ToolUseBlockKind> + Send + Sync, H: Handler<ToolUseBlockKind> + Send,
H::Scope: Send + Sync, H::Scope: Send,
{ {
fn dispatch_start(&mut self, start: &BlockStart) { fn dispatch_start(&mut self, start: &BlockStart) {
if let Some(scope) = &mut self.scope { if let Some(scope) = &mut self.scope {
@@ -328,7 +327,7 @@ where
/// # Examples /// # Examples
/// ///
/// ```ignore /// ```ignore
/// use llm_worker::{Timeline, Handler, TextBlockKind, TextBlockEvent}; /// use worker::{Timeline, Handler, TextBlockKind, TextBlockEvent};
/// ///
/// struct MyHandler; /// struct MyHandler;
/// impl Handler<TextBlockKind> for MyHandler { /// impl Handler<TextBlockKind> for MyHandler {
@@ -391,8 +390,8 @@ impl Timeline {
/// UsageKind用のHandlerを登録 /// UsageKind用のHandlerを登録
pub fn on_usage<H>(&mut self, handler: H) -> &mut Self pub fn on_usage<H>(&mut self, handler: H) -> &mut Self
where where
H: Handler<UsageKind> + Send + Sync + 'static, H: Handler<UsageKind> + Send + 'static,
H::Scope: Send + Sync, H::Scope: Send,
{ {
// Meta系はデフォルトでスコープを開始しておく // Meta系はデフォルトでスコープを開始しておく
let mut wrapper = HandlerWrapper::new(handler); let mut wrapper = HandlerWrapper::new(handler);
@@ -404,8 +403,8 @@ impl Timeline {
/// PingKind用のHandlerを登録 /// PingKind用のHandlerを登録
pub fn on_ping<H>(&mut self, handler: H) -> &mut Self pub fn on_ping<H>(&mut self, handler: H) -> &mut Self
where where
H: Handler<PingKind> + Send + Sync + 'static, H: Handler<PingKind> + Send + 'static,
H::Scope: Send + Sync, H::Scope: Send,
{ {
let mut wrapper = HandlerWrapper::new(handler); let mut wrapper = HandlerWrapper::new(handler);
wrapper.start_scope(); wrapper.start_scope();
@@ -416,8 +415,8 @@ impl Timeline {
/// StatusKind用のHandlerを登録 /// StatusKind用のHandlerを登録
pub fn on_status<H>(&mut self, handler: H) -> &mut Self pub fn on_status<H>(&mut self, handler: H) -> &mut Self
where where
H: Handler<StatusKind> + Send + Sync + 'static, H: Handler<StatusKind> + Send + 'static,
H::Scope: Send + Sync, H::Scope: Send,
{ {
let mut wrapper = HandlerWrapper::new(handler); let mut wrapper = HandlerWrapper::new(handler);
wrapper.start_scope(); wrapper.start_scope();
@@ -428,8 +427,8 @@ impl Timeline {
/// ErrorKind用のHandlerを登録 /// ErrorKind用のHandlerを登録
pub fn on_error<H>(&mut self, handler: H) -> &mut Self pub fn on_error<H>(&mut self, handler: H) -> &mut Self
where where
H: Handler<ErrorKind> + Send + Sync + 'static, H: Handler<ErrorKind> + Send + 'static,
H::Scope: Send + Sync, H::Scope: Send,
{ {
let mut wrapper = HandlerWrapper::new(handler); let mut wrapper = HandlerWrapper::new(handler);
wrapper.start_scope(); wrapper.start_scope();
@@ -440,8 +439,8 @@ impl Timeline {
/// TextBlockKind用のHandlerを登録 /// TextBlockKind用のHandlerを登録
pub fn on_text_block<H>(&mut self, handler: H) -> &mut Self pub fn on_text_block<H>(&mut self, handler: H) -> &mut Self
where where
H: Handler<TextBlockKind> + Send + Sync + 'static, H: Handler<TextBlockKind> + Send + 'static,
H::Scope: Send + Sync, H::Scope: Send,
{ {
self.text_block_handlers self.text_block_handlers
.push(Box::new(TextBlockHandlerWrapper::new(handler))); .push(Box::new(TextBlockHandlerWrapper::new(handler)));
@@ -451,8 +450,8 @@ impl Timeline {
/// ThinkingBlockKind用のHandlerを登録 /// ThinkingBlockKind用のHandlerを登録
pub fn on_thinking_block<H>(&mut self, handler: H) -> &mut Self pub fn on_thinking_block<H>(&mut self, handler: H) -> &mut Self
where where
H: Handler<ThinkingBlockKind> + Send + Sync + 'static, H: Handler<ThinkingBlockKind> + Send + 'static,
H::Scope: Send + Sync, H::Scope: Send,
{ {
self.thinking_block_handlers self.thinking_block_handlers
.push(Box::new(ThinkingBlockHandlerWrapper::new(handler))); .push(Box::new(ThinkingBlockHandlerWrapper::new(handler)));
@@ -462,8 +461,8 @@ impl Timeline {
/// ToolUseBlockKind用のHandlerを登録 /// ToolUseBlockKind用のHandlerを登録
pub fn on_tool_use_block<H>(&mut self, handler: H) -> &mut Self pub fn on_tool_use_block<H>(&mut self, handler: H) -> &mut Self
where where
H: Handler<ToolUseBlockKind> + Send + Sync + 'static, H: Handler<ToolUseBlockKind> + Send + 'static,
H::Scope: Send + Sync, H::Scope: Send,
{ {
self.tool_use_block_handlers self.tool_use_block_handlers
.push(Box::new(ToolUseBlockHandlerWrapper::new(handler))); .push(Box::new(ToolUseBlockHandlerWrapper::new(handler)));
@@ -578,21 +577,6 @@ impl Timeline {
pub fn current_block(&self) -> Option<BlockType> { pub fn current_block(&self) -> Option<BlockType> {
self.current_block self.current_block
} }
/// 現在アクティブなブロックを中断する
///
/// キャンセルやエラー時に呼び出し、進行中のブロックに対して
/// BlockAbortイベントを発火してスコープをクリーンアップする。
pub fn abort_current_block(&mut self) {
if let Some(block_type) = self.current_block {
let abort = crate::timeline::event::BlockAbort {
index: 0, // インデックスは不明なので0
block_type,
reason: "Cancelled".to_string(),
};
self.handle_block_abort(&abort);
}
}
} }
#[cfg(test)] #[cfg(test)]
@@ -3,11 +3,8 @@
//! TimelineのToolUseBlockHandler として登録され、 //! TimelineのToolUseBlockHandler として登録され、
//! ストリーム中のToolUseブロックを収集する。 //! ストリーム中のToolUseブロックを収集する。
use crate::{
handler::{Handler, ToolUseBlockEvent, ToolUseBlockKind},
hook::ToolCall,
};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use worker_types::{Handler, ToolCall, ToolUseBlockEvent, ToolUseBlockKind};
/// ToolUseブロックから収集したツール呼び出し情報を保持 /// ToolUseブロックから収集したツール呼び出し情報を保持
/// ///
@@ -101,7 +98,7 @@ impl Handler<ToolUseBlockKind> for ToolCallCollector {
mod tests { mod tests {
use super::*; use super::*;
use crate::timeline::Timeline; use crate::timeline::Timeline;
use crate::timeline::event::Event; use worker_types::Event;
#[test] #[test]
fn test_collect_single_tool_call() { fn test_collect_single_tool_call() {
+789
View File
@@ -0,0 +1,789 @@
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::{Arc, Mutex};
use futures::StreamExt;
use tracing::{debug, info, trace, warn};
use crate::timeline::{TextBlockCollector, Timeline, ToolCallCollector};
use crate::llm_client::{ClientError, LlmClient, Request, ToolDefinition};
use crate::subscriber_adapter::{
ErrorSubscriberAdapter, StatusSubscriberAdapter, TextBlockSubscriberAdapter,
ToolUseBlockSubscriberAdapter, UsageSubscriberAdapter,
};
use worker_types::{
ContentPart, ControlFlow, HookError, Locked, Message, MessageContent, Mutable, Tool, ToolCall,
ToolError, ToolResult, TurnResult, WorkerHook, WorkerState, WorkerSubscriber,
};
// =============================================================================
// Worker Error
// =============================================================================
/// Workerエラー
#[derive(Debug, thiserror::Error)]
pub enum WorkerError {
/// クライアントエラー
#[error("Client error: {0}")]
Client(#[from] ClientError),
/// ツールエラー
#[error("Tool error: {0}")]
Tool(#[from] ToolError),
/// Hookエラー
#[error("Hook error: {0}")]
Hook(#[from] HookError),
/// 処理が中断された
#[error("Aborted: {0}")]
Aborted(String),
}
// =============================================================================
// Worker Config
// =============================================================================
/// Worker設定
#[derive(Debug, Clone, Default)]
pub struct WorkerConfig {
// 将来の拡張用(現在は空)
_private: (),
}
// =============================================================================
// ターン制御用コールバック保持
// =============================================================================
/// ターンイベントを通知するためのコールバック (型消去)
trait TurnNotifier: Send {
fn on_turn_start(&self, turn: usize);
fn on_turn_end(&self, turn: usize);
}
struct SubscriberTurnNotifier<S: WorkerSubscriber + 'static> {
subscriber: Arc<Mutex<S>>,
}
impl<S: WorkerSubscriber + 'static> TurnNotifier for SubscriberTurnNotifier<S> {
fn on_turn_start(&self, turn: usize) {
if let Ok(mut s) = self.subscriber.lock() {
s.on_turn_start(turn);
}
}
fn on_turn_end(&self, turn: usize) {
if let Ok(mut s) = self.subscriber.lock() {
s.on_turn_end(turn);
}
}
}
// =============================================================================
// Worker
// =============================================================================
/// LLMとの対話を管理する中心コンポーネント
///
/// ユーザーからの入力を受け取り、LLMにリクエストを送信し、
/// ツール呼び出しがあれば自動的に実行してターンを進行させます。
///
/// # 状態遷移(Type-state
///
/// - [`Mutable`]: 初期状態。システムプロンプトや履歴を自由に編集可能。
/// - [`Locked`]: キャッシュ保護状態。`lock()`で遷移。前方コンテキストは不変。
///
/// # Examples
///
/// ```ignore
/// use worker::{Worker, Message};
///
/// // Workerを作成してツールを登録
/// let mut worker = Worker::new(client)
/// .system_prompt("You are a helpful assistant.");
/// worker.register_tool(my_tool);
///
/// // 対話を実行
/// let history = worker.run("Hello!").await?;
/// ```
///
/// # キャッシュ保護が必要な場合
///
/// ```ignore
/// let mut worker = Worker::new(client)
/// .system_prompt("...");
///
/// // 履歴を設定後、ロックしてキャッシュを保護
/// let mut locked = worker.lock();
/// locked.run("user input").await?;
/// ```
pub struct Worker<C: LlmClient, S: WorkerState = Mutable> {
/// LLMクライアント
client: C,
/// イベントタイムライン
timeline: Timeline,
/// テキストブロックコレクター(Timeline用ハンドラ)
text_block_collector: TextBlockCollector,
/// ツールコールコレクター(Timeline用ハンドラ)
tool_call_collector: ToolCallCollector,
/// 登録されたツール
tools: HashMap<String, Arc<dyn Tool>>,
/// 登録されたHook
hooks: Vec<Box<dyn WorkerHook>>,
/// システムプロンプト
system_prompt: Option<String>,
/// メッセージ履歴(Workerが所有)
history: Vec<Message>,
/// ロック時点での履歴長(Locked状態でのみ意味を持つ)
locked_prefix_len: usize,
/// ターンカウント
turn_count: usize,
/// ターン通知用のコールバック
turn_notifiers: Vec<Box<dyn TurnNotifier>>,
/// 状態マーカー
_state: PhantomData<S>,
}
// =============================================================================
// 共通実装(全状態で利用可能)
// =============================================================================
impl<C: LlmClient, S: WorkerState> Worker<C, S> {
/// イベント購読者を登録する
///
/// 登録したSubscriberは、LLMからのストリーミングイベントを
/// リアルタイムで受信できます。UIへのストリーム表示などに利用します。
///
/// # 受信できるイベント
///
/// - **ブロックイベント**: `on_text_block`, `on_tool_use_block`
/// - **メタイベント**: `on_usage`, `on_status`, `on_error`
/// - **完了イベント**: `on_text_complete`, `on_tool_call_complete`
/// - **ターン制御**: `on_turn_start`, `on_turn_end`
///
/// # Examples
///
/// ```ignore
/// use worker::{Worker, WorkerSubscriber, TextBlockEvent};
///
/// struct MyPrinter;
/// impl WorkerSubscriber for MyPrinter {
/// type TextBlockScope = ();
/// type ToolUseBlockScope = ();
///
/// fn on_text_block(&mut self, _: &mut (), event: &TextBlockEvent) {
/// if let TextBlockEvent::Delta(text) = event {
/// print!("{}", text);
/// }
/// }
/// }
///
/// worker.subscribe(MyPrinter);
/// ```
pub fn subscribe<Sub: WorkerSubscriber + 'static>(&mut self, subscriber: Sub) {
let subscriber = Arc::new(Mutex::new(subscriber));
// TextBlock用ハンドラを登録
self.timeline
.on_text_block(TextBlockSubscriberAdapter::new(subscriber.clone()));
// ToolUseBlock用ハンドラを登録
self.timeline
.on_tool_use_block(ToolUseBlockSubscriberAdapter::new(subscriber.clone()));
// Meta系ハンドラを登録
self.timeline
.on_usage(UsageSubscriberAdapter::new(subscriber.clone()));
self.timeline
.on_status(StatusSubscriberAdapter::new(subscriber.clone()));
self.timeline
.on_error(ErrorSubscriberAdapter::new(subscriber.clone()));
// ターン制御用コールバックを登録
self.turn_notifiers
.push(Box::new(SubscriberTurnNotifier { subscriber }));
}
/// ツールを登録する
///
/// 登録されたツールはLLMからの呼び出しで自動的に実行されます。
/// 同名のツールを登録した場合、後から登録したものが優先されます。
///
/// # Examples
///
/// ```ignore
/// use worker::Worker;
/// use my_tools::SearchTool;
///
/// worker.register_tool(SearchTool::new());
/// ```
pub fn register_tool(&mut self, tool: impl Tool + 'static) {
let name = tool.name().to_string();
self.tools.insert(name, Arc::new(tool));
}
/// 複数のツールを登録
pub fn register_tools(&mut self, tools: impl IntoIterator<Item = impl Tool + 'static>) {
for tool in tools {
self.register_tool(tool);
}
}
/// Hookを追加する
///
/// Hookはターンの進行・ツール実行に介入できます。
/// 複数のHookを登録した場合、登録順に実行されます。
///
/// # Examples
///
/// ```ignore
/// use worker::{Worker, WorkerHook, ControlFlow, ToolCall};
///
/// struct LoggingHook;
///
/// #[async_trait::async_trait]
/// impl WorkerHook for LoggingHook {
/// async fn before_tool_call(&self, call: &mut ToolCall) -> Result<ControlFlow, HookError> {
/// println!("Calling tool: {}", call.name);
/// Ok(ControlFlow::Continue)
/// }
/// }
///
/// worker.add_hook(LoggingHook);
/// ```
pub fn add_hook(&mut self, hook: impl WorkerHook + 'static) {
self.hooks.push(Box::new(hook));
}
/// タイムラインへの可変参照を取得(追加ハンドラ登録用)
pub fn timeline_mut(&mut self) -> &mut Timeline {
&mut self.timeline
}
/// 履歴への参照を取得
pub fn history(&self) -> &[Message] {
&self.history
}
/// システムプロンプトへの参照を取得
pub fn get_system_prompt(&self) -> Option<&str> {
self.system_prompt.as_deref()
}
/// 現在のターンカウントを取得
pub fn turn_count(&self) -> usize {
self.turn_count
}
/// 登録されたツールからToolDefinitionのリストを生成
fn build_tool_definitions(&self) -> Vec<ToolDefinition> {
self.tools
.values()
.map(|tool| {
ToolDefinition::new(tool.name())
.description(tool.description())
.input_schema(tool.input_schema())
})
.collect()
}
/// テキストブロックとツール呼び出しからアシスタントメッセージを構築
fn build_assistant_message(
&self,
text_blocks: &[String],
tool_calls: &[ToolCall],
) -> Option<Message> {
// テキストもツール呼び出しもない場合はNone
if text_blocks.is_empty() && tool_calls.is_empty() {
return None;
}
// テキストのみの場合はシンプルなテキストメッセージ
if tool_calls.is_empty() {
let text = text_blocks.join("");
return Some(Message::assistant(text));
}
// ツール呼び出しがある場合は Parts として構築
let mut parts = Vec::new();
// テキストパーツを追加
for text in text_blocks {
if !text.is_empty() {
parts.push(ContentPart::Text { text: text.clone() });
}
}
// ツール呼び出しパーツを追加
for call in tool_calls {
parts.push(ContentPart::ToolUse {
id: call.id.clone(),
name: call.name.clone(),
input: call.input.clone(),
});
}
Some(Message {
role: worker_types::Role::Assistant,
content: MessageContent::Parts(parts),
})
}
/// リクエストを構築
fn build_request(&self, tool_definitions: &[ToolDefinition]) -> Request {
let mut request = Request::new();
// システムプロンプトを設定
if let Some(ref system) = self.system_prompt {
request = request.system(system);
}
// メッセージを追加
for msg in &self.history {
// worker-types::Message から llm_client::Message への変換
request = request.message(crate::llm_client::Message {
role: match msg.role {
worker_types::Role::User => crate::llm_client::Role::User,
worker_types::Role::Assistant => crate::llm_client::Role::Assistant,
},
content: match &msg.content {
worker_types::MessageContent::Text(t) => {
crate::llm_client::MessageContent::Text(t.clone())
}
worker_types::MessageContent::ToolResult {
tool_use_id,
content,
} => crate::llm_client::MessageContent::ToolResult {
tool_use_id: tool_use_id.clone(),
content: content.clone(),
},
worker_types::MessageContent::Parts(parts) => {
crate::llm_client::MessageContent::Parts(
parts
.iter()
.map(|p| match p {
worker_types::ContentPart::Text { text } => {
crate::llm_client::ContentPart::Text { text: text.clone() }
}
worker_types::ContentPart::ToolUse { id, name, input } => {
crate::llm_client::ContentPart::ToolUse {
id: id.clone(),
name: name.clone(),
input: input.clone(),
}
}
worker_types::ContentPart::ToolResult {
tool_use_id,
content,
} => crate::llm_client::ContentPart::ToolResult {
tool_use_id: tool_use_id.clone(),
content: content.clone(),
},
})
.collect(),
)
}
},
});
}
// ツール定義を追加
for tool_def in tool_definitions {
request = request.tool(tool_def.clone());
}
request
}
/// Hooks: on_message_send
async fn run_on_message_send_hooks(&self) -> Result<ControlFlow, WorkerError> {
for hook in &self.hooks {
// Note: Locked状態でも履歴全体を参照として渡す(変更は不可)
// HookのAPIを変更し、immutable参照のみを渡すようにする必要があるかもしれない
// 現在は空のVecを渡して回避(要検討)
let mut temp_context = self.history.clone();
let result = hook.on_message_send(&mut temp_context).await?;
match result {
ControlFlow::Continue => continue,
ControlFlow::Skip => return Ok(ControlFlow::Skip),
ControlFlow::Abort(reason) => return Ok(ControlFlow::Abort(reason)),
}
}
Ok(ControlFlow::Continue)
}
/// Hooks: on_turn_end
async fn run_on_turn_end_hooks(&self) -> Result<TurnResult, WorkerError> {
for hook in &self.hooks {
let result = hook.on_turn_end(&self.history).await?;
match result {
TurnResult::Finish => continue,
TurnResult::ContinueWithMessages(msgs) => {
return Ok(TurnResult::ContinueWithMessages(msgs));
}
}
}
Ok(TurnResult::Finish)
}
/// ツールを並列実行
///
/// 全てのツールに対してbefore_tool_callフックを実行後、
/// 許可されたツールを並列に実行し、結果にafter_tool_callフックを適用する。
async fn execute_tools(
&self,
tool_calls: Vec<ToolCall>,
) -> Result<Vec<ToolResult>, WorkerError> {
use futures::future::join_all;
// Phase 1: before_tool_call フックを適用(スキップ/中断を判定)
let mut approved_calls = Vec::new();
for mut tool_call in tool_calls {
let mut skip = false;
for hook in &self.hooks {
let result = hook.before_tool_call(&mut tool_call).await?;
match result {
ControlFlow::Continue => {}
ControlFlow::Skip => {
skip = true;
break;
}
ControlFlow::Abort(reason) => {
return Err(WorkerError::Aborted(reason));
}
}
}
if !skip {
approved_calls.push(tool_call);
}
}
// Phase 2: 許可されたツールを並列実行
let futures: Vec<_> = approved_calls
.into_iter()
.map(|tool_call| {
let tools = &self.tools;
async move {
if let Some(tool) = tools.get(&tool_call.name) {
let input_json =
serde_json::to_string(&tool_call.input).unwrap_or_default();
match tool.execute(&input_json).await {
Ok(content) => ToolResult::success(&tool_call.id, content),
Err(e) => ToolResult::error(&tool_call.id, e.to_string()),
}
} else {
ToolResult::error(
&tool_call.id,
format!("Tool '{}' not found", tool_call.name),
)
}
}
})
.collect();
let mut results = join_all(futures).await;
// Phase 3: after_tool_call フックを適用
for tool_result in &mut results {
for hook in &self.hooks {
let result = hook.after_tool_call(tool_result).await?;
match result {
ControlFlow::Continue => {}
ControlFlow::Skip => break,
ControlFlow::Abort(reason) => {
return Err(WorkerError::Aborted(reason));
}
}
}
}
Ok(results)
}
/// 内部で使用するターン実行ロジック
async fn run_turn_loop(&mut self) -> Result<(), WorkerError> {
let tool_definitions = self.build_tool_definitions();
info!(
message_count = self.history.len(),
tool_count = tool_definitions.len(),
"Starting worker run"
);
loop {
// ターン開始を通知
let current_turn = self.turn_count;
debug!(turn = current_turn, "Turn start");
for notifier in &self.turn_notifiers {
notifier.on_turn_start(current_turn);
}
// Hook: on_message_send
let control = self.run_on_message_send_hooks().await?;
if let ControlFlow::Abort(reason) = control {
warn!(reason = %reason, "Aborted by hook");
// ターン終了を通知(異常終了)
for notifier in &self.turn_notifiers {
notifier.on_turn_end(current_turn);
}
return Err(WorkerError::Aborted(reason));
}
// リクエスト構築
let request = self.build_request(&tool_definitions);
debug!(
message_count = request.messages.len(),
tool_count = request.tools.len(),
has_system = request.system_prompt.is_some(),
"Sending request to LLM"
);
// ストリーム処理
debug!("Starting stream...");
let mut stream = self.client.stream(request).await?;
let mut event_count = 0;
while let Some(event_result) = stream.next().await {
match &event_result {
Ok(event) => {
trace!(event = ?event, "Received event");
event_count += 1;
}
Err(e) => {
warn!(error = %e, "Stream error");
}
}
let event = event_result?;
self.timeline.dispatch(&event);
}
debug!(event_count = event_count, "Stream completed");
// ターン終了を通知
for notifier in &self.turn_notifiers {
notifier.on_turn_end(current_turn);
}
self.turn_count += 1;
// 収集結果を取得
let text_blocks = self.text_block_collector.take_collected();
let tool_calls = self.tool_call_collector.take_collected();
// アシスタントメッセージを履歴に追加
let assistant_message = self.build_assistant_message(&text_blocks, &tool_calls);
if let Some(msg) = assistant_message {
self.history.push(msg);
}
if tool_calls.is_empty() {
// ツール呼び出しなし → ターン終了判定
let turn_result = self.run_on_turn_end_hooks().await?;
match turn_result {
TurnResult::Finish => {
return Ok(());
}
TurnResult::ContinueWithMessages(additional) => {
self.history.extend(additional);
continue;
}
}
}
// ツール実行
let tool_results = self.execute_tools(tool_calls).await?;
// ツール結果を履歴に追加
for result in tool_results {
self.history
.push(Message::tool_result(&result.tool_use_id, &result.content));
}
}
}
}
// =============================================================================
// Mutable状態専用の実装
// =============================================================================
impl<C: LlmClient> Worker<C, Mutable> {
/// 新しいWorkerを作成(Mutable状態)
pub fn new(client: C) -> Self {
let text_block_collector = TextBlockCollector::new();
let tool_call_collector = ToolCallCollector::new();
let mut timeline = Timeline::new();
// コレクターをTimelineに登録
timeline.on_text_block(text_block_collector.clone());
timeline.on_tool_use_block(tool_call_collector.clone());
Self {
client,
timeline,
text_block_collector,
tool_call_collector,
tools: HashMap::new(),
hooks: Vec::new(),
system_prompt: None,
history: Vec::new(),
locked_prefix_len: 0,
turn_count: 0,
turn_notifiers: Vec::new(),
_state: PhantomData,
}
}
/// システムプロンプトを設定(ビルダーパターン)
pub fn system_prompt(mut self, prompt: impl Into<String>) -> Self {
self.system_prompt = Some(prompt.into());
self
}
/// システムプロンプトを設定(可変参照版)
pub fn set_system_prompt(&mut self, prompt: impl Into<String>) {
self.system_prompt = Some(prompt.into());
}
/// 履歴への可変参照を取得
///
/// Mutable状態でのみ利用可能。履歴を自由に編集できる。
pub fn history_mut(&mut self) -> &mut Vec<Message> {
&mut self.history
}
/// 履歴を設定
pub fn set_history(&mut self, messages: Vec<Message>) {
self.history = messages;
}
/// 履歴にメッセージを追加(ビルダーパターン)
pub fn with_message(mut self, message: Message) -> Self {
self.history.push(message);
self
}
/// 履歴にメッセージを追加
pub fn push_message(&mut self, message: Message) {
self.history.push(message);
}
/// 複数のメッセージを履歴に追加(ビルダーパターン)
pub fn with_messages(mut self, messages: impl IntoIterator<Item = Message>) -> Self {
self.history.extend(messages);
self
}
/// 複数のメッセージを履歴に追加
pub fn extend_history(&mut self, messages: impl IntoIterator<Item = Message>) {
self.history.extend(messages);
}
/// 履歴をクリア
pub fn clear_history(&mut self) {
self.history.clear();
}
/// 設定を適用(将来の拡張用)
#[allow(dead_code)]
pub fn config(self, _config: WorkerConfig) -> Self {
self
}
/// ロックしてLocked状態へ遷移
///
/// この操作により、現在のシステムプロンプトと履歴が「確定済みプレフィックス」として
/// 固定される。以降は履歴への追記のみが可能となり、キャッシュヒットが保証される。
pub fn lock(self) -> Worker<C, Locked> {
let locked_prefix_len = self.history.len();
Worker {
client: self.client,
timeline: self.timeline,
text_block_collector: self.text_block_collector,
tool_call_collector: self.tool_call_collector,
tools: self.tools,
hooks: self.hooks,
system_prompt: self.system_prompt,
history: self.history,
locked_prefix_len,
turn_count: self.turn_count,
turn_notifiers: self.turn_notifiers,
_state: PhantomData,
}
}
/// ターンを実行(Mutable状態)
///
/// 新しいユーザーメッセージを履歴に追加し、LLMにリクエストを送信する。
/// ツール呼び出しがある場合は自動的にループする。
///
/// 注意: この関数は履歴を変更するため、キャッシュ保護が必要な場合は
/// `lock()` を呼んでからLocked状態で `run` を使用すること。
pub async fn run(&mut self, user_input: impl Into<String>) -> Result<&[Message], WorkerError> {
self.history.push(Message::user(user_input));
self.run_turn_loop().await?;
Ok(&self.history)
}
/// 複数メッセージでターンを実行(Mutable状態)
///
/// 指定されたメッセージを履歴に追加してから実行する。
pub async fn run_with_messages(
&mut self,
messages: Vec<Message>,
) -> Result<&[Message], WorkerError> {
self.history.extend(messages);
self.run_turn_loop().await?;
Ok(&self.history)
}
}
// =============================================================================
// Locked状態専用の実装
// =============================================================================
impl<C: LlmClient> Worker<C, Locked> {
/// ターンを実行(Locked状態)
///
/// 新しいユーザーメッセージを履歴の末尾に追加し、LLMにリクエストを送信する。
/// ロック時点より前の履歴(プレフィックス)は不変であるため、キャッシュヒットが保証される。
pub async fn run(&mut self, user_input: impl Into<String>) -> Result<&[Message], WorkerError> {
self.history.push(Message::user(user_input));
self.run_turn_loop().await?;
Ok(&self.history)
}
/// 複数メッセージでターンを実行(Locked状態)
pub async fn run_with_messages(
&mut self,
messages: Vec<Message>,
) -> Result<&[Message], WorkerError> {
self.history.extend(messages);
self.run_turn_loop().await?;
Ok(&self.history)
}
/// ロック時点のプレフィックス長を取得
pub fn locked_prefix_len(&self) -> usize {
self.locked_prefix_len
}
/// ロックを解除してMutable状態へ戻す
///
/// 注意: この操作を行うと、以降のリクエストでキャッシュがヒットしなくなる可能性がある。
/// 履歴を編集する必要がある場合にのみ使用すること。
pub fn unlock(self) -> Worker<C, Mutable> {
Worker {
client: self.client,
timeline: self.timeline,
text_block_collector: self.text_block_collector,
tool_call_collector: self.tool_call_collector,
tools: self.tools,
hooks: self.hooks,
system_prompt: self.system_prompt,
history: self.history,
locked_prefix_len: 0,
turn_count: self.turn_count,
turn_notifiers: self.turn_notifiers,
_state: PhantomData,
}
}
}
#[cfg(test)]
mod tests {
// 基本的なテストのみ。LlmClientを使ったテストは統合テストで行う。
}
@@ -1,4 +1,4 @@
//! Anthropic fixture-based integration tests //! Anthropic フィクスチャベースの統合テスト
mod common; mod common;
@@ -8,9 +8,9 @@ use std::sync::{Arc, Mutex};
use async_trait::async_trait; use async_trait::async_trait;
use futures::Stream; use futures::Stream;
use llm_worker::llm_client::event::{BlockType, DeltaContent, Event}; use worker::llm_client::{ClientError, LlmClient, Request};
use llm_worker::llm_client::{ClientError, LlmClient, Request}; use worker::timeline::{Handler, TextBlockEvent, TextBlockKind, Timeline};
use llm_worker::timeline::{Handler, TextBlockEvent, TextBlockKind, Timeline}; use worker_types::{BlockType, DeltaContent, Event};
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
@@ -267,8 +267,7 @@ pub fn assert_timeline_integration(subdir: &str) {
}); });
for event in &events { for event in &events {
let timeline_event: llm_worker::timeline::event::Event = event.clone().into(); timeline.dispatch(event);
timeline.dispatch(&timeline_event);
} }
let texts = collected.lock().unwrap(); let texts = collected.lock().unwrap();
@@ -1,4 +1,4 @@
//! Gemini fixture-based integration tests //! Gemini フィクスチャベースの統合テスト
mod common; mod common;
@@ -1,4 +1,4 @@
//! Ollama fixture-based integration tests //! Ollama フィクスチャベースの統合テスト
mod common; mod common;
@@ -1,4 +1,4 @@
//! OpenAI fixture-based integration tests //! OpenAI フィクスチャベースの統合テスト
mod common; mod common;

Some files were not shown because too many files have changed in this diff Show More