Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 194 additions & 0 deletions app/liii/pi/app/interactive/app-diff.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
;; Copyright (C) 2026 The Goldfish Scheme Authors
;; Licensed under the MIT License

(define-library (liii pi app interactive app-diff)
(export %pi-app-handle-diff-event! %pi-app-apply-diff! %pi-app-settle-diff!
%pi-app-reset-diff!
) ;export
(import (scheme base)
(liii string)
(liii hash-table)
(liii tui)
(liii pi app interactive state)
(liii pi app interactive renderers)
(liii pi app interactive tool-diff)
(liii pi app interactive views tool-card)
(liii pi app interactive status-card)
) ;import
(begin
(define-record-type <app-diff-run>
(%make-app-diff-run id early settled? summary)
%app-diff-run?
(id %app-diff-run-id)
(early %app-diff-run-early)
(settled? %app-diff-run-settled? %app-diff-run-settled-set!)
(summary %app-diff-run-summary %app-diff-run-summary-set!)
) ;define-record-type

(define (%pi-app-reset-diff! app)
;; Dispatch retires the old live index even if the optional run marker
;; is dropped by a saturated event channel.
(%pi-app-tool-cards-set! app '())
(%pi-app-diff-run-set! app #f)
) ;define

(define (%pi-app-apply-diff! app id card)
(let ((run (%pi-app-diff-run app)))
(when run
(let* ((early (%app-diff-run-early run)) (result (hash-table-ref early id)))
(when (pair? result)
(let ((applied (%tool-card-apply-diff! card (car result) (cadr result) (caddr result))
) ;applied
) ;
(when applied
(hash-table-set! early id 'consumed)
) ;when
(when (eq? applied 'changed)
(scroll-view-invalidate! (pi-app-scroll-view app))
) ;when
) ;let
) ;when
) ;let*
) ;when
) ;let
) ;define

(define (%pi-app-show-diff-summary! app run)
(let ((entries (%app-diff-run-summary run)))
(when (and (%app-diff-run-settled? run) (list? entries))
(%app-diff-run-summary-set! run 'shown)
(unless (null? entries)
(let ((box (make-container)))
(container-add! box (make-status-card "Turn changes"))
(for-each
(lambda (entry)
(let ((path (%clean-tool-display-text (car entry))))
(container-add! box
(make-status-card (string-append (string-replace path "\n" " ")
" "
(%tool-diff-counts (cadr entry) (caddr entry))
) ;string-append
) ;make-status-card
) ;container-add!
) ;let
) ;lambda
entries
) ;for-each
(container-add! (%pi-app-chat app) box)
(scroll-view-invalidate! (pi-app-scroll-view app))
) ;let
) ;unless
) ;when
) ;let
) ;define

(define (%pi-app-settle-diff! app)
(let ((run (%pi-app-diff-run app)))
(when run
(%app-diff-run-settled-set! run #t)
(%pi-app-show-diff-summary! app run)
) ;when
) ;let
) ;define

(define (%diff-startable? event)
(and (= (length event) 2) (exact-integer? (cadr event)) (> (cadr event) 0))
) ;define

(define (%diff-line-count? n)
(and (exact-integer? n) (<= 0 n %diff-lines-count-limit))
) ;define

(define (%diff-summary-entry? entry)
(and (list? entry)
(= (length entry) 3)
(string? (car entry))
(%diff-line-count? (cadr entry))
(%diff-line-count? (caddr entry))
(> (+ (cadr entry) (caddr entry)) 0)
) ;and
) ;define

(define (%diff-summary-entries? entries)
(and (list? entries)
(<= (length entries) 16)
(let loop
((rest entries))
(or (null? rest) (and (%diff-summary-entry? (car rest)) (loop (cdr rest))))
) ;let
) ;and
) ;define

(define (%diff-result? event)
(and (= (length event) 8)
(string? (caddr event))
(let ((text (list-ref event 4))
(added (list-ref event 5))
(removed (list-ref event 6))
) ;
(or (and (not text) (not added) (not removed))
(and (or (not text) (string? text))
(%diff-line-count? added)
(%diff-line-count? removed)
) ;and
) ;or
) ;let
) ;and
) ;define

(define (%pi-app-handle-diff-event! app event)
;; Only internal events reach this boundary; ignore malformed payloads
;; rather than letting optional display work interrupt the event loop.
(when (and (list? event) (pair? event) (pair? (cdr event)))
(%pi-app-dispatch-diff! app event)
) ;when
) ;define

(define (%pi-app-dispatch-diff! app event)
(let ((id (cadr event)) (run (%pi-app-diff-run app)))
(cond
((and (eq? (car event) 'run_started) (%diff-startable? event))
;; Agent events are ordered on their channel. A reused call ID
;; in a live run must not resolve to an earlier/history card.
(%pi-app-tool-cards-set! app '())
(%pi-app-diff-run-set! app (%make-app-diff-run id (make-hash-table) #f #f))
) ;
((and run (equal? id (%app-diff-run-id run)))
(case (car event)
((diff_result)
(when (%diff-result? event)
(%pi-app-handle-diff-result! app event run)
) ;when
) ;
((turn_diff_summary)
(when (and (not (%app-diff-run-summary run))
(= (length event) 3)
(%diff-summary-entries? (caddr event))
) ;and
(%app-diff-run-summary-set! run (caddr event))
(%pi-app-show-diff-summary! app run)
) ;when
) ;
) ;case
) ;
) ;cond
) ;let
) ;define
(define (%pi-app-handle-diff-result! app event run)
(let* ((call-id (caddr event))
(early (%app-diff-run-early run))
(card (assoc call-id (%pi-app-tool-cards app)))
) ;
(unless (hash-table-ref early call-id)
(hash-table-set! early
call-id
(list (list-ref event 4) (list-ref event 5) (list-ref event 6))
) ;hash-table-set!
(when card
(%pi-app-apply-diff! app call-id (cdr card))
) ;when
) ;unless
) ;let*
) ;define
) ;begin
) ;define-library
25 changes: 15 additions & 10 deletions app/liii/pi/app/interactive/app-events.scm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
(liii pi app interactive views)
(liii pi app interactive working)
(liii pi app interactive app-bash)
(liii pi app interactive app-diff)
(liii pi app interactive bash)
(liii pi app interactive app-name)
(liii pi app interactive app-queue)
Expand Down Expand Up @@ -150,7 +151,8 @@
(if (not msg)
rendered
(begin
;; 新任务开始:关闭上一轮引用,已显示的 thinking 与正文内容保留
;; 新任务不接受上一轮迟到的展示事件;历史卡片仍留在聊天区。
(%pi-app-reset-diff! app)
(%pi-app-close-thinking-card! app)
(%pi-app-close-assistant-card! app)
;; 1. 出队并正式上屏为深蓝用户消息卡片
Expand Down Expand Up @@ -256,15 +258,14 @@
(duration (and (> (length ev) 6) (list-ref ev 6)))
(card (%pi-app-find-tool-card app id))
) ;
(if card
(tool-card-set-complete! card raw-res is-err? duration)
;; 兼容未收到 start 的恢复/测试路径
(let ((new-card (make-tool-card name args 'pending)))
(tool-card-set-expanded! new-card (pi-app-tools-expanded? app))
(tool-card-set-complete! new-card raw-res is-err? duration)
(container-add! (%pi-app-chat app) new-card)
) ;let
) ;if
(unless card
(set! card (make-tool-card name args 'pending))
(tool-card-set-expanded! card (pi-app-tools-expanded? app))
(%pi-app-tool-cards-set! app (cons (cons id card) (%pi-app-tool-cards app)))
(container-add! (%pi-app-chat app) card)
) ;unless
(tool-card-set-complete! card raw-res is-err? duration)
(%pi-app-apply-diff! app id card)
) ;let*
;; 旧自定义 handler:(end name args is-error? result)
(let ((name (cadr ev))
Expand Down Expand Up @@ -315,6 +316,9 @@
;; Kimi Code OAuth 设备码登录:device_code 上屏、done 落盘/报错
((kimi_oauth_device_code) (pi-app-handle-oauth-device-code! app ev))
((kimi_oauth_done) (pi-app-handle-oauth-done! app ev))
((run_started diff_result turn_diff_summary)
(%pi-app-handle-diff-event! app ev)
) ;
((tool_execution_start)
(%pi-app-close-assistant-card! app)
(%pi-app-handle-tool-start! app ev)
Expand Down Expand Up @@ -404,6 +408,7 @@
(pi-app-add-assistant-text! app "Aborted")
) ;when
(pi-queue-finish-task! q)
(%pi-app-settle-diff! app)
(settle!)
) ;
((error)
Expand Down
1 change: 1 addition & 0 deletions app/liii/pi/app/interactive/app-history.scm
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@
) ;
) ;
;; 全部转换成功后再替换界面
(%pi-app-diff-run-set! app #f)
(container-clear! (%pi-app-chat app))
(for-each (lambda (card) (container-add! (%pi-app-chat app) card)) cards)
;; 复位旧会话的引用:工具卡片索引、thinking 引用与文本选区
Expand Down
7 changes: 6 additions & 1 deletion app/liii/pi/app/interactive/app.scm
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
(liii pi app interactive thinking)
(liii pi app interactive working)
(liii pi app interactive worker)
(liii pi app interactive diff-worker)
(liii pi app interactive bash)
(liii pi app interactive app-bash)
(liii pi app interactive app-tool-timer)
Expand Down Expand Up @@ -301,6 +302,7 @@
(event-chan (make-chan 32))
(abort-chan (make-chan 1))
(bash-jobs-chan (make-chan 16))
(display-jobs (make-chan 16))
(bash-abort-chan (make-chan 1))
(steering-chan (make-chan 16))
(follow-up-chan (make-chan 16))
Expand Down Expand Up @@ -369,6 +371,8 @@
#f
request-selection-chan
#f
display-jobs
#f
) ;%make-pi-app
) ;app
(ed (%make-pi-editor app tl))
Expand All @@ -388,8 +392,9 @@
(pi-app-rebuild-history! app session-file)
;; 无自定义 handler 时启动后台 agent worker
(unless handler
(go (%pi-display-worker display-jobs event-chan))
(go (pi-tui-agent-worker jobs-chan event-chan abort-chan session-file
steering-chan follow-up-chan request-selection-chan
steering-chan follow-up-chan request-selection-chan display-jobs
) ;pi-tui-agent-worker
) ;go
) ;unless
Expand Down
Loading
Loading