-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathResponse.php
More file actions
110 lines (94 loc) · 2.38 KB
/
Response.php
File metadata and controls
110 lines (94 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace BeyondCode\ClaudeHooks\Hooks;
class Response
{
protected array $data = [];
protected int $exitCode = 0;
/**
* Continue processing (exit with code 0)
*/
public function continue(): never
{
$this->data['continue'] = true;
$this->send();
}
/**
* Stop processing with a reason (exit with code 1)
*/
public function stop(string $reason): never
{
$this->data['continue'] = false;
$this->data['stopReason'] = $reason;
$this->exitCode = 1;
$this->send();
}
/**
* Block the tool call
*/
public function block(string $reason): self
{
$this->data['decision'] = 'block';
$this->data['reason'] = $reason;
return $this;
}
/**
* Block the tool call and immediately send the response
* Useful for replacing tool output entirely
*
* @param string $reason The reason/content to provide instead of tool execution
*/
public function blockAndSend(string $reason): never
{
$this->data['decision'] = 'block';
$this->data['reason'] = $reason;
$this->send();
}
/**
* Approve the tool call (PreToolUse only)
*/
public function approve(string $reason = ''): self
{
$this->data['decision'] = 'approve';
if ($reason) {
$this->data['reason'] = $reason;
}
return $this;
}
/**
* Approve the tool call and immediately send the response
*
* @param string $reason Optional approval reason
*/
public function approveAndSend(string $reason = ''): never
{
$this->data['decision'] = 'approve';
if ($reason) {
$this->data['reason'] = $reason;
}
$this->send();
}
/**
* Suppress output from transcript mode
*/
public function suppressOutput(): self
{
$this->data['suppressOutput'] = true;
return $this;
}
/**
* Merge multiple fields into the response
*/
public function merge(array $fields): self
{
$this->data = array_merge($this->data, $fields);
return $this;
}
/**
* Send the response and exit
*/
protected function send(): never
{
echo json_encode($this->data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
exit($this->exitCode);
}
}