Repository navigation
Expand file tree
/
Copy pathstate.scm
More file actions
182 lines (182 loc) · 5.93 KB
/
Copy pathstate.scm
File metadata and controls
182 lines (182 loc) · 5.93 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
(define-library (gfcode plan-mode state)
(export initial-state planning? executing? todos saved-tools toggle-plan
accept-plan start-execution mark-progress finished? snapshot restore-state
assistant-text todo-lines
) ;export
(import (scheme base)
(liii string)
(liii list)
(liii json)
(liii pi ai message)
(gfcode plan-mode rules)
) ;import
(begin
(define (filter-map proc xs)
(filter (lambda (x) x) (map proc xs))
) ;define
(define-record-type <plan-state>
(make-state mode tools todos)
plan-state?
(mode state-mode)
(tools saved-tools)
(todos todos)
) ;define-record-type
(define initial-state (make-state 'normal '() '()))
(define (planning? s)
(eq? (state-mode s) 'planning)
) ;define
(define (executing? s)
(eq? (state-mode s) 'executing)
) ;define
(define (toggle-plan s tools)
(if (planning? s) initial-state (make-state 'planning tools '()))
) ;define
(define (accept-plan s lines)
(make-state 'planning (saved-tools s) (map (lambda (v) (cons v #f)) lines))
) ;define
(define (start-execution s)
(make-state 'executing (saved-tools s) (todos s))
) ;define
(define (mark-progress s text final?)
(let ((all? (and final? (plan-complete? text))))
(make-state (state-mode s)
(saved-tools s)
(map
(lambda (item n) (cons (car item) (or (cdr item) all? (step-done? text n))))
(todos s)
(iota (length (todos s)) 1)
) ;map
) ;make-state
) ;let
) ;define
(define (finished? s)
(and (executing? s) (pair? (todos s)) (every cdr (todos s)))
) ;define
(define (todo-lines s)
(map
(lambda (item n)
(string-append (number->string n) (if (cdr item) ". ✓ " ". ☐ ") (car item))
) ;lambda
(todos s)
(iota (length (todos s)) 1)
) ;map
) ;define
(define (bool b)
(if b 'true 'false)
) ;define
(define (snapshot s)
(list (cons "enabled" (bool (planning? s)))
(cons "executing" (bool (executing? s)))
(cons "toolsBeforePlanMode" (list->vector (saved-tools s)))
(cons "todos"
(list->vector
(map
(lambda (i n)
(list (cons "step" n) (cons "text" (car i)) (cons "completed" (bool (cdr i))))
) ;lambda
(todos s)
(iota (length (todos s)) 1)
) ;map
) ;list->vector
) ;cons
) ;list
) ;define
(define* (assistant-text msg (visible-only? #f))
(let* ((obj (if (pi-assistant-message? msg) (pi-message->json msg) msg))
(blocks (and (json-object? obj)
(equal? "assistant" (json-ref obj "role"))
(json-ref obj "content")
) ;and
) ;blocks
) ;
(if (vector? blocks)
(string-join
(filter-map
(lambda (b)
(let ((k (and (json-object? b) (json-ref b "type"))))
(and (or (equal? k "text") (and (not visible-only?) (equal? k "thinking")))
(string? (json-ref b k))
(json-ref b k)
) ;and
) ;let
) ;lambda
(vector->list blocks)
) ;filter-map
"\n"
) ;string-join
""
) ;if
) ;let*
) ;define*
(define (decode data tools flag)
(let* ((raw (json-ref data "toolsBeforePlanMode"))
(items (json-ref data "todos"))
(mode
(cond ((json-ref-boolean data "executing" #f) 'executing)
((or (json-ref-boolean data "enabled" #f)
(and flag (null? (json-ref data "enabled")))
) ;or
'planning
) ;
(else 'normal)
) ;cond
) ;mode
(saved
(if (vector? raw)
(filter (lambda (n) (member n tools)) (vector->list raw))
tools
) ;if
) ;saved
(steps
(if (vector? items)
(filter-map
(lambda (i)
(and (json-object? i)
(string? (json-ref i "text"))
(cons (json-ref i "text") (json-ref-boolean i "completed" #f))
) ;and
) ;lambda
(vector->list items)
) ;filter-map
'()
) ;if
) ;steps
) ;
(if (eq? mode 'normal) initial-state (make-state mode saved steps))
) ;let*
) ;define
(define (restore-state entries tools flag)
(let loop
((ls entries)
(s (if flag (toggle-plan initial-state tools) initial-state))
(boundary? #f)
) ;
(if (null? ls)
s
(let* ((e (car ls)) (kind (json-ref e "type")) (type (json-ref e "customType")))
(cond
((and (equal? kind "custom")
(equal? type "plan-mode")
(json-object? (json-ref e "data"))
) ;and
(let ((next (decode (json-ref e "data") tools flag)))
(loop (cdr ls) next (and boundary? (executing? next) (executing? s)))
) ;let
) ;
((and (equal? kind "custom_message") (equal? type "plan-mode-execute"))
(loop (cdr ls) s #t)
) ;
((and boundary? (executing? s) (equal? kind "message"))
(loop (cdr ls)
(mark-progress s (assistant-text (json-ref e "message") #t) #f)
#t
) ;loop
) ;
(else (loop (cdr ls) s boundary?))
) ;cond
) ;let*
) ;if
) ;let
) ;define
) ;begin
) ;define-library