-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.ts
More file actions
198 lines (170 loc) · 6.02 KB
/
index.ts
File metadata and controls
198 lines (170 loc) · 6.02 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import type { Plugin } from "@opencode-ai/plugin"
import type { Event, Part, Permission } from "@opencode-ai/sdk"
import { buildPayload } from "./payload"
import { warpNotify } from "./notify"
import pkg from "../package.json" with { type: "json" }
const PLUGIN_VERSION = pkg.version
const NOTIFICATION_TITLE = "warp://cli-agent"
export function truncate(str: string, maxLen: number): string {
if (str.length <= maxLen) return str
return str.slice(0, maxLen - 3) + "..."
}
export function extractTextFromParts(parts: Part[]): string {
return parts
.filter((p): p is Part & { type: "text"; text: string } =>
p.type === "text" && "text" in p && Boolean(p.text),
)
.map((p) => p.text)
.join(" ")
}
function sendPermissionNotification(perm: Permission, cwd: string): void {
const sessionId = perm.sessionID
const toolName = perm.type || "unknown"
const metadata = perm.metadata || {}
let toolPreview = ""
if (typeof metadata.command === "string") {
toolPreview = metadata.command
} else if (typeof metadata.file_path === "string") {
toolPreview = metadata.file_path as string
} else if (typeof metadata.filePath === "string") {
toolPreview = metadata.filePath as string
} else {
const raw = JSON.stringify(metadata)
toolPreview = raw.slice(0, 80)
}
let summary = `Wants to run ${toolName}`
if (toolPreview) {
summary += `: ${truncate(toolPreview, 120)}`
}
const body = buildPayload("permission_request", sessionId, cwd, {
summary,
tool_name: toolName,
tool_input: metadata,
})
warpNotify(NOTIFICATION_TITLE, body)
}
export const WarpPlugin: Plugin = async ({ client, directory }) => {
if (!process.env.WARP_CLI_AGENT_PROTOCOL_VERSION) {
await client.app.log({
body: {
service: "opencode-warp",
level: "warn",
message:
"⚠️ Detected unsupported Warp version. Please update Warp to use this plugin.",
},
})
return {}
}
await client.app.log({
body: {
service: "opencode-warp",
level: "info",
message: "Warp plugin initialized",
},
})
return {
event: async ({ event }: { event: Event }) => {
const cwd = directory || ""
switch (event.type) {
case "session.created": {
const sessionId = event.properties.info.id
const body = buildPayload("session_start", sessionId, cwd, {
plugin_version: PLUGIN_VERSION,
})
warpNotify(NOTIFICATION_TITLE, body)
return
}
case "session.idle": {
const sessionId = event.properties.sessionID
// Fetch the conversation to extract last query and response
// (port of on-stop.sh transcript parsing)
let query = ""
let response = ""
if (sessionId) {
try {
const result = await client.session.messages({
path: { id: sessionId },
})
const messages = result.data
if (messages) {
const reversed = [...messages].reverse()
const lastUser = reversed.find(
(m) => m.info.role === "user",
)
if (lastUser) {
query = extractTextFromParts(lastUser.parts)
}
const lastAssistant = reversed.find(
(m) => m.info.role === "assistant",
)
if (lastAssistant) {
response = extractTextFromParts(lastAssistant.parts)
}
}
} catch {
// If we can't fetch messages, send the notification without query/response
}
}
const body = buildPayload("stop", sessionId, cwd, {
query: truncate(query, 200),
response: truncate(response, 200),
transcript_path: "",
})
warpNotify(NOTIFICATION_TITLE, body)
return
}
case "permission.updated": {
sendPermissionNotification(event.properties, cwd)
return
}
case "permission.replied": {
const { sessionID, response } = event.properties
if (response === "reject") return
const body = buildPayload("permission_replied", sessionID, cwd)
warpNotify(NOTIFICATION_TITLE, body)
return
}
default: {
// permission.asked is listed in the opencode docs but has no SDK type.
// Handle it with the same logic as permission.updated.
if ((event as any).type === "permission.asked") {
sendPermissionNotification((event as any).properties, cwd)
}
}
}
},
// Fires once per new user message — used to send the prompt_submit hook.
// (We avoid the generic message.updated event because OpenCode fires it
// multiple times per message, and a late duplicate can clobber the
// completion notification.)
"chat.message": async (input, output) => {
const cwd = directory || ""
const queryText = extractTextFromParts(output.parts)
if (!queryText) return
const body = buildPayload("prompt_submit", input.sessionID, cwd, {
query: truncate(queryText, 200),
})
warpNotify(NOTIFICATION_TITLE, body)
},
// Fires before a tool executes — used to detect the built-in
// "question" tool so Warp can notify the user that input is needed.
"tool.execute.before": async (input) => {
if (input.tool !== "question") return
const cwd = directory || ""
const body = buildPayload("question_asked", input.sessionID, cwd, {
tool_name: input.tool,
})
warpNotify(NOTIFICATION_TITLE, body)
},
// Tool completion — fires after every tool call
"tool.execute.after": async (input) => {
const toolName = input.tool
const sessionId = input.sessionID
const cwd = directory || ""
const body = buildPayload("tool_complete", sessionId, cwd, {
tool_name: toolName,
})
warpNotify(NOTIFICATION_TITLE, body)
},
}
}