-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPicoCodeStatusBarWidget.kt
More file actions
222 lines (186 loc) · 7.97 KB
/
PicoCodeStatusBarWidget.kt
File metadata and controls
222 lines (186 loc) · 7.97 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.picocode
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.StatusBar
import com.intellij.openapi.wm.StatusBarWidget
import com.intellij.openapi.wm.StatusBarWidgetFactory
import com.intellij.util.Consumer
import com.google.gson.Gson
import com.google.gson.JsonObject
import java.awt.event.MouseEvent
import java.net.HttpURLConnection
import java.net.URL
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
/**
* Status bar widget that displays PicoCode indexing status
*/
class PicoCodeStatusBarWidget(private val project: Project) : StatusBarWidget,
StatusBarWidget.TextPresentation {
companion object {
const val ID = "PicoCodeStatusWidget"
private const val POLLING_INTERVAL_SECONDS = 5L
}
private val gson = Gson()
private val executor = Executors.newSingleThreadScheduledExecutor()
private var currentStatus: String = "Unknown"
private var statusBar: StatusBar? = null
private var projectId: String? = null
private var indexingStats: IndexingStats? = null
data class IndexingStats(
val fileCount: Int = 0,
val embeddingCount: Int = 0,
val isIndexed: Boolean = false
)
init {
// Start polling for status updates
executor.scheduleAtFixedRate(
{ updateStatus() },
0,
POLLING_INTERVAL_SECONDS,
TimeUnit.SECONDS
)
}
override fun ID(): String = ID
override fun getPresentation(): StatusBarWidget.WidgetPresentation = this
override fun install(statusBar: StatusBar) {
this.statusBar = statusBar
}
override fun dispose() {
executor.shutdown()
try {
executor.awaitTermination(5, TimeUnit.SECONDS)
} catch (e: InterruptedException) {
executor.shutdownNow()
}
}
override fun getText(): String {
return when {
currentStatus == "indexing" -> "⚡ PicoCode: Indexing..."
currentStatus == "ready" && indexingStats?.isIndexed == true ->
"✓ PicoCode: ${indexingStats?.fileCount ?: 0} files"
currentStatus == "error" -> "✗ PicoCode: Error"
currentStatus == "created" -> "○ PicoCode: Not indexed"
else -> "PicoCode"
}
}
override fun getAlignment(): Float = 0.5f
override fun getTooltipText(): String? {
return when {
currentStatus == "indexing" -> "PicoCode is indexing your project..."
currentStatus == "ready" && indexingStats != null ->
"PicoCode: ${indexingStats?.fileCount} files, ${indexingStats?.embeddingCount} embeddings indexed"
currentStatus == "error" -> "PicoCode indexing error occurred"
currentStatus == "created" -> "PicoCode: Project created but not indexed yet"
else -> "PicoCode status unknown - check if server is running"
}
}
override fun getClickConsumer(): Consumer<MouseEvent>? {
return Consumer {
// Open the PicoCode chat dialog on click
ApplicationManager.getApplication().invokeLater {
val chatContent = PicoCodeToolWindowContent(project)
val dialog = object : com.intellij.openapi.ui.DialogWrapper(project) {
init {
init()
title = "PicoCode RAG Assistant"
}
override fun createCenterPanel(): javax.swing.JComponent {
return chatContent.getContent()
}
override fun createActions(): Array<com.intellij.openapi.ui.DialogWrapper.DialogWrapperAction> {
return emptyArray() // No default OK/Cancel buttons
}
}
dialog.show()
}
}
}
private fun updateStatus() {
ApplicationManager.getApplication().executeOnPooledThread {
try {
val projectPath = project.basePath ?: return@executeOnPooledThread
// Get or create project to get project ID
if (projectId == null) {
projectId = getOrCreateProject(projectPath)
}
projectId?.let { id ->
// Fetch project status
val status = fetchProjectStatus(id)
currentStatus = status.first
indexingStats = status.second
// Update status bar on EDT
ApplicationManager.getApplication().invokeLater {
statusBar?.updateWidget(ID)
}
}
} catch (e: Exception) {
// Silently fail - don't spam logs if server is not running
currentStatus = "Unavailable"
}
}
}
private fun getOrCreateProject(projectPath: String): String? {
return try {
val settings = PicoCodeSettings.getInstance(project)
val host = settings.state.serverHost
val port = settings.state.serverPort
val url = URL("http://$host:$port/api/projects")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/json")
connection.doOutput = true
val body = gson.toJson(mapOf(
"path" to projectPath,
"name" to project.name
))
connection.outputStream.use { it.write(body.toByteArray()) }
val response = connection.inputStream.bufferedReader().readText()
val json = gson.fromJson(response, JsonObject::class.java)
json.get("id")?.asString
} catch (e: Exception) {
null
}
}
private fun fetchProjectStatus(projectId: String): Pair<String, IndexingStats?> {
return try {
val settings = PicoCodeSettings.getInstance(project)
val host = settings.state.serverHost
val port = settings.state.serverPort
val url = URL("http://$host:$port/api/projects/$projectId")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
val response = connection.inputStream.bufferedReader().readText()
val json = gson.fromJson(response, JsonObject::class.java)
val status = json.get("status")?.asString ?: "unknown"
val statsJson = json.getAsJsonObject("indexing_stats")
val stats = if (statsJson != null) {
IndexingStats(
fileCount = statsJson.get("file_count")?.asInt ?: 0,
embeddingCount = statsJson.get("embedding_count")?.asInt ?: 0,
isIndexed = statsJson.get("is_indexed")?.asBoolean ?: false
)
} else {
null
}
Pair(status, stats)
} catch (e: Exception) {
Pair("unavailable", null)
}
}
}
/**
* Factory for creating the status bar widget
*/
class PicoCodeStatusBarWidgetFactory : StatusBarWidgetFactory {
override fun getId(): String = PicoCodeStatusBarWidget.ID
override fun getDisplayName(): String = "PicoCode Status"
override fun isAvailable(project: Project): Boolean = true
override fun createWidget(project: Project): StatusBarWidget {
return PicoCodeStatusBarWidget(project)
}
override fun disposeWidget(widget: StatusBarWidget) {
// Disposal is handled by the widget itself
}
override fun canBeEnabledOn(statusBar: StatusBar): Boolean = true
}