docs: add hooks, user input, and auth options to SDK READMEs (#270) · ruyut/github__copilot-sdk@a124990 · GitHub
Skip to content

Commit a124990

Browse files
authored
docs: add hooks, user input, and auth options to SDK READMEs (github#270)
Update all SDK documentation with new features from PRs github#237 and github#269: - Add githubToken and useLoggedInUser client options for authentication - Add onUserInputRequest handler for enabling ask_user tool - Add hooks configuration for session lifecycle events - Add User Input Requests section with examples - Add Session Hooks section documenting all available hooks: - onPreToolUse, onPostToolUse, onUserPromptSubmitted - onSessionStart, onSessionEnd, onErrorOccurred
1 parent a76dffc commit a124990

4 files changed

Lines changed: 407 additions & 0 deletions

File tree

‎dotnet/README.md‎

Lines changed: 116 additions & 0 deletions

‎go/README.md‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ func main() {
9696
- `AutoStart` (\*bool): Auto-start server on first use (default: true). Use `Bool(false)` to disable.
9797
- `AutoRestart` (\*bool): Auto-restart on crash (default: true). Use `Bool(false)` to disable.
9898
- `Env` ([]string): Environment variables for CLI process (default: inherits from current process)
99+
- `GithubToken` (string): GitHub token for authentication. When provided, takes priority over other auth methods.
100+
- `UseLoggedInUser` (\*bool): Whether to use logged-in user for authentication (default: true, but false when `GithubToken` is provided). Cannot be used with `CLIUrl`.
99101

100102
**SessionConfig:**
101103

@@ -106,6 +108,8 @@ func main() {
106108
- `Provider` (\*ProviderConfig): Custom API provider configuration (BYOK). See [Custom Providers](#custom-providers) section.
107109
- `Streaming` (bool): Enable streaming delta events
108110
- `InfiniteSessions` (\*InfiniteSessionConfig): Automatic context compaction configuration
111+
- `OnUserInputRequest` (UserInputHandler): Handler for user input requests from the agent (enables ask_user tool). See [User Input Requests](#user-input-requests) section.
112+
- `Hooks` (\*SessionHooks): Hook handlers for session lifecycle events. See [Session Hooks](#session-hooks) section.
109113

110114
**ResumeSessionConfig:**
111115

@@ -397,6 +401,101 @@ session, err := client.CreateSession(&copilot.SessionConfig{
397401
> - For Azure OpenAI endpoints (`*.openai.azure.com`), you **must** use `Type: "azure"`, not `Type: "openai"`.
398402
> - The `BaseURL` should be just the host (e.g., `https://my-resource.openai.azure.com`). Do **not** include `/openai/v1` in the URL - the SDK handles path construction automatically.
399403
404+
## User Input Requests
405+
406+
Enable the agent to ask questions to the user using the `ask_user` tool by providing an `OnUserInputRequest` handler:
407+
408+
```go
409+
session, err := client.CreateSession(&copilot.SessionConfig{
410+
Model: "gpt-5",
411+
OnUserInputRequest: func(request copilot.UserInputRequest, invocation copilot.UserInputInvocation) (copilot.UserInputResponse, error) {
412+
// request.Question - The question to ask
413+
// request.Choices - Optional slice of choices for multiple choice
414+
// request.AllowFreeform - Whether freeform input is allowed (default: true)
415+
416+
fmt.Printf("Agent asks: %s\n", request.Question)
417+
if len(request.Choices) > 0 {
418+
fmt.Printf("Choices: %v\n", request.Choices)
419+
}
420+
421+
// Return the user's response
422+
return copilot.UserInputResponse{
423+
Answer: "User's answer here",
424+
WasFreeform: true, // Whether the answer was freeform (not from choices)
425+
}, nil
426+
},
427+
})
428+
```
429+
430+
## Session Hooks
431+
432+
Hook into session lifecycle events by providing handlers in the `Hooks` configuration:
433+
434+
```go
435+
session, err := client.CreateSession(&copilot.SessionConfig{
436+
Model: "gpt-5",
437+
Hooks: &copilot.SessionHooks{
438+
// Called before each tool execution
439+
OnPreToolUse: func(input copilot.PreToolUseHookInput, invocation copilot.HookInvocation) (*copilot.PreToolUseHookOutput, error) {
440+
fmt.Printf("About to run tool: %s\n", input.ToolName)
441+
// Return permission decision and optionally modify args
442+
return &copilot.PreToolUseHookOutput{
443+
PermissionDecision: "allow", // "allow", "deny", or "ask"
444+
ModifiedArgs: input.ToolArgs, // Optionally modify tool arguments
445+
AdditionalContext: "Extra context for the model",
446+
}, nil
447+
},
448+
449+
// Called after each tool execution
450+
OnPostToolUse: func(input copilot.PostToolUseHookInput, invocation copilot.HookInvocation) (*copilot.PostToolUseHookOutput, error) {
451+
fmt.Printf("Tool %s completed\n", input.ToolName)
452+
return &copilot.PostToolUseHookOutput{
453+
AdditionalContext: "Post-execution notes",
454+
}, nil
455+
},
456+
457+
// Called when user submits a prompt
458+
OnUserPromptSubmitted: func(input copilot.UserPromptSubmittedHookInput, invocation copilot.HookInvocation) (*copilot.UserPromptSubmittedHookOutput, error) {
459+
fmt.Printf("User prompt: %s\n", input.Prompt)
460+
return &copilot.UserPromptSubmittedHookOutput{
461+
ModifiedPrompt: input.Prompt, // Optionally modify the prompt
462+
}, nil
463+
},
464+
465+
// Called when session starts
466+
OnSessionStart: func(input copilot.SessionStartHookInput, invocation copilot.HookInvocation) (*copilot.SessionStartHookOutput, error) {
467+
fmt.Printf("Session started from: %s\n", input.Source) // "startup", "resume", "new"
468+
return &copilot.SessionStartHookOutput{
469+
AdditionalContext: "Session initialization context",
470+
}, nil
471+
},
472+
473+
// Called when session ends
474+
OnSessionEnd: func(input copilot.SessionEndHookInput, invocation copilot.HookInvocation) (*copilot.SessionEndHookOutput, error) {
475+
fmt.Printf("Session ended: %s\n", input.Reason)
476+
return nil, nil
477+
},
478+
479+
// Called when an error occurs
480+
OnErrorOccurred: func(input copilot.ErrorOccurredHookInput, invocation copilot.HookInvocation) (*copilot.ErrorOccurredHookOutput, error) {
481+
fmt.Printf("Error in %s: %s\n", input.ErrorContext, input.Error)
482+
return &copilot.ErrorOccurredHookOutput{
483+
ErrorHandling: "retry", // "retry", "skip", or "abort"
484+
}, nil
485+
},
486+
},
487+
})
488+
```
489+
490+
**Available hooks:**
491+
492+
- `OnPreToolUse` - Intercept tool calls before execution. Can allow/deny or modify arguments.
493+
- `OnPostToolUse` - Process tool results after execution. Can modify results or add context.
494+
- `OnUserPromptSubmitted` - Intercept user prompts. Can modify the prompt before processing.
495+
- `OnSessionStart` - Run logic when a session starts or resumes.
496+
- `OnSessionEnd` - Cleanup or logging when session ends.
497+
- `OnErrorOccurred` - Handle errors with retry/skip/abort strategies.
498+
400499
## Transport Modes
401500

402501
### stdio (Default)

‎nodejs/README.md‎

Lines changed: 99 additions & 0 deletions

0 commit comments

Comments
 (0)