Liking cljdoc? Tell your friends :D

User prompt transformed hook

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 and output

Input fieldTypeDescription
sessionIdstringSession reported by the hook input, not necessarily the callback owner's session
timestampdate/timeTime the hook was invoked
workingDirectorystringCurrent working directory
promptstringPrompt after userPromptSubmitted hooks
transformedPromptstringModel-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.

Examples

TypeScript
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),
            }),
        },
    },
});
Python
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"])
            }
        }
    }
)
Go
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
			},
		},
	},
})

.NET
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),
            }),
        }
    }
});
Java
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();
Rust
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 Sanderson
Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close