forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseIssueFlagBanner.ts
More file actions
133 lines (122 loc) · 3.74 KB
/
Copy pathuseIssueFlagBanner.ts
File metadata and controls
133 lines (122 loc) · 3.74 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { useMemo, useRef } from 'react'
import { BASH_TOOL_NAME } from '../tools/BashTool/toolName.js'
import type { Message } from '../types/message.js'
import { getUserMessageText } from '../utils/messages.js'
const EXTERNAL_COMMAND_PATTERNS = [
/\bcurl\b/,
/\bwget\b/,
/\bssh\b/,
/\bkubectl\b/,
/\bsrun\b/,
/\bdocker\b/,
/\bbq\b/,
/\bgsutil\b/,
/\bgcloud\b/,
/\baws\b/,
/\bgit\s+push\b/,
/\bgit\s+pull\b/,
/\bgit\s+fetch\b/,
/\bgh\s+(pr|issue)\b/,
/\bnc\b/,
/\bncat\b/,
/\btelnet\b/,
/\bftp\b/,
]
const FRICTION_PATTERNS = [
// "No," or "No!" at start — comma/exclamation implies correction tone
// (avoids "No problem", "No thanks", "No I think we should...")
/^no[,!]\s/i,
// Direct corrections about Claude's output
/\bthat'?s (wrong|incorrect|not (what|right|correct))\b/i,
/\bnot what I (asked|wanted|meant|said)\b/i,
// Referencing prior instructions Claude missed
/\bI (said|asked|wanted|told you|already said)\b/i,
// Questioning Claude's actions
/\bwhy did you\b/i,
/\byou should(n'?t| not)? have\b/i,
/\byou were supposed to\b/i,
// Explicit retry/revert of Claude's work
/\btry again\b/i,
/\b(undo|revert) (that|this|it|what you)\b/i,
]
export function isSessionContainerCompatible(messages: Message[]): boolean {
for (const msg of messages) {
if (msg.type !== 'assistant') {
continue
}
const content = msg.message.content
if (!Array.isArray(content)) {
continue
}
for (const block of content) {
if (block.type !== 'tool_use' || !('name' in block)) {
continue
}
const toolName = block.name as string
if (toolName.startsWith('mcp__')) {
return false
}
if (toolName === BASH_TOOL_NAME) {
const input = (block as { input?: Record<string, unknown> }).input
const command = (input?.command as string) || ''
if (EXTERNAL_COMMAND_PATTERNS.some(p => p.test(command))) {
return false
}
}
}
}
return true
}
export function hasFrictionSignal(messages: Message[]): boolean {
for (let i = messages.length - 1; i >= 0; i--) {
const msg = messages[i]!
if (msg.type !== 'user') {
continue
}
const text = getUserMessageText(msg)
if (!text) {
continue
}
return FRICTION_PATTERNS.some(p => p.test(text))
}
return false
}
const MIN_SUBMIT_COUNT = 3
const COOLDOWN_MS = 30 * 60 * 1000
export function useIssueFlagBanner(
messages: Message[],
submitCount: number,
): boolean {
if (process.env.USER_TYPE !== 'ant') {
return false
}
// biome-ignore lint/correctness/useHookAtTopLevel: process.env.USER_TYPE is a compile-time constant
const lastTriggeredAtRef = useRef(0)
// biome-ignore lint/correctness/useHookAtTopLevel: process.env.USER_TYPE is a compile-time constant
const activeForSubmitRef = useRef(-1)
// Memoize the O(messages) scans. This hook runs on every REPL render
// (including every keystroke), but messages is stable during typing.
// isSessionContainerCompatible walks all messages + regex-tests each
// bash command — by far the heaviest work here.
// biome-ignore lint/correctness/useHookAtTopLevel: process.env.USER_TYPE is a compile-time constant
const shouldTrigger = useMemo(
() => isSessionContainerCompatible(messages) && hasFrictionSignal(messages),
[messages],
)
// Keep showing the banner until the user submits another message
if (activeForSubmitRef.current === submitCount) {
return true
}
if (Date.now() - lastTriggeredAtRef.current < COOLDOWN_MS) {
return false
}
if (submitCount < MIN_SUBMIT_COUNT) {
return false
}
if (!shouldTrigger) {
return false
}
lastTriggeredAtRef.current = Date.now()
activeForSubmitRef.current = submitCount
return true
}