-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathask_user.ts
More file actions
171 lines (149 loc) · 4.82 KB
/
Copy pathask_user.ts
File metadata and controls
171 lines (149 loc) · 4.82 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { tool } from "@opencode-ai/plugin"
import * as fs from "fs"
import * as path from "path"
import * as os from "os"
// IPC Configuration
const IPC_DIR = path.join(os.homedir(), ".opencode", "ask_user")
const POLL_INTERVAL_MS = 500
const DEFAULT_TIMEOUT_SEC = 300 // 5 minutes
// Ensure IPC directory exists
function ensureIpcDir() {
if (!fs.existsSync(IPC_DIR)) {
fs.mkdirSync(IPC_DIR, { recursive: true })
}
}
// Generate a unique question ID
function generateQuestionId(): string {
const timestamp = Date.now()
const random = Math.random().toString(36).substring(2, 9)
return `q_${timestamp}_${random}`
}
// Question and Response interfaces
interface Question {
id: string
question: string
title?: string
sessionID: string
messageID: string
timestamp: number
}
interface Response {
id: string
response: string
responded: boolean
timestamp: number
}
export default tool({
description: `Ask the user a question and wait for their free-form text response.
Use this tool when you need explicit user input, confirmation, or clarification before proceeding with a task. The user will see the question in a separate terminal window running the ask-user-cli helper and can type their response.
IMPORTANT GUIDELINES:
- Use this for questions that genuinely require user input or decisions
- Be specific and clear in your questions
- Provide context in the title parameter when helpful
- The user can cancel by pressing Ctrl+C in the CLI helper
The tool will wait for the user's response (default timeout: 5 minutes).`,
args: {
question: tool.schema
.string()
.describe("The question to ask the user. Be specific and clear."),
title: tool.schema
.string()
.optional()
.describe("Optional title providing context for the question"),
timeout: tool.schema
.number()
.optional()
.describe("Timeout in seconds to wait for response (default: 300 = 5 minutes)"),
},
async execute(args, context) {
ensureIpcDir()
const questionId = generateQuestionId()
const timeoutSec = args.timeout ?? DEFAULT_TIMEOUT_SEC
const timeoutMs = timeoutSec * 1000
// Create the question file
const questionData: Question = {
id: questionId,
question: args.question,
title: args.title,
sessionID: context.sessionID,
messageID: context.messageID,
timestamp: Date.now(),
}
const questionFile = path.join(IPC_DIR, `question_${questionId}.json`)
const responseFile = path.join(IPC_DIR, `response_${questionId}.json`)
// Write the question file
fs.writeFileSync(questionFile, JSON.stringify(questionData, null, 2))
const startTime = Date.now()
try {
// Poll for response
while (true) {
// Check for abort signal
if (context.abort.aborted) {
// Clean up question file
if (fs.existsSync(questionFile)) {
fs.unlinkSync(questionFile)
}
return JSON.stringify({
responded: false,
response: "",
cancelled: true,
reason: "Agent aborted the request",
})
}
// Check for timeout
if (Date.now() - startTime > timeoutMs) {
// Clean up question file
if (fs.existsSync(questionFile)) {
fs.unlinkSync(questionFile)
}
return JSON.stringify({
responded: false,
response: "",
cancelled: true,
reason: `Timeout after ${timeoutSec} seconds waiting for user response`,
})
}
// Check for response file
if (fs.existsSync(responseFile)) {
try {
const responseData: Response = JSON.parse(
fs.readFileSync(responseFile, "utf-8")
)
// Clean up files
if (fs.existsSync(questionFile)) {
fs.unlinkSync(questionFile)
}
fs.unlinkSync(responseFile)
if (responseData.responded) {
return JSON.stringify({
responded: true,
response: responseData.response,
cancelled: false,
})
} else {
return JSON.stringify({
responded: false,
response: "",
cancelled: true,
reason: "User cancelled the request",
})
}
} catch (e) {
// Response file might be partially written, wait and retry
}
}
// Wait before next poll
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS))
}
} catch (error) {
// Clean up on error
if (fs.existsSync(questionFile)) {
fs.unlinkSync(questionFile)
}
if (fs.existsSync(responseFile)) {
fs.unlinkSync(responseFile)
}
throw error
}
},
})