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
206 changes: 121 additions & 85 deletions calcit.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -545,19 +545,14 @@
tag? $ :model state
:model state
, :gemini
is-viewing-history? $ and (js-present-dynamic? current-session-id)
.fold
assert-type
first $ filter sessions $ fn (s)
hint-fn $ Fn app.schema/ChatSession $ -> Bool
= (:id s) (decode-map-as current-session-id 'String)
Option app.schema/ChatSession
fn ()
hint-fn $ {} $ :return 'Bool
, false
fn (session)
hint-fn $ Fn app.schema/ChatSession $ -> Bool
:is-history? session
is-viewing-history? $ match current-session-id
(:none) false
(:some id)
.fold
first $ filter sessions $ fn (session)
= (:id session) id
fn () false
fn (session) (:is-history? session)
last-assistant $ .fold
assert-type (last messages) (Option app.schema/ChatMessage)
fn ()
Expand Down Expand Up @@ -661,7 +656,7 @@
and
> (.len messages) 0
:done? state
not $ js-present-dynamic? current-session-id
not $ .some? current-session-id
d! $ DispatchOp :save-session state
d! $ DispatchOp :load-session cursor state $ -> sessions
filter $ fn (s)
Expand Down Expand Up @@ -949,7 +944,7 @@
and
> (.len messages) 0
:done? state
not $ js-present-dynamic? current-session-id
not $ .some? current-session-id
d! $ DispatchOp :save-session state
d! $ DispatchOp :session None
submit-with-key!
Expand Down Expand Up @@ -2307,7 +2302,7 @@
let
storage $ browser/window-local-storage
store $ :store @*reel
storage .set-item! (:storage-key config/site) (format-cirru-edn store)
storage .set-item! (:storage-key config/site) (schema/encode-store store)
, &unit
:examples $ []
:schema $ quote $ Fn (-> Unit) (:features :js-ffi)
Expand Down Expand Up @@ -2423,15 +2418,40 @@
'Store $ %{} 'CodeEntry (:doc |)
:code $ quote $ defstruct Store (:states Map)
:sessions $ List app.schema/ChatSession
:current-session-id Dynamic
:model Dynamic
:current-session-id $ Option String
:model $ Option Tag
:archived-count Number
:examples $ []
:schema $ quote StructDef
'WidgetState $ %{} 'CodeEntry (:doc |)
:code $ quote $ defunion WidgetState ChatState MessageBoxState
:examples $ []
:schema $ quote UnionDef
'encode-store $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn encode-store (store)
let
fields $ &struct:to-map store
with-selection $ match (:current-session-id store)
(:some id) (assoc fields :current-session-id id)
(:none) (assoc fields :current-session-id nil)
format-cirru-edn $ match (:model store)
(:some model) (assoc with-selection :model model)
(:none) (assoc with-selection :model nil)
:examples $ []
:schema $ quote $ Fn Store (-> String)
:tests $ [] $ %{} 'TestEntry (:name |keeps-legacy-selection-fields)
:code $ quote $ let
selected $ -> store
assoc :current-session-id $ Some |legacy
assoc :model $ Some :anthropic
selected-wire $ parse-cirru-edn $ encode-store selected
empty-wire $ parse-cirru-edn $ encode-store store
assert= (Some |legacy) (get selected-wire :current-session-id)
assert= (Some :anthropic) (get selected-wire :model)
assert= true $ nil? $ .unwrap (get empty-wire :current-session-id)
assert= true $ nil? $ .unwrap (get empty-wire :model)
assert= selected $ normalize-store selected-wire
assert= store $ normalize-store empty-wire
'normalize-chat-message $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn normalize-chat-message (raw)
hint-fn $ Fn T (-> app.schema/ChatMessage) (:generics T) (:features :js-ffi)
Expand Down Expand Up @@ -2514,16 +2534,11 @@
:code $ quote $ defn normalize-store (raw)
hint-fn $ Fn T (-> app.schema/Store) (:generics T) (:features :js-ffi)
if
and (struct? raw) (&struct:matches? raw Store)
struct-match raw
Store typed $ if
match (struct-definition typed)
(:none) false
(:some definition) (identical? definition Store)
, typed $ Store :states (:states typed) :sessions
map (:sessions typed)
fn (session) (normalize-chat-session session)
, :current-session-id (:current-session-id typed) :model (:model typed) :archived-count (:archived-count typed)
and (struct? raw)
match (struct-definition raw)
(:some definition) (identical? definition Store)
(:none) false
struct-match raw (Store typed typed)
_ other $ raise "|Unexpected nominal value for Store"
let
input $ if (struct? raw)
Expand All @@ -2532,52 +2547,81 @@
&struct:to-map raw
raise "|Unexpected nominal value for Store"
, raw
if (map? input)
let
data $ decode-map-as input $ Map Tag Dynamic
raw-session-value $ .unwrap-or (get data :sessions) ([])
raw-sessions $ if (list? raw-session-value)
assert-type raw-session-value $ List Dynamic
[]
sessions $ map raw-sessions $ fn (session) (normalize-chat-session session)
patched $ dissoc data :current-chapter-id
base $ decode-map-as
assoc patched :sessions $ []
, 'app.schema/Store
Store :states (:states base) :sessions sessions :current-session-id (:current-session-id base) :model (:model base) :archived-count $ :archived-count base
raise "|Expected store map"
data $ decode-map-as input $ Map Tag Dynamic
raw-session-value $ .unwrap-or (get data :sessions) ([])
raw-sessions $ if (list? raw-session-value)
assert-type raw-session-value $ List Dynamic
[]
sessions $ map raw-sessions $ fn (session) (normalize-chat-session session)
patched $ -> data (dissoc :current-chapter-id) (assoc :sessions sessions)
assoc :current-session-id $ match (get data :current-session-id)
(:none) None
(:some session-id)
cond
nil? session-id
, None
(string? session-id) (Some session-id)
true $ match session-id
(:some id)
Some $ decode-map-as id 'String
(:none) None
_ $ raise "|Invalid saved session ID"
assoc :model $ match (get data :model)
(:none) None
(:some model)
cond
tag? model
Some model
(enum? model)
match model
(:some tag)
Some $ decode-map-as tag 'Tag
(:none) None
_ $ raise "|Invalid saved model"
true None
decode-map-as patched 'app.schema/Store
:examples $ []
:schema $ quote $ Fn T (-> Store) (:generics T) (:features :js-ffi)
:tests $ [] $ %{} 'TestEntry (:name |migrates-legacy-message-thinking)
:code $ quote $ let
legacy $ {}
:states $ {} $ :cursor ([])
:sessions $ [] $ {} (:id |legacy) (:created-at 1)
:messages $ [] $ {} (:role :user) (:content |hello)
:model :gemini
:preview |hello
:is-history? true
:current-session-id nil
:model nil
:archived-count 0
normalized $ assert-type (normalize-store legacy) 'app.schema/Store
sessions $ assert-type (:sessions normalized) (List app.schema/ChatSession)
fallback-session $ ChatSession :id |fallback :created-at 0 :messages ([]) :model :gemini :preview | :is-history? false
session $ .unwrap-or
assert-type (first sessions) (Option app.schema/ChatSession)
, fallback-session
messages $ assert-type (:messages session) (List app.schema/ChatMessage)
fallback-message $ ChatMessage :role :user :content | :thinking |fallback
message $ .unwrap-or
assert-type (first messages) (Option app.schema/ChatMessage)
, fallback-message
is $ = | $ :thinking message
:tags $ #{} :regression :unit
:tests $ []
%{} 'TestEntry (:name |migrates-legacy-message-thinking)
:code $ quote $ let
legacy $ {}
:states $ {} $ :cursor ([])
:sessions $ [] $ {} (:id |legacy) (:created-at 1)
:messages $ [] $ {} (:role :user) (:content |hello)
:model :gemini
:preview |hello
:is-history? true
:current-session-id nil
:model nil
:archived-count 0
normalized $ assert-type (normalize-store legacy) 'app.schema/Store
sessions $ assert-type (:sessions normalized) (List app.schema/ChatSession)
fallback-session $ ChatSession :id |fallback :created-at 0 :messages ([]) :model :gemini :preview | :is-history? false
session $ .unwrap-or
assert-type (first sessions) (Option app.schema/ChatSession)
, fallback-session
messages $ assert-type (:messages session) (List app.schema/ChatMessage)
fallback-message $ ChatMessage :role :user :content | :thinking |fallback
message $ .unwrap-or
assert-type (first messages) (Option app.schema/ChatMessage)
, fallback-message
is $ = | $ :thinking message
:tags $ #{} :regression :unit
%{} 'TestEntry (:name |optional-selection-roundtrip)
:code $ quote $ let
old $ &struct:to-map store
restored $ normalize-store $ assoc (assoc old :current-session-id |legacy) :model :anthropic
round-tripped $ normalize-store $ parse-cirru-edn (format-cirru-edn restored)
assert= (Some |legacy) (:current-session-id restored)
assert= (Some :anthropic) (:model restored)
assert= restored round-tripped
assert= store $ normalize-store $ parse-cirru-edn (format-cirru-edn store)
'store $ %{} 'CodeEntry (:doc |)
:code $ quote $ def store
Store :states
{} $ :cursor $ []
, :sessions ([]) :current-session-id nil :model nil :archived-count 0
, :sessions ([]) :current-session-id None :model None :archived-count 0
:examples $ []
:schema $ quote Store
:ns $ %{} 'NsEntry (:doc |)
Expand Down Expand Up @@ -2647,21 +2691,13 @@
assoc store :states $ decode-map-as updated-states $ Map Dynamic Dynamic
(:hydrate-storage data) data
(:change-model)
if
let
model $ :model store
if (tag? model) (= model :anthropic) false
assoc store :model :gemini
assoc store :model :anthropic
assoc store :model $ if
= (:model store) (Some :anthropic)
Some :gemini
Some :anthropic
(:save-session state)
let
store1 $ save-current-session store state
assoc store1 :current-session-id nil
(:session id)
match id
(:none)
assoc store :current-session-id $ :current-session-id app.schema/store
(:some selected) (assoc store :current-session-id selected)
assoc (save-current-session store state) :current-session-id None
(:session id) (assoc store :current-session-id id)
(:load-session cursor state session)
let
typed-session session
Expand All @@ -2672,7 +2708,7 @@
assoc :messages $ :messages typed-session
assoc :done? true
Map Dynamic Dynamic
assoc store1 :current-session-id $ :id typed-session
assoc store1 :current-session-id $ Some $ :id typed-session
(:remove-session id)
assoc store :sessions $ filter (:sessions store)
fn (s)
Expand All @@ -2682,13 +2718,13 @@
-> store
assoc :sessions $ []
assoc :archived-count new-count
assoc :current-session-id nil
assoc :current-session-id None
(:update-archived-count new-count)
-> store $ assoc :archived-count new-count
(:clear-sessions)
-> store
assoc :sessions $ []
assoc :current-session-id nil
assoc :current-session-id None
_ $ do (eprintln "|unknown op:" op) store
:examples $ []
:schema $ quote $ Fn Store app.schema/DispatchOp String Number (-> Store)
Expand Down
5 changes: 1 addition & 4 deletions config/calcit-quality.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@
"app.main/reload!": {
"unsafe-coerce/explicit-unsafe": 1
},
"app.schema/store": {
"app.schema/encode-store": {
"code-nil/unresolved": 2
},
"app.updater/updater": {
"code-nil/unresolved": 3
}
}
}
Loading