@yieldstar/sqlite-runtime 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/bun.d.ts +13 -0
  2. package/dist/bun.d.ts.map +1 -0
  3. package/dist/bun.js +43 -0
  4. package/dist/dao/step-response-dao.d.ts +19 -0
  5. package/dist/dao/step-response-dao.d.ts.map +1 -0
  6. package/dist/dao/task-queue-dao.d.ts +21 -0
  7. package/dist/dao/task-queue-dao.d.ts.map +1 -0
  8. package/dist/dao/timers-dao.d.ts +22 -0
  9. package/dist/dao/timers-dao.d.ts.map +1 -0
  10. package/dist/index-4vq70w0r.js +796 -0
  11. package/dist/index-h7bm9xg4.js +26 -0
  12. package/dist/index-s8wqgfwt.js +19 -0
  13. package/dist/index-x891hxwg.js +796 -0
  14. package/dist/index.d.ts +9 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +16 -0
  17. package/dist/node-driver.d.ts +10 -0
  18. package/dist/node-driver.d.ts.map +1 -0
  19. package/dist/node.d.ts +13 -0
  20. package/dist/node.d.ts.map +1 -0
  21. package/dist/node.js +49 -0
  22. package/dist/sqlite-db.d.ts +29 -0
  23. package/dist/sqlite-db.d.ts.map +1 -0
  24. package/dist/sqlite-driver.d.ts +21 -0
  25. package/dist/sqlite-driver.d.ts.map +1 -0
  26. package/dist/sqlite-event-loop.d.ts +18 -0
  27. package/dist/sqlite-event-loop.d.ts.map +1 -0
  28. package/dist/sqlite-heap.d.ts +26 -0
  29. package/dist/sqlite-heap.d.ts.map +1 -0
  30. package/dist/sqlite-scheduler.d.ts +13 -0
  31. package/dist/sqlite-scheduler.d.ts.map +1 -0
  32. package/dist/sqlite-store.d.ts +54 -0
  33. package/dist/sqlite-store.d.ts.map +1 -0
  34. package/dist/sqlite-store.test.d.ts +2 -0
  35. package/dist/sqlite-store.test.d.ts.map +1 -0
  36. package/dist/sqlite-task-queue.d.ts +26 -0
  37. package/dist/sqlite-task-queue.d.ts.map +1 -0
  38. package/dist/sqlite-timers.d.ts +18 -0
  39. package/dist/sqlite-timers.d.ts.map +1 -0
  40. package/package.json +43 -0
  41. package/src/bun.ts +32 -0
  42. package/src/dao/step-response-dao.ts +80 -0
  43. package/src/dao/task-queue-dao.ts +79 -0
  44. package/src/dao/timers-dao.ts +81 -0
  45. package/src/index.ts +14 -0
  46. package/src/node.ts +47 -0
  47. package/src/sqlite-db.ts +70 -0
  48. package/src/sqlite-driver.ts +30 -0
  49. package/src/sqlite-event-loop.ts +41 -0
  50. package/src/sqlite-heap.ts +54 -0
  51. package/src/sqlite-scheduler.ts +24 -0
  52. package/src/sqlite-store.ts +755 -0
  53. package/src/sqlite-task-queue.ts +64 -0
  54. package/src/sqlite-timers.ts +40 -0
  55. package/tsconfig.json +9 -0
  56. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,9 @@
1
+ export { SqliteHeapClient } from "./sqlite-heap";
2
+ export { SqliteStoreClient } from "./sqlite-store";
3
+ export { SqliteSchedulerClient } from "./sqlite-scheduler";
4
+ export { SqliteEventLoop } from "./sqlite-event-loop";
5
+ export { SqliteTaskQueueClient } from "./sqlite-task-queue";
6
+ export { SqliteTimersClient } from "./sqlite-timers";
7
+ export type { SqliteDriver, SqliteParameters, SqliteParameterValue, SqliteRunResult, SqliteStatement, } from "./sqlite-driver";
8
+ export type { SqliteDbOpts } from "./sqlite-db";
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,YAAY,EACV,YAAY,EACZ,gBAAgB,EAChB,oBAAoB,EACpB,eAAe,EACf,eAAe,GAChB,MAAM,iBAAiB,CAAC;AACzB,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ import {
2
+ SqliteEventLoop,
3
+ SqliteHeapClient,
4
+ SqliteSchedulerClient,
5
+ SqliteStoreClient,
6
+ SqliteTaskQueueClient,
7
+ SqliteTimersClient
8
+ } from "./index-4vq70w0r.js";
9
+ export {
10
+ SqliteTimersClient,
11
+ SqliteTaskQueueClient,
12
+ SqliteStoreClient,
13
+ SqliteSchedulerClient,
14
+ SqliteHeapClient,
15
+ SqliteEventLoop
16
+ };
@@ -0,0 +1,10 @@
1
+ import type { DatabaseSync } from "node:sqlite";
2
+ import type { SqliteDriver, SqliteStatement } from "./sqlite-driver";
3
+ export declare class NodeSqliteDriver implements SqliteDriver {
4
+ private readonly database;
5
+ constructor(database: DatabaseSync);
6
+ run(sql: string): void;
7
+ query<Row>(sql: string): SqliteStatement<Row>;
8
+ close(): void;
9
+ }
10
+ //# sourceMappingURL=node-driver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-driver.d.ts","sourceRoot":"","sources":["../src/node-driver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAiB,MAAM,aAAa,CAAC;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAoB,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEvF,qBAAa,gBAAiB,YAAW,YAAY;IACvC,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAAR,QAAQ,EAAE,YAAY;IAEnD,GAAG,CAAC,GAAG,EAAE,MAAM;IAIf,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,eAAe,CAAC,GAAG,CAAC;IAkB7C,KAAK;CAGN"}
package/dist/node.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { DatabaseSync } from "node:sqlite";
2
+ import { type SqliteDbOpts } from "./sqlite-db";
3
+ import type { SqliteDriver, SqliteStatement } from "./sqlite-driver";
4
+ export * from "./index";
5
+ export declare class NodeSqliteDriver implements SqliteDriver {
6
+ private readonly database;
7
+ constructor(database: DatabaseSync);
8
+ run(sql: string): void;
9
+ query<Row>(sql: string): SqliteStatement<Row>;
10
+ close(): void;
11
+ }
12
+ export declare function createSqliteDb(opts?: SqliteDbOpts): NodeSqliteDriver;
13
+ //# sourceMappingURL=node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAsB,MAAM,aAAa,CAAC;AAC/D,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EACV,YAAY,EAEZ,eAAe,EAChB,MAAM,iBAAiB,CAAC;AAEzB,cAAc,SAAS,CAAC;AAExB,qBAAa,gBAAiB,YAAW,YAAY;IACvC,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAAR,QAAQ,EAAE,YAAY;IAEnD,GAAG,CAAC,GAAG,EAAE,MAAM;IAIf,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,eAAe,CAAC,GAAG,CAAC;IAI7C,KAAK;CAGN;AAaD,wBAAgB,cAAc,CAAC,IAAI,CAAC,EAAE,YAAY,oBAKjD"}
package/dist/node.js ADDED
@@ -0,0 +1,49 @@
1
+ import {
2
+ createSqliteDriver,
3
+ wrapSqliteStatement
4
+ } from "./index-h7bm9xg4.js";
5
+ import {
6
+ SqliteEventLoop,
7
+ SqliteHeapClient,
8
+ SqliteSchedulerClient,
9
+ SqliteStoreClient,
10
+ SqliteTaskQueueClient,
11
+ SqliteTimersClient
12
+ } from "./index-4vq70w0r.js";
13
+
14
+ // src/node.ts
15
+ import { DatabaseSync } from "node:sqlite";
16
+ class NodeSqliteDriver {
17
+ database;
18
+ constructor(database) {
19
+ this.database = database;
20
+ }
21
+ run(sql) {
22
+ this.database.exec(sql);
23
+ }
24
+ query(sql) {
25
+ return wrapSqliteStatement(this.database.prepare(sql), normalizeParameters);
26
+ }
27
+ close() {
28
+ this.database.close();
29
+ }
30
+ }
31
+ function normalizeParameters(parameters) {
32
+ return Object.fromEntries(Object.entries(parameters).map(([name, value]) => [
33
+ name,
34
+ typeof value === "boolean" ? Number(value) : value
35
+ ]));
36
+ }
37
+ function createSqliteDb(opts) {
38
+ return createSqliteDriver((path) => new NodeSqliteDriver(new DatabaseSync(path)), opts);
39
+ }
40
+ export {
41
+ createSqliteDb,
42
+ SqliteTimersClient,
43
+ SqliteTaskQueueClient,
44
+ SqliteStoreClient,
45
+ SqliteSchedulerClient,
46
+ SqliteHeapClient,
47
+ SqliteEventLoop,
48
+ NodeSqliteDriver
49
+ };
@@ -0,0 +1,29 @@
1
+ import type { SqliteDriver, SqliteParameters, SqliteRunResult, SqliteStatement } from "./sqlite-driver";
2
+ export type SqliteDbOpts = {
3
+ /**
4
+ * Path to the SQLite database file
5
+ * @default "./.db/yieldstar.sqlite"
6
+ */
7
+ path?: string;
8
+ /**
9
+ * Enable Write-Ahead Logging (WAL) mode.
10
+ * Recommended for better performance and concurrency.
11
+ * @default true
12
+ */
13
+ wal?: boolean;
14
+ };
15
+ export declare function createSqliteDriver<Driver extends SqliteDriver>(create: (path: string) => Driver, opts?: SqliteDbOpts): Driver;
16
+ /** The native prepared-statement shape connectors adapt to SqliteStatement. */
17
+ type NativeStatement = {
18
+ get(parameters?: any): unknown;
19
+ all(parameters?: any): unknown[];
20
+ run(parameters?: any): SqliteRunResult;
21
+ };
22
+ /**
23
+ * Adapt a native prepared statement to the SqliteStatement contract: bind
24
+ * parameters are only passed when provided, with `bind` mapping them to the
25
+ * native driver's binding shape.
26
+ */
27
+ export declare function wrapSqliteStatement<Row>(statement: NativeStatement, bind?: (parameters: SqliteParameters) => any): SqliteStatement<Row>;
28
+ export {};
29
+ //# sourceMappingURL=sqlite-db.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-db.d.ts","sourceRoot":"","sources":["../src/sqlite-db.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,eAAe,EAChB,MAAM,iBAAiB,CAAC;AAEzB,MAAM,MAAM,YAAY,GAAG;IACzB;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;;OAIG;IACH,GAAG,CAAC,EAAE,OAAO,CAAC;CACf,CAAC;AAOF,wBAAgB,kBAAkB,CAAC,MAAM,SAAS,YAAY,EAC5D,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,EAChC,IAAI,CAAC,EAAE,YAAY,UAWpB;AAED,+EAA+E;AAC/E,KAAK,eAAe,GAAG;IACrB,GAAG,CAAC,UAAU,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC;IAC/B,GAAG,CAAC,UAAU,CAAC,EAAE,GAAG,GAAG,OAAO,EAAE,CAAC;IACjC,GAAG,CAAC,UAAU,CAAC,EAAE,GAAG,GAAG,eAAe,CAAC;CACxC,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EACrC,SAAS,EAAE,eAAe,EAC1B,IAAI,GAAE,CAAC,UAAU,EAAE,gBAAgB,KAAK,GAAgC,GACvE,eAAe,CAAC,GAAG,CAAC,CAWtB"}
@@ -0,0 +1,21 @@
1
+ export type SqliteParameterValue = string | number | bigint | boolean | Uint8Array | null;
2
+ export type SqliteParameters = Record<`$${string}`, SqliteParameterValue>;
3
+ export type SqliteRunResult = {
4
+ changes: number | bigint;
5
+ };
6
+ /**
7
+ * Callers must supply exactly the named parameters used by the SQL, including
8
+ * each `$` prefix. Connectors do not normalize additional names because native
9
+ * drivers differ: some ignore them while others throw.
10
+ */
11
+ export interface SqliteStatement<Row> {
12
+ get(parameters?: SqliteParameters): Row | undefined;
13
+ all(parameters?: SqliteParameters): Row[];
14
+ run(parameters?: SqliteParameters): SqliteRunResult;
15
+ }
16
+ export interface SqliteDriver {
17
+ run(sql: string): void;
18
+ query<Row>(sql: string): SqliteStatement<Row>;
19
+ close(): void;
20
+ }
21
+ //# sourceMappingURL=sqlite-driver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-driver.d.ts","sourceRoot":"","sources":["../src/sqlite-driver.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,oBAAoB,GAC5B,MAAM,GACN,MAAM,GACN,MAAM,GACN,OAAO,GACP,UAAU,GACV,IAAI,CAAC;AAET,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC,IAAI,MAAM,EAAE,EAAE,oBAAoB,CAAC,CAAC;AAE1E,MAAM,MAAM,eAAe,GAAG;IAC5B,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;CAC1B,CAAC;AAEF;;;;GAIG;AACH,MAAM,WAAW,eAAe,CAAC,GAAG;IAClC,GAAG,CAAC,UAAU,CAAC,EAAE,gBAAgB,GAAG,GAAG,GAAG,SAAS,CAAC;IACpD,GAAG,CAAC,UAAU,CAAC,EAAE,gBAAgB,GAAG,GAAG,EAAE,CAAC;IAC1C,GAAG,CAAC,UAAU,CAAC,EAAE,gBAAgB,GAAG,eAAe,CAAC;CACrD;AAED,MAAM,WAAW,YAAY;IAC3B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC;IAC9C,KAAK,IAAI,IAAI,CAAC;CACf"}
@@ -0,0 +1,18 @@
1
+ import { SqliteTaskQueue } from "./sqlite-task-queue";
2
+ import { SqliteTimers } from "./sqlite-timers";
3
+ import type { EventProcessor } from "@yieldstar/core";
4
+ import type { Logger } from "pino";
5
+ import type { SqliteDriver } from "./sqlite-driver";
6
+ export declare class SqliteEventLoop {
7
+ taskQueue: SqliteTaskQueue;
8
+ timers: SqliteTimers;
9
+ private isRunning;
10
+ constructor(db: SqliteDriver);
11
+ start(params: {
12
+ onNewEvent: EventProcessor;
13
+ logger: Logger;
14
+ }): void;
15
+ stop(): void;
16
+ private loop;
17
+ }
18
+ //# sourceMappingURL=sqlite-event-loop.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-event-loop.d.ts","sourceRoot":"","sources":["../src/sqlite-event-loop.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AACnC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEpD,qBAAa,eAAe;IAC1B,SAAS,EAAE,eAAe,CAAC;IAC3B,MAAM,EAAE,YAAY,CAAC;IACrB,OAAO,CAAC,SAAS,CAAkB;gBAEvB,EAAE,EAAE,YAAY;IAK5B,KAAK,CAAC,MAAM,EAAE;QAAE,UAAU,EAAE,cAAc,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE;IAK5D,IAAI;YAIU,IAAI;CAenB"}
@@ -0,0 +1,26 @@
1
+ import type { HeapClient } from "@yieldstar/core";
2
+ import type { SqliteDriver } from "./sqlite-driver";
3
+ export declare class SqliteHeapClient implements HeapClient {
4
+ private stepResponsesDao;
5
+ constructor(db: SqliteDriver);
6
+ getAllSteps(): Promise<unknown[]>;
7
+ deleteAll(): Promise<void>;
8
+ readStep(params: {
9
+ executionId: string;
10
+ stepKey: string;
11
+ }): Promise<{
12
+ stepResponseJson: string;
13
+ meta: {
14
+ attempt: number;
15
+ done: boolean;
16
+ };
17
+ } | null>;
18
+ writeStep(params: {
19
+ executionId: string;
20
+ stepKey: string;
21
+ stepAttempt: number;
22
+ stepDone: boolean;
23
+ stepResponseJson: string;
24
+ }): Promise<void>;
25
+ }
26
+ //# sourceMappingURL=sqlite-heap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-heap.d.ts","sourceRoot":"","sources":["../src/sqlite-heap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEpD,qBAAa,gBAAiB,YAAW,UAAU;IACjD,OAAO,CAAC,gBAAgB,CAAmB;gBAE/B,EAAE,EAAE,YAAY;IAItB,WAAW;IAIX,SAAS;IAIT,QAAQ,CAAC,MAAM,EAAE;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE;;;;;;;IAmBzD,SAAS,CAAC,MAAM,EAAE;QACtB,WAAW,EAAE,MAAM,CAAC;QACpB,OAAO,EAAE,MAAM,CAAC;QAChB,WAAW,EAAE,MAAM,CAAC;QACpB,QAAQ,EAAE,OAAO,CAAC;QAClB,gBAAgB,EAAE,MAAM,CAAC;KAC1B;CASF"}
@@ -0,0 +1,13 @@
1
+ import type { SchedulerClient, WorkflowEvent } from "@yieldstar/core";
2
+ import { SqliteTaskQueueClient } from "./sqlite-task-queue";
3
+ import { SqliteTimersClient } from "./sqlite-timers";
4
+ export declare class SqliteSchedulerClient implements SchedulerClient {
5
+ private taskQueue;
6
+ private timersClient;
7
+ constructor(params: {
8
+ taskQueueClient: SqliteTaskQueueClient;
9
+ timersClient: SqliteTimersClient;
10
+ });
11
+ requestWakeUp(event: WorkflowEvent, resumeIn?: number): Promise<void>;
12
+ }
13
+ //# sourceMappingURL=sqlite-scheduler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-scheduler.d.ts","sourceRoot":"","sources":["../src/sqlite-scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACtE,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAErD,qBAAa,qBAAsB,YAAW,eAAe;IAC3D,OAAO,CAAC,SAAS,CAAwB;IACzC,OAAO,CAAC,YAAY,CAAqB;gBAE7B,MAAM,EAAE;QAClB,eAAe,EAAE,qBAAqB,CAAC;QACvC,YAAY,EAAE,kBAAkB,CAAC;KAClC;IAKK,aAAa,CAAC,KAAK,EAAE,aAAa,EAAE,QAAQ,CAAC,EAAE,MAAM;CAO5D"}
@@ -0,0 +1,54 @@
1
+ import type { SchedulerClient } from "@yieldstar/core";
2
+ import type { SqliteDriver } from "./sqlite-driver";
3
+ import { CasStoreClient, type StandardSchemaV1, type StoreDefinition, type StoreDeleteFromResult, type StoreSnapshot, type StoreState, type StoreStepId, type StoreWaiter, type StoreMutation, type StoreMutationCommitResult } from "@yieldstar/core";
4
+ export declare class SqliteStoreClient extends CasStoreClient {
5
+ private db;
6
+ private schedulerClient;
7
+ private writeLock;
8
+ constructor(params: {
9
+ db: SqliteDriver;
10
+ schedulerClient: SchedulerClient;
11
+ });
12
+ private enqueueWrite;
13
+ getOrCreateStore<Schema extends StandardSchemaV1>(params: {
14
+ definition: StoreDefinition<Schema>;
15
+ id: string;
16
+ initial?: StoreState<Schema> | (() => StoreState<Schema> | Promise<StoreState<Schema>>);
17
+ }): Promise<StoreSnapshot<StoreState<Schema>>>;
18
+ getStore<Schema extends StandardSchemaV1>(params: {
19
+ definition: StoreDefinition<Schema>;
20
+ id: string;
21
+ }): Promise<StoreSnapshot<StoreState<Schema>>>;
22
+ protected getAppliedStoreStep(params: {
23
+ definition: StoreDefinition;
24
+ id: string;
25
+ stepId: StoreStepId;
26
+ }): Promise<{
27
+ result: unknown;
28
+ } | undefined>;
29
+ protected commitStoreMutation(mutation: StoreMutation): Promise<StoreMutationCommitResult>;
30
+ listStores<Schema extends StandardSchemaV1>(definition: StoreDefinition<Schema>): Promise<string[]>;
31
+ deleteStore<Schema extends StandardSchemaV1>(params: {
32
+ definition: StoreDefinition<Schema>;
33
+ id: string;
34
+ }): Promise<void>;
35
+ deleteStoreFrom(params: {
36
+ definition: StoreDefinition;
37
+ id: string;
38
+ snapshot: StoreSnapshot<unknown>;
39
+ stepId?: StoreStepId;
40
+ }): Promise<StoreDeleteFromResult>;
41
+ registerWaiter(waiter: StoreWaiter): Promise<void>;
42
+ private setupDb;
43
+ private createStoresTable;
44
+ private getAppliedStepRow;
45
+ private insertAppliedStepRow;
46
+ private getStoreRow;
47
+ private selectMatchingWaiters;
48
+ private deleteWaiterRow;
49
+ private insertWakeOutboxRow;
50
+ private drainWakeOutbox;
51
+ private drainWakeOutboxBestEffort;
52
+ private assertSnapshotHasInstanceId;
53
+ }
54
+ //# sourceMappingURL=sqlite-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-store.d.ts","sourceRoot":"","sources":["../src/sqlite-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAiB,MAAM,iBAAiB,CAAC;AAEtE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAEL,cAAc,EAGd,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,qBAAqB,EAE1B,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,yBAAyB,EAC/B,MAAM,iBAAiB,CAAC;AAoCzB,qBAAa,iBAAkB,SAAQ,cAAc;IACnD,OAAO,CAAC,EAAE,CAAe;IACzB,OAAO,CAAC,eAAe,CAAkB;IAEzC,OAAO,CAAC,SAAS,CAAuC;gBAE5C,MAAM,EAAE;QAAE,EAAE,EAAE,YAAY,CAAC;QAAC,eAAe,EAAE,eAAe,CAAA;KAAE;IAW1E,OAAO,CAAC,YAAY;IAMd,gBAAgB,CAAC,MAAM,SAAS,gBAAgB,EAAE,MAAM,EAAE;QAC9D,UAAU,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;QACpC,EAAE,EAAE,MAAM,CAAC;QACX,OAAO,CAAC,EACJ,UAAU,CAAC,MAAM,CAAC,GAClB,CAAC,MAAM,UAAU,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;KAC9D,GAAG,OAAO,CAAC,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;IAoExC,QAAQ,CAAC,MAAM,SAAS,gBAAgB,EAAE,MAAM,EAAE;QACtD,UAAU,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;QACpC,EAAE,EAAE,MAAM,CAAC;KACZ,GAAG,OAAO,CAAC,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;cAgB9B,mBAAmB,CAAC,MAAM,EAAE;QAC1C,UAAU,EAAE,eAAe,CAAC;QAC5B,EAAE,EAAE,MAAM,CAAC;QACX,MAAM,EAAE,WAAW,CAAC;KACrB,GAAG,OAAO,CAAC;QAAE,MAAM,EAAE,OAAO,CAAA;KAAE,GAAG,SAAS,CAAC;IAS5C,SAAS,CAAC,mBAAmB,CAC3B,QAAQ,EAAE,aAAa,GACtB,OAAO,CAAC,yBAAyB,CAAC;IA+F/B,UAAU,CAAC,MAAM,SAAS,gBAAgB,EAC9C,UAAU,EAAE,eAAe,CAAC,MAAM,CAAC,GAClC,OAAO,CAAC,MAAM,EAAE,CAAC;IAWd,WAAW,CAAC,MAAM,SAAS,gBAAgB,EAAE,MAAM,EAAE;QACzD,UAAU,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;QACpC,EAAE,EAAE,MAAM,CAAC;KACZ,GAAG,OAAO,CAAC,IAAI,CAAC;IA+BX,eAAe,CAAC,MAAM,EAAE;QAC5B,UAAU,EAAE,eAAe,CAAC;QAC5B,EAAE,EAAE,MAAM,CAAC;QACX,QAAQ,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;QACjC,MAAM,CAAC,EAAE,WAAW,CAAC;KACtB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IA8E5B,cAAc,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IA6DxD,OAAO,CAAC,OAAO;IAmCf,OAAO,CAAC,iBAAiB;IAazB,OAAO,CAAC,iBAAiB;IAqBzB,OAAO,CAAC,oBAAoB;IAsB5B,OAAO,CAAC,WAAW;IAYnB,OAAO,CAAC,qBAAqB;IAiC7B,OAAO,CAAC,eAAe;IAyBvB,OAAO,CAAC,mBAAmB;YAoBb,eAAe;YAyDf,yBAAyB;IASvC,OAAO,CAAC,2BAA2B;CAOpC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=sqlite-store.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-store.test.d.ts","sourceRoot":"","sources":["../src/sqlite-store.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,26 @@
1
+ import type { WorkflowEvent } from "@yieldstar/core";
2
+ import type { SqliteDriver } from "./sqlite-driver";
3
+ export declare class SqliteTaskQueue {
4
+ private taskQueueDao;
5
+ constructor(db: SqliteDriver);
6
+ add(event: WorkflowEvent): void;
7
+ process(): {
8
+ taskId: number;
9
+ visibilityTimeout: number;
10
+ event: {
11
+ workflowId: string;
12
+ executionId: string;
13
+ params: any;
14
+ context: Map<any, any>;
15
+ };
16
+ } | undefined;
17
+ remove(taskId: number): void;
18
+ makeVisible(taskId: number): void;
19
+ get isEmpty(): boolean;
20
+ }
21
+ export declare class SqliteTaskQueueClient {
22
+ private taskQueueDao;
23
+ constructor(db: SqliteDriver);
24
+ add(event: WorkflowEvent): void;
25
+ }
26
+ //# sourceMappingURL=sqlite-task-queue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-task-queue.d.ts","sourceRoot":"","sources":["../src/sqlite-task-queue.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAErD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAIpD,qBAAa,eAAe;IAC1B,OAAO,CAAC,YAAY,CAAe;gBAEvB,EAAE,EAAE,YAAY;IAK5B,GAAG,CAAC,KAAK,EAAE,aAAa;IAIxB,OAAO;;;;;;;;;;IAsBP,MAAM,CAAC,MAAM,EAAE,MAAM;IAIrB,WAAW,CAAC,MAAM,EAAE,MAAM;IAI1B,IAAI,OAAO,IAAI,OAAO,CAErB;CACF;AAED,qBAAa,qBAAqB;IAChC,OAAO,CAAC,YAAY,CAAe;gBAEvB,EAAE,EAAE,YAAY;IAI5B,GAAG,CAAC,KAAK,EAAE,aAAa;CAGzB"}
@@ -0,0 +1,18 @@
1
+ import { SqliteTaskQueue } from "./sqlite-task-queue";
2
+ import type { WorkflowEvent } from "@yieldstar/core";
3
+ import type { SqliteDriver } from "./sqlite-driver";
4
+ export declare class SqliteTimers {
5
+ private timersDao;
6
+ private taskQueue;
7
+ constructor(params: {
8
+ db: SqliteDriver;
9
+ taskQueue: SqliteTaskQueue;
10
+ });
11
+ processTimers(): void;
12
+ }
13
+ export declare class SqliteTimersClient {
14
+ private timersDao;
15
+ constructor(db: SqliteDriver);
16
+ createTimer(event: WorkflowEvent, delay: number): void;
17
+ }
18
+ //# sourceMappingURL=sqlite-timers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-timers.d.ts","sourceRoot":"","sources":["../src/sqlite-timers.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEpD,qBAAa,YAAY;IACvB,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,SAAS,CAAkB;gBAEvB,MAAM,EAAE;QAAE,EAAE,EAAE,YAAY,CAAC;QAAC,SAAS,EAAE,eAAe,CAAA;KAAE;IAKpE,aAAa;CAYd;AAED,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,SAAS,CAAY;gBAEjB,EAAE,EAAE,YAAY;IAI5B,WAAW,CAAC,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM;CAGhD"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@yieldstar/sqlite-runtime",
3
+ "version": "0.5.0",
4
+ "license": "MIT",
5
+ "author": "Daniel Grant",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/notationlabs/yieldstar.git",
9
+ "directory": "packages/sqlite-runtime"
10
+ },
11
+ "engines": {
12
+ "node": ">=22.6"
13
+ },
14
+ "type": "module",
15
+ "main": "dist/index.js",
16
+ "types": "dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js"
21
+ },
22
+ "./bun": {
23
+ "types": "./dist/bun.d.ts",
24
+ "import": "./dist/bun.js"
25
+ },
26
+ "./node": {
27
+ "types": "./dist/node.d.ts",
28
+ "import": "./dist/node.js"
29
+ }
30
+ },
31
+ "dependencies": {
32
+ "pino": "^9.9.0",
33
+ "uuid": "^11.1.0",
34
+ "@yieldstar/core": "0.5.0"
35
+ },
36
+ "devDependencies": {
37
+ "@types/bun": "^1.2.21",
38
+ "@types/node": "^22.18.0"
39
+ },
40
+ "scripts": {
41
+ "build": "bun build src/index.ts src/bun.ts src/node.ts --outdir dist --target node --packages=external --splitting"
42
+ }
43
+ }
package/src/bun.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { Database } from "bun:sqlite";
2
+ import {
3
+ createSqliteDriver,
4
+ wrapSqliteStatement,
5
+ type SqliteDbOpts,
6
+ } from "./sqlite-db";
7
+ import type { SqliteDriver, SqliteStatement } from "./sqlite-driver";
8
+
9
+ export * from "./index";
10
+
11
+ export class BunSqliteDriver implements SqliteDriver {
12
+ constructor(private readonly database: Database) {}
13
+
14
+ run(sql: string) {
15
+ this.database.run(sql);
16
+ }
17
+
18
+ query<Row>(sql: string): SqliteStatement<Row> {
19
+ return wrapSqliteStatement(this.database.query(sql));
20
+ }
21
+
22
+ close() {
23
+ this.database.close();
24
+ }
25
+ }
26
+
27
+ export function createSqliteDb(opts?: SqliteDbOpts) {
28
+ return createSqliteDriver(
29
+ (path) => new BunSqliteDriver(new Database(path, { create: true })),
30
+ opts
31
+ );
32
+ }
@@ -0,0 +1,80 @@
1
+ import type { SqliteDriver } from "../sqlite-driver";
2
+
3
+ class StepReponseRow {
4
+ execution_id!: string;
5
+ step_key!: string;
6
+ step_attempt!: number;
7
+ step_done!: 0 | 1;
8
+ step_response!: string;
9
+ }
10
+
11
+ export class StepResponsesDao {
12
+ private db: SqliteDriver;
13
+
14
+ constructor(db: SqliteDriver) {
15
+ this.db = db;
16
+ this.setupDb();
17
+ }
18
+
19
+ setupDb() {
20
+ this.db.run(`
21
+ CREATE TABLE IF NOT EXISTS step_responses (
22
+ execution_id TEXT NOT NULL,
23
+ step_key TEXT NOT NULL,
24
+ step_attempt INTEGER NOT NULL,
25
+ step_done BOOL NOT NULL,
26
+ step_response JSONB,
27
+ PRIMARY KEY (execution_id, step_key, step_attempt)
28
+ );
29
+ CREATE INDEX IF NOT EXISTS idx_execution_step_attempt
30
+ ON step_responses(execution_id, step_key, step_attempt DESC);
31
+ `);
32
+ }
33
+
34
+ getAllSteps() {
35
+ const query = this.db.query(`SELECT * FROM step_responses`);
36
+ return query.all();
37
+ }
38
+
39
+ deleteAll() {
40
+ const query = this.db.query(`DELETE FROM step_responses`);
41
+ query.run();
42
+ }
43
+
44
+ getLatestStepResponse(executionId: string, stepKey: string) {
45
+ const query = this.db.query<StepReponseRow>(
46
+ `SELECT * FROM step_responses
47
+ WHERE execution_id = $executionId
48
+ AND step_key = $stepKey
49
+ ORDER BY step_attempt DESC LIMIT 1`
50
+ );
51
+
52
+ const result = query.get({
53
+ $executionId: executionId,
54
+ $stepKey: stepKey,
55
+ });
56
+
57
+ return result;
58
+ }
59
+
60
+ insertStepResponse(
61
+ executionId: string,
62
+ stepKey: string,
63
+ stepAttempt: number,
64
+ stepDone: boolean,
65
+ stepResponseJson: string
66
+ ) {
67
+ const query = this.db.query(
68
+ `INSERT INTO step_responses (execution_id, step_key, step_attempt, step_done, step_response)
69
+ VALUES ($executionId, $stepKey, $stepAttempt, $stepDone, $stepResponse)`
70
+ );
71
+
72
+ query.run({
73
+ $executionId: executionId,
74
+ $stepKey: stepKey,
75
+ $stepAttempt: stepAttempt,
76
+ $stepDone: stepDone,
77
+ $stepResponse: stepResponseJson,
78
+ });
79
+ }
80
+ }
@@ -0,0 +1,79 @@
1
+ import type { WorkflowEvent } from "@yieldstar/core";
2
+ import type { SqliteDriver } from "../sqlite-driver";
3
+
4
+ class TaskRow {
5
+ task_id!: number;
6
+ workflow_id!: string;
7
+ execution_id!: string;
8
+ params?: string;
9
+ context?: string;
10
+ }
11
+
12
+ class CountRow {
13
+ count!: number;
14
+ }
15
+
16
+ export class TaskQueueDao {
17
+ private db: SqliteDriver;
18
+
19
+ constructor(db: SqliteDriver) {
20
+ this.db = db;
21
+ }
22
+
23
+ setupDb() {
24
+ this.db.run(`
25
+ CREATE TABLE IF NOT EXISTS task_queue (
26
+ task_id INTEGER PRIMARY KEY AUTOINCREMENT,
27
+ workflow_id TEXT NOT NULL,
28
+ execution_id TEXT NOT NULL,
29
+ params TEXT,
30
+ context TEXT,
31
+ visible_from INTEGER DEFAULT (strftime('%s', 'now'))
32
+ )
33
+ `);
34
+ }
35
+
36
+ insertTask(event: WorkflowEvent) {
37
+ const query = this.db.query(
38
+ `INSERT INTO task_queue (workflow_id, execution_id, params, context)
39
+ VALUES ($workflowId, $executionId, $params, $context)`
40
+ );
41
+
42
+ query.run({
43
+ $workflowId: event.workflowId,
44
+ $executionId: event.executionId,
45
+ $params: event.params ? JSON.stringify(event.params) : null,
46
+ $context: event.context
47
+ ? JSON.stringify(Array.from(event.context.entries()))
48
+ : null,
49
+ });
50
+ }
51
+
52
+ getNextTask() {
53
+ const query = this.db.query<TaskRow>(
54
+ `SELECT * FROM task_queue WHERE visible_from < strftime('%s', 'now') ORDER BY task_id LIMIT 1`
55
+ );
56
+ return query.get();
57
+ }
58
+
59
+ updateTaskVisibility(taskId: number, visibleFrom: number) {
60
+ const query = this.db.query(
61
+ "UPDATE task_queue SET visible_from = $visibleFrom WHERE task_id = $taskId"
62
+ );
63
+ query.run({ $taskId: taskId, $visibleFrom: visibleFrom });
64
+ }
65
+
66
+ deleteTaskById(id: number) {
67
+ const query = this.db.query(
68
+ `DELETE FROM task_queue WHERE task_id = $taskId`
69
+ );
70
+ query.run({ $taskId: id });
71
+ }
72
+
73
+ getTaskCount() {
74
+ const query = this.db.query<CountRow>(
75
+ `SELECT COUNT(*) as count FROM task_queue WHERE visible_from < strftime('%s', 'now')`
76
+ );
77
+ return query.get()!.count;
78
+ }
79
+ }