77import { Effect , Schema } from "effect" ;
88import { scopeKv , makeInMemoryScopedKv , type Kv , type ToolId , type ScopedKv } from "@executor/sdk" ;
99
10- import type { OpenApiOperationStore , StoredSource } from "./operation-store" ;
10+ import type { OpenApiOperationStore , StoredOperation , StoredSource } from "./operation-store" ;
1111import { OperationBinding , InvocationConfig , HeaderValue } from "./types" ;
1212
1313// ---------------------------------------------------------------------------
@@ -45,7 +45,13 @@ const decodeSource = Schema.decodeUnknownSync(Schema.parseJson(StoredSourceSchem
4545const makeStore = (
4646 bindings : ScopedKv ,
4747 sources : ScopedKv ,
48- ) : OpenApiOperationStore => ( {
48+ ) : OpenApiOperationStore => {
49+ const withKvTransaction = < A , E > (
50+ kv : ScopedKv ,
51+ effect : Effect . Effect < A , E , never > ,
52+ ) : Effect . Effect < A , E , never > => kv . withTransaction ?.( effect ) ?? effect ;
53+
54+ return ( {
4955 get : ( toolId ) =>
5056 Effect . gen ( function * ( ) {
5157 const raw = yield * bindings . get ( toolId ) ;
@@ -54,10 +60,18 @@ const makeStore = (
5460 return { binding : entry . binding , config : entry . config } ;
5561 } ) ,
5662
57- put : ( toolId , namespace , binding , config ) =>
58- bindings . set (
59- toolId ,
60- encodeEntry ( new StoredEntry ( { namespace, binding, config } ) ) ,
63+ put : ( entries : readonly StoredOperation [ ] ) =>
64+ withKvTransaction (
65+ bindings ,
66+ Effect . forEach (
67+ entries ,
68+ ( { toolId, namespace, binding, config } ) =>
69+ bindings . set (
70+ toolId ,
71+ encodeEntry ( new StoredEntry ( { namespace, binding, config } ) ) ,
72+ ) ,
73+ { discard : true } ,
74+ ) ,
6175 ) ,
6276
6377 remove : ( toolId ) => bindings . delete ( toolId ) . pipe ( Effect . asVoid ) ,
@@ -98,7 +112,8 @@ const makeStore = (
98112 const entries = yield * sources . list ( ) ;
99113 return entries . map ( ( e ) => decodeSource ( e . value ) as StoredSource ) ;
100114 } ) ,
101- } ) ;
115+ } ) ;
116+ } ;
102117
103118// ---------------------------------------------------------------------------
104119// Factory from global Kv
0 commit comments