The userPromptTransformed hook runs after the runtime adds generated context to a submitted prompt, but before the resulting content is persisted to session history or sent to the model.
Use it when you need to inspect or replace the exact model-facing prompt. The prompt input contains the user prompt after any userPromptSubmitted hooks have run, while transformedPrompt also contains runtime-generated context such as <current_datetime>.
| Input field | Type | Description |
|---|---|---|
sessionId | string | Session reported by the hook input, not necessarily the callback owner's session |
timestamp | date/time | Time the hook was invoked |
workingDirectory | string | Current working directory |
prompt | string | Prompt after userPromptSubmitted hooks |
transformedPrompt | string | Model-facing prompt after runtime transformations |
Return no value to leave the transformed prompt unchanged. Return modifiedTransformedPrompt to replace the content that is stored in session history and sent to the model.
declare function redact(text: string): string;
const session = await client.createSession({
hooks: {
handlers: {
onUserPromptTransformed: async (input) => ({
modifiedTransformedPrompt: redact(input.transformedPrompt),
}),
},
},
});
const session = await client.createSession({
hooks: {
handlers: {
onUserPromptTransformed: async (input) => ({
modifiedTransformedPrompt: redact(input.transformedPrompt),
}),
},
},
});
from copilot import CopilotClient
def redact(text: str) -> str:
return text
async def main(client: CopilotClient):
session = await client.create_session(
hooks={
"handlers": {
"on_user_prompt_transformed": lambda input_data, invocation: {
"modifiedTransformedPrompt": redact(input_data["transformedPrompt"])
}
}
}
)
session = await client.create_session(
hooks={
"handlers": {
"on_user_prompt_transformed": lambda input_data, invocation: {
"modifiedTransformedPrompt": redact(input_data["transformedPrompt"])
}
}
}
)
package main
import (
"context"
copilot "github.com/microsoft/copilot-sdk/go/v2"
)
func redact(text string) string { return text }
func main() {
var client *copilot.Client
ctx := context.Background()
session, err := client.CreateSession(ctx, &copilot.CreateSessionOptions{
Hooks: copilot.SessionHookOptions{
Handlers: &copilot.SessionHooks{
OnUserPromptTransformed: func(input copilot.UserPromptTransformedHookInput, invocation copilot.HookInvocation) (*copilot.UserPromptTransformedHookOutput, error) {
return &copilot.UserPromptTransformedHookOutput{
ModifiedTransformedPrompt: copilot.String(redact(input.TransformedPrompt)),
}, nil
},
},
},
})
_, _ = session, err
}
session, err := client.CreateSession(ctx, &copilot.CreateSessionOptions{
Hooks: copilot.SessionHookOptions{
Handlers: &copilot.SessionHooks{
OnUserPromptTransformed: func(input copilot.UserPromptTransformedHookInput, invocation copilot.HookInvocation) (*copilot.UserPromptTransformedHookOutput, error) {
return &copilot.UserPromptTransformedHookOutput{
ModifiedTransformedPrompt: copilot.String(redact(input.TransformedPrompt)),
}, nil
},
},
},
})
using Microsoft.Copilot;
static string Redact(string text) => text;
await using var client = new CopilotClient(new CopilotClientOptions { Mode = CopilotClientMode.CodingAgent });
var session = await client.CreateSessionAsync(new CreateSessionOptions
{
Hooks = new SessionHookOptions
{
Handlers = new SessionHooks
{
OnUserPromptTransformed = (input, invocation) => Task.FromResult<UserPromptTransformedHookOutput?>(new()
{
ModifiedTransformedPrompt = Redact(input.TransformedPrompt),
}),
}
}
});
var session = await client.CreateSessionAsync(new CreateSessionOptions
{
Hooks = new SessionHookOptions
{
Handlers = new SessionHooks
{
OnUserPromptTransformed = (input, invocation) => Task.FromResult<UserPromptTransformedHookOutput?>(new()
{
ModifiedTransformedPrompt = Redact(input.TransformedPrompt),
}),
}
}
});
import com.microsoft.copilot.CopilotClient;
import com.microsoft.copilot.rpc.*;
import java.util.concurrent.CompletableFuture;
public class UserPromptTransformedExample {
static String redact(String text) {
return text;
}
public static void main(String[] args) throws Exception {
try (var client = new CopilotClient(new CopilotClientOptions().setMode(CopilotClientMode.CODING_AGENT))) {
var hooks = new SessionHooks()
.setOnUserPromptTransformed((input, invocation) -> CompletableFuture
.completedFuture(new UserPromptTransformedHookOutput(redact(input
.transformedPrompt()))));
var session = client
.createSession(new CreateSessionOptions()
.setHooks(new SessionHookOptions()
.setHandlers(hooks)))
.get();
}
}
}
var hooks = new SessionHooks()
.setOnUserPromptTransformed((input, invocation) -> CompletableFuture
.completedFuture(new UserPromptTransformedHookOutput(redact(input
.transformedPrompt()))));
var session = client
.createSession(new CreateSessionOptions()
.setHooks(new SessionHookOptions()
.setHandlers(hooks)))
.get();
struct MyHooks;
fn redact(prompt: &str) -> String {
prompt.to_string()
}
#[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(
CreateSessionOptions::default()
.with_hooks(|__sdk_group| __sdk_group.with_handlers(Arc::new(MyHooks))),
)
.await?;
#[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(
CreateSessionOptions::default()
.with_hooks(|__sdk_group| __sdk_group.with_handlers(Arc::new(MyHooks))),
)
.await?;
The replacement is persisted as the user message content, so resumed sessions replay the modified content unchanged.
Can you improve this documentation? These fine people already did:
copilot-cli-release-app[bot], copilot-sdk-mirror-writer[bot] & Steve SandersonEdit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |