モデルに対する正確なプロンプトを検査または置き換える必要がある場合に使用します。
prompt入力には、userPromptSubmittedフックが実行された後のユーザー プロンプトが含まれますが、transformedPromptには、<current_datetime>などのランタイムで生成されたコンテキストも含まれます。
入力と出力
| 入力フィールド | タイプ | Description |
|---|---|---|
sessionId | 文字列 | ランタイム セッション ID |
timestamp | 日付/時刻 | フックが呼び出された時刻 |
cwd / workingDirectory | 文字列 | 現在の作業ディレクトリ |
prompt | 文字列 | |
userフック後のプロンプト | ||
transformedPrompt | 文字列 | ランタイム変換後のモデル向けプロンプト |
変換されたプロンプトを変更せずに残す値を返しません。 セッション履歴に格納され、モデルに送信されるコンテンツを置き換える modifiedTransformedPrompt を返します。
Examples
TypeScript
const session = await client.createSession({
hooks: {
onUserPromptTransformed: async (input) => ({
modifiedTransformedPrompt: redact(input.transformedPrompt),
}),
},
});
Python
session = await client.create_session(
hooks={
"on_user_prompt_transformed": lambda input_data, invocation: {
"modifiedTransformedPrompt": redact(input_data["transformedPrompt"])
}
}
)
Go
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
Hooks: &copilot.SessionHooks{
OnUserPromptTransformed: func(input copilot.UserPromptTransformedHookInput, invocation copilot.HookInvocation) (*copilot.UserPromptTransformedHookOutput, error) {
return &copilot.UserPromptTransformedHookOutput{
ModifiedTransformedPrompt: copilot.String(redact(input.TransformedPrompt)),
}, nil
},
},
})
.NET
var session = await client.CreateSessionAsync(new SessionConfig
{
Hooks = new SessionHooks
{
OnUserPromptTransformed = (input, invocation) =>
Task.FromResult<UserPromptTransformedHookOutput?>(new()
{
ModifiedTransformedPrompt = Redact(input.TransformedPrompt),
}),
},
});
Java
var hooks = new SessionHooks().setOnUserPromptTransformed((input, invocation) ->
CompletableFuture.completedFuture(
new UserPromptTransformedHookOutput(redact(input.transformedPrompt()))));
var session = client.createSession(new SessionConfig().setHooks(hooks)).get();
Rust
#[async_trait]
impl SessionHooks for MyHooks {
async fn on_user_prompt_transformed(
&self,
input: UserPromptTransformedInput,
_ctx: HookContext,
) -> Option<UserPromptTransformedOutput> {
Some(UserPromptTransformedOutput {
modified_transformed_prompt: Some(redact(&input.transformed_prompt)),
})
}
}
let session = client
.create_session(SessionConfig::default().with_hooks(Arc::new(MyHooks)))
.await?;
置換はユーザー メッセージの内容として保持されるため、再開されたセッションでは変更されたコンテンツが変更されずに再生されます。