試験段階。 ツールの置換では、実験用
session.tools.setRPC をラップし、それをサポートするランタイムが必要です。
このクライアントアプリのツールを置き換える
このクライアントが提供する必要があるツールの完全なセットを渡してください。 それは、セッションの作成時または再開時、あるいは以前の置き換え時にクライアントが指定したツールを置き換えます。 組み込み、MCP、プラグイン ツール、および同じセッションに接続している他のクライアントが提供するツールは影響を受けません。 このクライアントのすべてのツールを削除するには、空のリストを渡します。
ツールはセッションを作成する場合とまったく同じように定義され、ハンドラーのないツールはそこで行うのと同じように動作します。
import { defineTool } from "@github/copilot-sdk";
import { z } from "zod";
const searchIssues = defineTool("search_issues", {
description: "Search the issues shown on the current page",
parameters: z.object({ query: z.string().describe("Search text") }),
handler: async ({ query }) => findIssues(query),
});
// Supply only the tools the current page provides.
await session.setTools([searchIssues]);
// Remove all of this client's tools.
await session.setTools([]);
from pydantic import BaseModel, Field
from copilot.tools import define_tool
class SearchIssuesParams(BaseModel):
query: str = Field(description="Search text")
@define_tool(description="Search the issues shown on the current page")
async def search_issues(params: SearchIssuesParams) -> str:
return await find_issues(params.query)
# Supply only the tools the current page provides.
await session.set_tools([search_issues])
# Remove all of this client's tools.
await session.set_tools([])
type SearchIssuesParams struct {
Query string `json:"query" jsonschema:"Search text"`
}
searchIssues := copilot.DefineTool(
"search_issues",
"Search the issues shown on the current page",
func(params SearchIssuesParams, inv copilot.ToolInvocation) (string, error) {
return findIssues(params.Query)
},
)
// Supply only the tools the current page provides.
if err := session.SetTools(ctx, []copilot.Tool{searchIssues}); err != nil {
log.Fatal(err)
}
// Remove all of this client's tools.
if err := session.SetTools(ctx, nil); err != nil {
log.Fatal(err)
}
var searchIssues = CopilotTool.DefineTool(
([Description("Search text")] string query) => FindIssues(query),
factoryOptions: new AIFunctionFactoryOptions
{
Name = "search_issues",
Description = "Search the issues shown on the current page",
});
// Supply only the tools the current page provides.
await session.SetToolsAsync([searchIssues]);
// Remove all of this client's tools.
await session.SetToolsAsync([]);
SetToolsAsync では、#pragma warning disable GHCP001 によって試験的な診断が発生します。これは、<NoWarn>GHCP001</NoWarn> またはプロジェクト レベルの GHCP001 で抑制します。
var searchIssues = ToolDefinition.create(
"search_issues",
"Search the issues shown on the current page",
Map.of(
"type", "object",
"properties", Map.of("query", Map.of("type", "string", "description", "Search text")),
"required", List.of("query")),
invocation -> CompletableFuture.completedFuture(
findIssues((String) invocation.getArguments().get("query"))));
// Supply only the tools the current page provides.
session.setTools(List.of(searchIssues)).get();
// Remove all of this client's tools.
session.setTools(List.of()).get();
setTools は @CopilotExperimentalであるため、使用するクラスまたはメソッドは @AllowCopilotExperimentalを使用してオプトインする必要があります。
実験用 API の使用を参照してください。
#[derive(Deserialize, JsonSchema)]
struct SearchIssuesParams {
/// Search text
query: String,
}
let search_issues = define_tool(
"search_issues",
"Search the issues shown on the current page",
|_inv, params: SearchIssuesParams| async move {
Ok(ToolResult::Text(find_issues(¶ms.query).await))
},
);
// Supply only the tools the current page provides.
session.set_tools(vec![search_issues]).await?;
// Remove all of this client's tools.
session.set_tools(Vec::new()).await?;
新しいツールが有効になったとき
- モデル リクエスト。 ランタイムは、エージェントの次のモデル リクエストで利用可能な新しいツールを提供します。 その要求は、既に進行中のやり取りの一部にすることができます。
- ハンドラー。 ランタイムが置換を受け入れるとすぐに、セッションがディスパッチするすべてのツール呼び出しは新しいハンドラーを使用します。 既に実行されている呼び出し処理は、それらを開始したハンドラーで終了します。
- 拒否。 ランタイムが置換を拒否した場合、呼び出しは失敗し、以前のツールとハンドラーはそのまま残ります。 ランタイムは、無効な文字を含むツール名、および接続されている別のクライアントが既に提供しているツール名を拒否します。
- 並べ替え。 同じセッションでの同時置換は、行われた順序で一度に 1 つずつ適用されます。
- 取り消し。 呼び出しを取り消せる SDK では、以前の置き換え要求がまだ実行中の間に呼び出しを取り消しても何も送信されません。 要求が送信されると、キャンセルは待機のみを停止します。ランタイムが置換を受け入れた場合、新しいハンドラーは引き続き適用されます。
ターン中のツールの交換
既に実行中のモデル要求は以前のツールで行われたため、エージェントはたった今削除したツールを引き続き呼び出すことができます。 別の接続されたクライアントが同じ名���のツールを提供する可能性があるため、セッションはその呼び出しに応答しないため、ターンが中止されるまで呼び出しは保留状態を維持できます。
実行中のターンで削除しようとしているツールが引き続き呼び出される可能性がある場合は、セッションがアイドル状態の間 (たとえば、session.idle イベントの後) にツールを置き換えるか、最初にターンを中止します。 ツールのハンドラーを同じ名前で置き換えることは、ターン中に安全です。ランタイムが置換を受け入れた後にディスパッチされた呼び出しは、新しいハンドラーを使用します。
SDK リファレンス
| SDK | メソッド |
|---|---|
| TypeScript | session.set |
| Python | await session.set_tools(tools) |
| Go | session.Set |
| .NET | await session.Set |
| Java | |
Completable を返す session.set | |
| Rust | session.set_tools(tools).await |