Skip to content

Commit d30459a

Browse files
authored
Merge pull request #70 from RhysSullivan/pr/openapi-batch-writes
Speed up OpenAPI source registration writes
2 parents 6ce4603 + 814991b commit d30459a

8 files changed

Lines changed: 92 additions & 34 deletions

File tree

‎packages/core/sdk/src/plugin-kv.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export interface Kv {
1818
readonly delete: (namespace: string, key: string) => Effect.Effect<boolean>;
1919
readonly list: (namespace: string) => Effect.Effect<readonly { key: string; value: string }[]>;
2020
readonly deleteAll: (namespace: string) => Effect.Effect<number>;
21+
readonly withTransaction?: <A, E>(
22+
effect: Effect.Effect<A, E, never>,
23+
) => Effect.Effect<A, E, never>;
2124
}
2225

2326
/**
@@ -30,6 +33,9 @@ export interface ScopedKv {
3033
readonly delete: (key: string) => Effect.Effect<boolean>;
3134
readonly list: () => Effect.Effect<readonly { key: string; value: string }[]>;
3235
readonly deleteAll: () => Effect.Effect<number>;
36+
readonly withTransaction?: <A, E>(
37+
effect: Effect.Effect<A, E, never>,
38+
) => Effect.Effect<A, E, never>;
3339
}
3440

3541
/**
@@ -41,6 +47,7 @@ export const scopeKv = (kv: Kv, namespace: string): ScopedKv => ({
4147
delete: (key) => kv.delete(namespace, key),
4248
list: () => kv.list(namespace),
4349
deleteAll: () => kv.deleteAll(namespace),
50+
withTransaction: kv.withTransaction,
4451
});
4552

4653
/**
@@ -62,5 +69,6 @@ export const makeInMemoryScopedKv = (): ScopedKv => {
6269
store.clear();
6370
return n;
6471
}),
72+
withTransaction: (effect) => effect,
6573
};
6674
};

‎packages/core/storage-file/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,5 @@ export const makeScopedKv = (kv: Kv, folder: string): Kv => ({
9999
delete: (namespace, key) => kv.delete(`${folder}::${namespace}`, key),
100100
list: (namespace) => kv.list(`${folder}::${namespace}`),
101101
deleteAll: (namespace) => kv.deleteAll(`${folder}::${namespace}`),
102+
withTransaction: kv.withTransaction,
102103
});

‎packages/core/storage-file/src/plugin-kv.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Kv implementations — SQLite and in-memory
33
// ---------------------------------------------------------------------------
44

5-
import { Effect } from "effect";
5+
import { Effect, Exit } from "effect";
66
import type * as SqlClient from "@effect/sql/SqlClient";
77
import type { Kv } from "@executor/sdk";
88

@@ -58,6 +58,27 @@ export const makeSqliteKv = (sql: SqlClient.SqlClient): Kv => ({
5858
yield* sql`DELETE FROM kv WHERE namespace = ${namespace}`;
5959
return before[0]?.c ?? 0;
6060
})),
61+
62+
withTransaction: <A, E>(effect: Effect.Effect<A, E, never>) =>
63+
absorbSql(
64+
Effect.uninterruptibleMask((restore) =>
65+
Effect.gen(function* () {
66+
yield* sql`BEGIN`;
67+
const exit = yield* restore(effect).pipe(Effect.exit);
68+
69+
if (Exit.isSuccess(exit)) {
70+
yield* sql`COMMIT`;
71+
} else {
72+
yield* sql`ROLLBACK`;
73+
}
74+
75+
return yield* Exit.matchEffect(exit, {
76+
onFailure: Effect.failCause,
77+
onSuccess: Effect.succeed,
78+
});
79+
}),
80+
),
81+
),
6182
});
6283

6384
// ---------------------------------------------------------------------------
@@ -95,5 +116,7 @@ export const makeInMemoryKv = (): Kv => {
95116
store.delete(namespace);
96117
return count;
97118
}),
119+
120+
withTransaction: (effect) => effect,
98121
};
99122
};

‎packages/core/storage-file/src/tool-registry.ts‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export const makeKvToolRegistry = (
3030
toolsKv: ScopedKv,
3131
defsKv: ScopedKv,
3232
) => {
33+
const withKvTransaction = <A, E>(
34+
kv: ScopedKv,
35+
effect: Effect.Effect<A, E, never>,
36+
): Effect.Effect<A, E, never> => kv.withTransaction?.(effect) ?? effect;
37+
3338
const runtimeTools = new Map<string, ToolRegistration>();
3439
const runtimeHandlers = new Map<string, RuntimeToolHandler>();
3540
const runtimeDefs = new Map<string, unknown>();
@@ -104,11 +109,14 @@ export const makeKvToolRegistry = (
104109
}),
105110

106111
registerDefinitions: (newDefs: Record<string, unknown>) =>
107-
Effect.gen(function* () {
108-
for (const [name, schema] of Object.entries(newDefs)) {
109-
yield* defsKv.set(name, JSON.stringify(schema));
110-
}
111-
}),
112+
withKvTransaction(
113+
defsKv,
114+
Effect.gen(function* () {
115+
for (const [name, schema] of Object.entries(newDefs)) {
116+
yield* defsKv.set(name, JSON.stringify(schema));
117+
}
118+
}),
119+
),
112120

113121
registerRuntimeDefinitions: (newDefs: Record<string, unknown>) =>
114122
Effect.sync(() => {
@@ -160,11 +168,14 @@ export const makeKvToolRegistry = (
160168
}),
161169

162170
register: (newTools: readonly ToolRegistration[]) =>
163-
Effect.gen(function* () {
164-
for (const t of newTools) {
165-
yield* toolsKv.set(t.id, encodeTool(t));
166-
}
167-
}),
171+
withKvTransaction(
172+
toolsKv,
173+
Effect.gen(function* () {
174+
for (const t of newTools) {
175+
yield* toolsKv.set(t.id, encodeTool(t));
176+
}
177+
}),
178+
),
168179

169180
registerRuntime: (newTools: readonly ToolRegistration[]) =>
170181
Effect.sync(() => {

‎packages/plugins/openapi/src/sdk/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export {
88
} from "./plugin";
99
export {
1010
type OpenApiOperationStore,
11+
type StoredOperation,
1112
type StoredSource,
1213
type SourceConfig,
1314
} from "./operation-store";

‎packages/plugins/openapi/src/sdk/kv-operation-store.ts‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { Effect, Schema } from "effect";
88
import { 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";
1111
import { OperationBinding, InvocationConfig, HeaderValue } from "./types";
1212

1313
// ---------------------------------------------------------------------------
@@ -45,7 +45,13 @@ const decodeSource = Schema.decodeUnknownSync(Schema.parseJson(StoredSourceSchem
4545
const 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

‎packages/plugins/openapi/src/sdk/operation-store.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@ export interface StoredSource {
2020
readonly config: SourceConfig;
2121
}
2222

23+
export interface StoredOperation {
24+
readonly toolId: ToolId;
25+
readonly namespace: string;
26+
readonly binding: OperationBinding;
27+
readonly config: InvocationConfig;
28+
}
29+
2330
export interface OpenApiOperationStore {
2431
readonly get: (
2532
toolId: ToolId,
2633
) => Effect.Effect<{ binding: OperationBinding; config: InvocationConfig } | null>;
2734

28-
readonly put: (
29-
toolId: ToolId,
30-
namespace: string,
31-
binding: OperationBinding,
32-
config: InvocationConfig,
33-
) => Effect.Effect<void>;
35+
readonly put: (entries: readonly StoredOperation[]) => Effect.Effect<void>;
3436

3537
readonly remove: (toolId: ToolId) => Effect.Effect<void>;
3638

‎packages/plugins/openapi/src/sdk/plugin.ts‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -271,16 +271,13 @@ export const openApiPlugin = (options?: {
271271
toRegistration(def, namespace),
272272
);
273273

274-
yield* Effect.forEach(
275-
definitions,
276-
(def) =>
277-
operationStore.put(
278-
ToolId.make(`${namespace}.${def.toolPath}`),
279-
namespace,
280-
toBinding(def),
281-
invocationConfig,
282-
),
283-
{ discard: true },
274+
yield* operationStore.put(
275+
definitions.map((def) => ({
276+
toolId: ToolId.make(`${namespace}.${def.toolPath}`),
277+
namespace,
278+
binding: toBinding(def),
279+
config: invocationConfig,
280+
})),
284281
);
285282

286283
yield* ctx.tools.register(registrations);

0 commit comments

Comments
 (0)