-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpi-coding-agent-core.el
More file actions
405 lines (352 loc) · 17.4 KB
/
pi-coding-agent-core.el
File metadata and controls
405 lines (352 loc) · 17.4 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
;;; pi-coding-agent-core.el --- Core functionality for pi-coding-agent -*- lexical-binding: t; -*-
;; Copyright (C) 2026 Daniel Nouri
;; Author: Daniel Nouri <daniel.nouri@gmail.com>
;; Maintainer: Daniel Nouri <daniel.nouri@gmail.com>
;; URL: https://github.com/dnouri/pi-coding-agent
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Core functionality for pi-coding-agent: JSON parsing, line buffering, RPC communication.
;; This module provides the low-level plumbing for communicating with the
;; pi coding agent via JSON-over-stdio.
;;; Code:
(require 'cl-lib)
(require 'json)
;;;; JSON Parsing
(defun pi-coding-agent--parse-json-line (line)
"Parse LINE as JSON, returning a plist.
Returns nil if LINE is not valid JSON."
(condition-case nil
(json-parse-string line :object-type 'plist)
(json-error nil)))
;;;; Line Accumulation
(defun pi-coding-agent--accumulate-lines (accumulated chunk)
;; Note: Empty strings are filtered here because they're not valid JSON.
;; This couples line splitting with JSON semantics, but keeps the API simple.
"Accumulate CHUNK into ACCUMULATED, extracting complete lines.
Returns a cons cell (COMPLETE-LINES . REMAINDER) where COMPLETE-LINES
is a list of complete newline-terminated lines (without the newlines)
and REMAINDER is any incomplete line fragment to save for next call."
(let* ((combined (concat accumulated chunk))
(parts (split-string combined "\n"))
(complete-lines (butlast parts))
(remainder (car (last parts))))
(cons (seq-filter (lambda (s) (not (string-empty-p s))) complete-lines)
remainder)))
;;;; JSON Encoding
(defun pi-coding-agent--encode-command (command)
"Encode COMMAND plist as a JSON line for sending to pi.
COMMAND must be a valid plist with string/number/list values.
Returns a JSON string terminated with a newline."
(concat (json-encode command) "\n"))
;;;; Request ID Management
(defvar pi-coding-agent--request-id-counter 0
"Counter for generating unique request IDs.")
(defun pi-coding-agent--next-request-id ()
"Generate the next unique request ID."
(format "req_%d" (cl-incf pi-coding-agent--request-id-counter)))
(defun pi-coding-agent--get-pending-requests (process)
"Get or create the pending requests hash table for PROCESS.
Each process has its own table stored as a process property."
(or (process-get process 'pi-coding-agent-pending-requests)
(let ((table (make-hash-table :test 'equal)))
(process-put process 'pi-coding-agent-pending-requests table)
table)))
(defun pi-coding-agent--get-pending-command-types (process)
"Get or create pending command type table for PROCESS.
Maps request IDs to command type strings."
(or (process-get process 'pi-coding-agent-pending-command-types)
(let ((table (make-hash-table :test 'equal)))
(process-put process 'pi-coding-agent-pending-command-types table)
table)))
(defun pi-coding-agent--rpc-async (process command callback)
"Send COMMAND to pi PROCESS asynchronously.
COMMAND is a plist that will be augmented with a unique ID.
CALLBACK is called with the response plist when received."
(let* ((id (pi-coding-agent--next-request-id))
(full-command (plist-put (copy-sequence command) :id id))
(pending (pi-coding-agent--get-pending-requests process))
(pending-types (pi-coding-agent--get-pending-command-types process)))
(puthash id callback pending)
(puthash id (plist-get command :type) pending-types)
(process-send-string process (pi-coding-agent--encode-command full-command))))
(defun pi-coding-agent--send-extension-ui-response (process response)
"Send extension UI RESPONSE to pi PROCESS.
RESPONSE must include the original :id from the request, as pi uses
this to match responses to pending promises."
(process-send-string process (pi-coding-agent--encode-command response)))
(defun pi-coding-agent--rpc-sync (process command &optional timeout)
"Send COMMAND to pi PROCESS synchronously, returning the response.
Blocks until response is received or TIMEOUT seconds elapse.
TIMEOUT defaults to `pi-coding-agent-rpc-timeout' (or 30 seconds).
Returns nil on timeout."
(let ((response nil)
(timeout (or timeout
(and (boundp 'pi-coding-agent-rpc-timeout) pi-coding-agent-rpc-timeout)
30))
(start-time (float-time)))
(pi-coding-agent--rpc-async process command (lambda (r) (setq response r)))
(while (and (null response)
(< (- (float-time) start-time) timeout)
(process-live-p process))
(accept-process-output process 0.1))
response))
;;;; Process Management
(defun pi-coding-agent--process-filter (proc output)
"Handle OUTPUT from pi PROC.
Accumulates output and dispatches complete JSON lines."
(let* ((inhibit-redisplay t)
(partial (or (process-get proc 'pi-coding-agent-partial-output) ""))
(result (pi-coding-agent--accumulate-lines partial output))
(lines (car result)))
(process-put proc 'pi-coding-agent-partial-output (cdr result))
(dolist (line lines)
(when-let* ((json (pi-coding-agent--parse-json-line line)))
(pi-coding-agent--dispatch-response proc json)))))
(defun pi-coding-agent--process-sentinel (proc event)
"Handle process state change EVENT for PROC."
(unless (process-live-p proc)
(pi-coding-agent--handle-process-exit proc event)))
(defun pi-coding-agent--dispatch-response (proc json)
"Dispatch JSON response from PROC to callback or event handler.
Response routing order: explicit ID, id-less `:command' match, then
id-less sole pending request. Non-response JSON is treated as an event."
(let ((type (plist-get json :type))
(id (plist-get json :id)))
(if (equal type "response")
(let* ((pending (pi-coding-agent--get-pending-requests proc))
(pending-types (pi-coding-agent--get-pending-command-types proc))
(dispatch-response
(lambda (request-id callback)
(remhash request-id pending)
(remhash request-id pending-types)
(funcall callback json))))
(cond
((and id (gethash id pending))
(funcall dispatch-response id (gethash id pending)))
((null id)
(let ((matched-id nil)
(matched-callback nil)
(matched-count 0)
(command (plist-get json :command)))
(when command
(maphash (lambda (request-id command-type)
(when (equal command-type command)
(setq matched-count (1+ matched-count))
(when (= matched-count 1)
(setq matched-id request-id
matched-callback (gethash request-id pending)))))
pending-types))
(cond
((and (= matched-count 1) matched-callback)
(funcall dispatch-response matched-id matched-callback))
((= (hash-table-count pending) 1)
(let (only-id only-callback)
(maphash (lambda (request-id callback)
(setq only-id request-id
only-callback callback))
pending)
(when only-callback
(funcall dispatch-response only-id only-callback)))))))))
;; Call only this process's handler, not all handlers
(pi-coding-agent--handle-event proc json))))
(defun pi-coding-agent--handle-event (proc event)
"Handle an EVENT from pi PROC.
Calls only the handler registered for this specific process."
;; Call only this process's handler
(when-let* ((handler (process-get proc 'pi-coding-agent-display-handler)))
(funcall handler event)))
(defun pi-coding-agent--handle-process-exit (proc event)
"Clean up when pi process PROC exits with EVENT.
Calls pending request callbacks for this process with an error response
containing EVENT, then clears this process's pending request tables."
(let ((pending (process-get proc 'pi-coding-agent-pending-requests))
(pending-types (process-get proc 'pi-coding-agent-pending-command-types))
(error-response (list :type "response"
:success :false
:error (format "Process exited: %s" (string-trim event)))))
(when pending
(maphash (lambda (_id callback)
(funcall callback error-response))
pending)
(clrhash pending))
(when pending-types
(clrhash pending-types))))
(defvar pi-coding-agent-executable) ; forward decl — core.el cannot require ui.el
(defvar pi-coding-agent-extra-args nil
"Extra arguments to pass to the pi command.
A list of strings that will be appended to the base command.
Example: (setq pi-coding-agent-extra-args \\='(\"-e\" \"/path/to/ext.ts\"))
This is useful for testing extensions or passing additional flags.")
(defun pi-coding-agent--start-process (directory)
"Start pi RPC process in DIRECTORY.
Returns the process object."
(let ((default-directory directory))
(make-process
:name "pi"
:command `(,@pi-coding-agent-executable "--mode" "rpc" ,@pi-coding-agent-extra-args)
:connection-type 'pipe
:filter #'pi-coding-agent--process-filter
:sentinel #'pi-coding-agent--process-sentinel)))
;;;; State Management
(defvar-local pi-coding-agent--status 'idle
"Current status of the pi session (buffer-local in chat buffer).
One of: `idle', `streaming', `compacting'.
This is the single source of truth for session activity state.
Status transitions are driven by events from pi:
- `idle' -> `streaming' on agent_start
- `streaming' -> `idle' on agent_end
- `idle' -> `compacting' on auto_compaction_start
- `compacting' -> `idle' on auto_compaction_end")
(defvar-local pi-coding-agent--state nil
"Current state of the pi session (buffer-local in chat buffer).
A plist with keys like :model, :thinking-level, :messages, etc.")
(defvar-local pi-coding-agent--state-timestamp nil
"Time when state was last updated (buffer-local in chat buffer).")
(defconst pi-coding-agent--state-verify-interval 30
"Seconds between state verification checks.")
(defun pi-coding-agent--state-needs-verification-p ()
"Return t if state should be verified with get_state.
Verification is needed when:
- State and timestamp exist
- Session is idle (not streaming or compacting)
- Timestamp is older than `pi-coding-agent--state-verify-interval' seconds."
(and pi-coding-agent--state
pi-coding-agent--state-timestamp
(eq pi-coding-agent--status 'idle)
(> (- (float-time) pi-coding-agent--state-timestamp)
pi-coding-agent--state-verify-interval)))
(defun pi-coding-agent--json-false-p (value)
"Return t if VALUE represents JSON false."
(eq value :false))
(defun pi-coding-agent--json-null-p (value)
"Return t if VALUE represents JSON null.
`json-parse-string' decodes JSON null as the keyword :null."
(eq value :null))
(defun pi-coding-agent--normalize-boolean (value)
"Convert JSON boolean VALUE to Elisp boolean.
JSON true (t) stays t, JSON false (:false) becomes nil."
(if (pi-coding-agent--json-false-p value) nil value))
(defun pi-coding-agent--normalize-string-or-null (value)
"Return VALUE if it's a string, nil otherwise.
Use when reading JSON fields that may be null or string.
JSON null (:null) and non-strings become nil."
(and (stringp value) value))
(defun pi-coding-agent--update-state-from-event (event)
"Update status and state based on EVENT.
Handles agent lifecycle, message events, and error/retry events.
Sets status to `streaming' on agent_start, `idle' on agent_end."
(let ((type (plist-get event :type)))
(pcase type
("agent_start"
(setq pi-coding-agent--status 'streaming)
(plist-put pi-coding-agent--state :is-retrying nil)
(plist-put pi-coding-agent--state :last-error nil)
(setq pi-coding-agent--state-timestamp (float-time)))
("agent_end"
(setq pi-coding-agent--status 'idle)
(plist-put pi-coding-agent--state :is-retrying nil)
(plist-put pi-coding-agent--state :messages (plist-get event :messages))
(setq pi-coding-agent--state-timestamp (float-time)))
("message_start"
(plist-put pi-coding-agent--state :current-message (plist-get event :message)))
("message_end"
(plist-put pi-coding-agent--state :current-message nil))
("tool_execution_start"
(pi-coding-agent--handle-tool-start event))
("tool_execution_update"
(pi-coding-agent--handle-tool-update event))
("tool_execution_end"
(pi-coding-agent--handle-tool-end event))
("auto_retry_start"
(plist-put pi-coding-agent--state :is-retrying t)
(plist-put pi-coding-agent--state :retry-attempt (plist-get event :attempt))
(plist-put pi-coding-agent--state :last-error (plist-get event :errorMessage)))
("auto_retry_end"
(plist-put pi-coding-agent--state :is-retrying nil)
(unless (eq (plist-get event :success) t)
(plist-put pi-coding-agent--state :last-error (plist-get event :finalError))))
("extension_error"
(plist-put pi-coding-agent--state :last-error (plist-get event :error))))))
(defun pi-coding-agent--ensure-active-tools ()
"Ensure :active-tools hash table exists in state."
(unless (plist-get pi-coding-agent--state :active-tools)
(setq pi-coding-agent--state (plist-put pi-coding-agent--state :active-tools
(make-hash-table :test 'equal))))
(plist-get pi-coding-agent--state :active-tools))
(defun pi-coding-agent--handle-tool-start (event)
"Handle tool_execution_start EVENT."
(let ((tools (pi-coding-agent--ensure-active-tools))
(id (plist-get event :toolCallId))
(name (plist-get event :toolName))
(args (plist-get event :args)))
(puthash id (list :name name :args args) tools)))
(defun pi-coding-agent--handle-tool-update (event)
"Handle tool_execution_update EVENT."
(let* ((tools (plist-get pi-coding-agent--state :active-tools))
(id (plist-get event :toolCallId))
(tool (and tools (gethash id tools))))
(when tool
(plist-put tool :partial-result (plist-get event :partialResult)))))
(defun pi-coding-agent--handle-tool-end (event)
"Handle tool_execution_end EVENT."
(let* ((tools (plist-get pi-coding-agent--state :active-tools))
(id (plist-get event :toolCallId)))
(when tools
(remhash id tools))))
(defun pi-coding-agent--update-state-from-response (response)
"Update state from a command RESPONSE.
Only processes successful responses for state-modifying commands."
(when (eq (plist-get response :success) t)
(let ((command (plist-get response :command))
(data (plist-get response :data)))
(pcase command
("set_model"
(plist-put pi-coding-agent--state :model data)
(setq pi-coding-agent--state-timestamp (float-time)))
("cycle_model"
(when data
(plist-put pi-coding-agent--state :model (plist-get data :model))
(plist-put pi-coding-agent--state :thinking-level (plist-get data :thinkingLevel))
(setq pi-coding-agent--state-timestamp (float-time))))
("cycle_thinking_level"
(when data
(plist-put pi-coding-agent--state :thinking-level (plist-get data :level))
(setq pi-coding-agent--state-timestamp (float-time))))
("set_thinking_level"
(setq pi-coding-agent--state-timestamp (float-time)))
("get_state"
(let ((new-state (pi-coding-agent--extract-state-from-response response)))
(setq pi-coding-agent--status (plist-get new-state :status)
pi-coding-agent--state new-state
pi-coding-agent--state-timestamp (float-time))))))))
(defun pi-coding-agent--extract-state-from-response (response)
"Extract state plist from a get_state RESPONSE.
Converts camelCase keys to kebab-case and normalizes booleans.
Returns plist with :status key for setting `pi-coding-agent--status'."
(when-let* ((data (plist-get response :data)))
(let ((is-streaming (pi-coding-agent--normalize-boolean (plist-get data :isStreaming)))
(is-compacting (pi-coding-agent--normalize-boolean (plist-get data :isCompacting))))
(list :status (cond (is-streaming 'streaming)
(is-compacting 'compacting)
(t 'idle))
:model (plist-get data :model)
:thinking-level (plist-get data :thinkingLevel)
:session-id (plist-get data :sessionId)
:session-file (plist-get data :sessionFile)
:message-count (plist-get data :messageCount)
:pending-message-count (plist-get data :pendingMessageCount)))))
(provide 'pi-coding-agent-core)
;;; pi-coding-agent-core.el ends here