@swirls/looms 0.0.2 → 0.1.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 (256) hide show
  1. package/CHANGELOG.md +9 -2
  2. package/dist/actor/cell.d.ts +9 -4
  3. package/dist/actor/cell.js +7 -11
  4. package/dist/actor/host.d.ts +7 -3
  5. package/dist/actor/host.js +55 -32
  6. package/dist/actor/index.d.ts +1 -1
  7. package/dist/actor/index.js +1 -1
  8. package/dist/actor/routing.d.ts +12 -1
  9. package/dist/actor/routing.js +57 -51
  10. package/dist/agent/agent-session.d.ts +5 -0
  11. package/dist/agent/agent-session.js +270 -0
  12. package/dist/agent/definitions-store.d.ts +2 -3
  13. package/dist/agent/definitions-store.js +2 -4
  14. package/dist/agent/definitions.d.ts +46 -20
  15. package/dist/agent/definitions.js +36 -8
  16. package/dist/agent/effects.d.ts +359 -2
  17. package/dist/agent/effects.js +60 -34
  18. package/dist/agent/events.d.ts +175 -19
  19. package/dist/agent/events.js +45 -10
  20. package/dist/agent/index.d.ts +1 -0
  21. package/dist/agent/index.js +1 -0
  22. package/dist/agent/llm.d.ts +11 -2
  23. package/dist/agent/llm.js +13 -3
  24. package/dist/agent/module.d.ts +812 -78
  25. package/dist/agent/module.js +16 -4
  26. package/dist/agent/scope.d.ts +174 -18
  27. package/dist/agent/signals.d.ts +24 -1
  28. package/dist/agent/signals.js +77 -0
  29. package/dist/agent/threads.d.ts +3 -33
  30. package/dist/agent/threads.js +20 -25
  31. package/dist/agent/tool-schema.js +0 -1
  32. package/dist/agent/types.d.ts +49 -4
  33. package/dist/agent/types.js +5 -5
  34. package/dist/ai-vercel/index.d.ts +1 -0
  35. package/dist/ai-vercel/index.js +1 -0
  36. package/dist/ai-vercel/messages.js +22 -7
  37. package/dist/ai-vercel/vercel-evaluator.d.ts +23 -0
  38. package/dist/ai-vercel/vercel-evaluator.js +68 -0
  39. package/dist/ai-vercel/vercel-llm.js +64 -44
  40. package/dist/approval/effects.d.ts +89 -0
  41. package/dist/approval/module.d.ts +179 -29
  42. package/dist/approval/projections.d.ts +0 -14
  43. package/dist/approval/projections.js +20 -3
  44. package/dist/approval/signals.d.ts +2 -0
  45. package/dist/approval/signals.js +18 -2
  46. package/dist/cli/cli.js +2 -0
  47. package/dist/cli/commands.js +12 -16
  48. package/dist/client/client.d.ts +1195 -18
  49. package/dist/client/client.js +118 -120
  50. package/dist/client/index.d.ts +1 -1
  51. package/dist/client/index.js +1 -1
  52. package/dist/client/sse.d.ts +13 -0
  53. package/dist/client/sse.js +38 -44
  54. package/dist/cloudflare/alarm-scheduler.d.ts +12 -2
  55. package/dist/cloudflare/alarm-scheduler.js +34 -7
  56. package/dist/cloudflare/durable-object.d.ts +10 -2
  57. package/dist/cloudflare/durable-object.js +59 -28
  58. package/dist/cloudflare/event-store.d.ts +7 -2
  59. package/dist/cloudflare/event-store.js +8 -7
  60. package/dist/cloudflare/index.d.ts +2 -2
  61. package/dist/cloudflare/index.js +2 -2
  62. package/dist/cloudflare/router.js +13 -14
  63. package/dist/core/catalog.d.ts +3 -3
  64. package/dist/core/catalog.js +2 -5
  65. package/dist/core/coalesce.js +5 -3
  66. package/dist/core/compose.d.ts +2 -1
  67. package/dist/core/compose.js +18 -2
  68. package/dist/core/definition-store.d.ts +11 -0
  69. package/dist/core/definition-store.js +6 -0
  70. package/dist/core/effects.d.ts +127 -15
  71. package/dist/core/effects.js +89 -17
  72. package/dist/core/envelope.d.ts +27 -7
  73. package/dist/core/envelope.js +20 -6
  74. package/dist/core/event-codec.d.ts +4 -2
  75. package/dist/core/event-codec.js +10 -5
  76. package/dist/core/event-index.js +1 -3
  77. package/dist/core/fold.d.ts +3 -0
  78. package/dist/core/fold.js +171 -24
  79. package/dist/core/ids.d.ts +5 -0
  80. package/dist/core/ids.js +35 -2
  81. package/dist/core/index.d.ts +5 -1
  82. package/dist/core/index.js +5 -1
  83. package/dist/core/kind.d.ts +22 -0
  84. package/dist/core/kind.js +17 -0
  85. package/dist/core/limits.d.ts +34 -0
  86. package/dist/core/limits.js +46 -0
  87. package/dist/core/match.d.ts +1 -0
  88. package/dist/core/match.js +10 -1
  89. package/dist/core/module-scope.d.ts +22 -20
  90. package/dist/core/module-scope.js +38 -28
  91. package/dist/core/module.d.ts +16 -5
  92. package/dist/core/module.js +4 -0
  93. package/dist/core/projection.d.ts +13 -13
  94. package/dist/core/projection.js +9 -5
  95. package/dist/core/protocol.d.ts +68 -11
  96. package/dist/core/protocol.js +82 -11
  97. package/dist/core/replay.d.ts +312 -3
  98. package/dist/core/replay.js +11 -1
  99. package/dist/core/schema.d.ts +8 -19
  100. package/dist/core/schema.js +25 -80
  101. package/dist/core/serialize.js +2 -4
  102. package/dist/core/snapshot-store.d.ts +2 -1
  103. package/dist/core/snapshot-store.js +8 -2
  104. package/dist/core/snapshots.d.ts +5 -1
  105. package/dist/core/snapshots.js +10 -15
  106. package/dist/core/sqlite-store.js +56 -11
  107. package/dist/core/state.d.ts +142 -4
  108. package/dist/core/state.js +78 -0
  109. package/dist/core/store.d.ts +38 -3
  110. package/dist/core/store.js +77 -22
  111. package/dist/core/thread.d.ts +10 -9
  112. package/dist/core/types.d.ts +5 -8
  113. package/dist/core/types.js +28 -24
  114. package/dist/core/validate-event.js +14 -4
  115. package/dist/core/worker-callback.d.ts +17 -0
  116. package/dist/core/worker-callback.js +1 -0
  117. package/dist/debugger/components/event-stream.js +9 -10
  118. package/dist/debugger/events/protocol-summarize.js +26 -0
  119. package/dist/debugger/lib/event-meta.js +1 -2
  120. package/dist/evaluator/definitions-store.d.ts +11 -0
  121. package/dist/evaluator/definitions-store.js +8 -0
  122. package/dist/evaluator/definitions.d.ts +18 -0
  123. package/dist/evaluator/definitions.js +4 -0
  124. package/dist/evaluator/effects.d.ts +226 -0
  125. package/dist/evaluator/effects.js +132 -0
  126. package/dist/evaluator/evaluator.d.ts +24 -0
  127. package/dist/evaluator/evaluator.js +67 -0
  128. package/dist/evaluator/events.d.ts +59 -0
  129. package/dist/evaluator/events.js +21 -0
  130. package/dist/evaluator/index.d.ts +11 -0
  131. package/dist/evaluator/index.js +11 -0
  132. package/dist/evaluator/module.d.ts +606 -0
  133. package/dist/evaluator/module.js +25 -0
  134. package/dist/evaluator/projections.d.ts +50 -0
  135. package/dist/evaluator/projections.js +20 -0
  136. package/dist/evaluator/scope.d.ts +59 -0
  137. package/dist/evaluator/scope.js +7 -0
  138. package/dist/evaluator/signals.d.ts +3 -0
  139. package/dist/evaluator/signals.js +17 -0
  140. package/dist/evaluator/threads.d.ts +62 -0
  141. package/dist/evaluator/threads.js +117 -0
  142. package/dist/evaluator/types.d.ts +125 -0
  143. package/dist/evaluator/types.js +51 -0
  144. package/dist/projectors/delivery.d.ts +83 -0
  145. package/dist/projectors/delivery.js +182 -0
  146. package/dist/projectors/index-model.d.ts +1 -1
  147. package/dist/projectors/index-model.js +4 -6
  148. package/dist/projectors/index.d.ts +2 -0
  149. package/dist/projectors/index.js +2 -0
  150. package/dist/projectors/memory.d.ts +1 -1
  151. package/dist/projectors/memory.js +10 -8
  152. package/dist/projectors/postgres.js +44 -58
  153. package/dist/projectors/projector.d.ts +17 -0
  154. package/dist/projectors/projector.js +26 -1
  155. package/dist/projectors/sqlite-delivery.d.ts +4 -0
  156. package/dist/projectors/sqlite-delivery.js +147 -0
  157. package/dist/projectors/sqlite.js +23 -23
  158. package/dist/projectors/with-projectors.d.ts +7 -2
  159. package/dist/projectors/with-projectors.js +27 -28
  160. package/dist/react/notify.js +11 -8
  161. package/dist/react/react.d.ts +2 -3
  162. package/dist/react/react.js +14 -30
  163. package/dist/react/sse.d.ts +1 -1
  164. package/dist/react/sse.js +1 -1
  165. package/dist/react/store.d.ts +2 -2
  166. package/dist/react/store.js +109 -119
  167. package/dist/runtime/cancellation.d.ts +16 -0
  168. package/dist/runtime/cancellation.js +129 -0
  169. package/dist/runtime/deadline-transition-planner.d.ts +31 -0
  170. package/dist/runtime/deadline-transition-planner.js +139 -0
  171. package/dist/runtime/dispatch-effect.d.ts +3 -2
  172. package/dist/runtime/dispatch-effect.js +7 -13
  173. package/dist/runtime/errors.d.ts +51 -0
  174. package/dist/runtime/errors.js +39 -0
  175. package/dist/runtime/{runtime-helpers.d.ts → helpers.d.ts} +24 -4
  176. package/dist/runtime/{runtime-helpers.js → helpers.js} +56 -19
  177. package/dist/runtime/index.d.ts +3 -3
  178. package/dist/runtime/index.js +3 -3
  179. package/dist/runtime/ingress.d.ts +15 -0
  180. package/dist/runtime/ingress.js +42 -0
  181. package/dist/runtime/local-effect-executor.d.ts +21 -0
  182. package/dist/runtime/local-effect-executor.js +137 -0
  183. package/dist/runtime/looms.d.ts +22 -4
  184. package/dist/runtime/looms.js +54 -36
  185. package/dist/runtime/observer.d.ts +45 -0
  186. package/dist/runtime/observer.js +11 -0
  187. package/dist/runtime/repository.d.ts +55 -0
  188. package/dist/runtime/repository.js +278 -0
  189. package/dist/runtime/retry-policy.d.ts +16 -0
  190. package/dist/runtime/retry-policy.js +29 -0
  191. package/dist/runtime/run-execution-context.d.ts +30 -0
  192. package/dist/runtime/run-execution-context.js +56 -0
  193. package/dist/runtime/run-start.d.ts +4 -0
  194. package/dist/runtime/run-start.js +44 -0
  195. package/dist/runtime/runtime.d.ts +6 -87
  196. package/dist/runtime/runtime.js +233 -470
  197. package/dist/runtime/server/auth.d.ts +40 -0
  198. package/dist/runtime/server/auth.js +40 -0
  199. package/dist/runtime/server/fetch-handler.d.ts +14 -0
  200. package/dist/runtime/server/fetch-handler.js +51 -0
  201. package/dist/runtime/server/http.d.ts +37 -0
  202. package/dist/runtime/server/http.js +62 -0
  203. package/dist/runtime/server/route-table.d.ts +55 -0
  204. package/dist/runtime/server/route-table.js +74 -0
  205. package/dist/runtime/server/routes/effects.d.ts +3 -0
  206. package/dist/runtime/server/routes/effects.js +59 -0
  207. package/dist/runtime/server/routes/operations.d.ts +3 -0
  208. package/dist/runtime/server/routes/operations.js +14 -0
  209. package/dist/runtime/server/routes/runs.d.ts +3 -0
  210. package/dist/runtime/server/routes/runs.js +192 -0
  211. package/dist/runtime/server.d.ts +2 -8
  212. package/dist/runtime/server.js +4 -211
  213. package/dist/runtime/sse.d.ts +2 -1
  214. package/dist/runtime/sse.js +24 -17
  215. package/dist/runtime/types.d.ts +115 -0
  216. package/dist/runtime/types.js +1 -0
  217. package/dist/runtime/wake-engine.d.ts +20 -0
  218. package/dist/runtime/wake-engine.js +311 -0
  219. package/dist/runtime/wake-scheduler.d.ts +3 -4
  220. package/dist/runtime/wake-scheduler.js +25 -31
  221. package/dist/runtime/worker-callbacks.d.ts +16 -0
  222. package/dist/runtime/worker-callbacks.js +167 -0
  223. package/dist/runtime/worker-effect-dispatch.d.ts +36 -0
  224. package/dist/runtime/worker-effect-dispatch.js +126 -0
  225. package/dist/s2/config.d.ts +1 -1
  226. package/dist/s2/config.js +1 -1
  227. package/dist/s2/lite.js +70 -82
  228. package/dist/s2/pagination.d.ts +15 -0
  229. package/dist/s2/pagination.js +36 -0
  230. package/dist/s2/projector.js +6 -5
  231. package/dist/s2/snapshot-store.js +148 -163
  232. package/dist/s2/store.d.ts +3 -21
  233. package/dist/s2/store.js +71 -165
  234. package/dist/testing/index.d.ts +15 -0
  235. package/dist/testing/index.js +53 -16
  236. package/dist/workflow/definitions-store.d.ts +2 -3
  237. package/dist/workflow/definitions-store.js +2 -2
  238. package/dist/workflow/definitions.d.ts +78 -22
  239. package/dist/workflow/definitions.js +80 -14
  240. package/dist/workflow/effects.d.ts +453 -107
  241. package/dist/workflow/effects.js +214 -115
  242. package/dist/workflow/events.d.ts +149 -5
  243. package/dist/workflow/events.js +27 -5
  244. package/dist/workflow/graph.d.ts +56 -0
  245. package/dist/workflow/graph.js +207 -0
  246. package/dist/workflow/index.d.ts +1 -0
  247. package/dist/workflow/index.js +1 -0
  248. package/dist/workflow/module.d.ts +1047 -266
  249. package/dist/workflow/projections.d.ts +14 -12
  250. package/dist/workflow/projections.js +30 -8
  251. package/dist/workflow/scope.d.ts +146 -4
  252. package/dist/workflow/threads.d.ts +383 -155
  253. package/dist/workflow/threads.js +308 -110
  254. package/package.json +7 -2
  255. package/dist/runtime/events-api.d.ts +0 -4
  256. package/dist/runtime/events-api.js +0 -84
@@ -0,0 +1,18 @@
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import type { Schema } from 'effect';
3
+ import { type JsonValue } from '@swirls/looms/core';
4
+ import type { EvaluatorQuestions, EvaluatorRouteArgs, EvaluatorRouteDecision, EvaluatorSkipResult } from './types';
5
+ export interface EvaluatorDefinition<TName extends string = string, TInput = JsonValue> {
6
+ readonly kind: 'evaluator';
7
+ readonly name: TName;
8
+ readonly description?: string;
9
+ /** Provider model id. Omit it to use the adapter default. */
10
+ readonly model?: string;
11
+ readonly input?: StandardSchemaV1<any, TInput> | Schema.ConstraintDecoder<TInput>;
12
+ readonly questions: EvaluatorQuestions;
13
+ state?(this: void, input: TInput): string;
14
+ skipModel?(this: void, input: TInput): EvaluatorSkipResult | undefined;
15
+ route?(this: void, args: EvaluatorRouteArgs): EvaluatorRouteDecision;
16
+ }
17
+ export type AnyEvaluatorDefinition = EvaluatorDefinition;
18
+ export declare function defineEvaluator<TName extends string, TInput = JsonValue>(def: Omit<EvaluatorDefinition<TName, TInput>, 'kind'>): EvaluatorDefinition<TName, TInput>;
@@ -0,0 +1,4 @@
1
+ import { defineKind } from '@swirls/looms/core';
2
+ export function defineEvaluator(def) {
3
+ return defineKind('evaluator', def);
4
+ }
@@ -0,0 +1,226 @@
1
+ import { Schema } from 'effect';
2
+ import { EvaluatorDefinitionsTag } from './definitions-store';
3
+ import { EvaluatorTag } from './evaluator';
4
+ export declare const evaluateEffect: NoInfer<import("@swirls/looms/core").EffectDefinition<{
5
+ readonly evaluationId: string;
6
+ readonly definitionName?: string | undefined;
7
+ readonly input?: Schema.Json | undefined;
8
+ }, EvaluatorDefinitionsTag | EvaluatorTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
9
+ readonly type: "evaluator.evaluated";
10
+ readonly payload: {
11
+ readonly evaluationId: string;
12
+ readonly definitionName?: string | undefined;
13
+ readonly answers: {
14
+ readonly [x: string]: {
15
+ readonly type: "boolean";
16
+ readonly value: boolean;
17
+ readonly probability: number;
18
+ } | {
19
+ readonly type: "choice";
20
+ readonly value: string;
21
+ } | {
22
+ readonly type: "score";
23
+ readonly value: number;
24
+ };
25
+ };
26
+ readonly route: string;
27
+ readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
28
+ readonly usage?: {
29
+ readonly input: number;
30
+ readonly output: number;
31
+ } | undefined;
32
+ };
33
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
34
+ readonly type: "evaluator.requested";
35
+ readonly payload: {
36
+ readonly evaluationId: string;
37
+ readonly definitionName?: string | undefined;
38
+ readonly state: string;
39
+ readonly model?: string | undefined;
40
+ };
41
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
42
+ readonly type: "runtime.effect.ambiguous";
43
+ readonly payload: {
44
+ readonly effectId: string;
45
+ readonly attempt: number;
46
+ readonly error: string;
47
+ };
48
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
49
+ readonly type: "runtime.effect.attempt.started";
50
+ readonly payload: {
51
+ readonly effectId: string;
52
+ readonly attempt: number;
53
+ };
54
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
55
+ readonly type: "runtime.effect.cancel.requested";
56
+ readonly payload: {
57
+ readonly effectId: string;
58
+ readonly attempt: number;
59
+ readonly deadlineAt: number | null;
60
+ };
61
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
62
+ readonly type: "runtime.effect.cancelled";
63
+ readonly payload: {
64
+ readonly effectId: string;
65
+ readonly attempt: number;
66
+ };
67
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
68
+ readonly type: "runtime.effect.completed";
69
+ readonly payload: {
70
+ readonly effectId: string;
71
+ readonly attempt: number;
72
+ };
73
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
74
+ readonly type: "runtime.effect.dispatched";
75
+ readonly payload: {
76
+ readonly effectId: string;
77
+ readonly attempt: number;
78
+ readonly deadlineAt: number | null;
79
+ };
80
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
81
+ readonly type: "runtime.effect.failed";
82
+ readonly payload: {
83
+ readonly effectId: string;
84
+ readonly error: string;
85
+ };
86
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
87
+ readonly type: "runtime.effect.heartbeat";
88
+ readonly payload: {
89
+ readonly effectId: string;
90
+ readonly attempt: number;
91
+ readonly heartbeatAt: number;
92
+ readonly deadlineAt: number | null;
93
+ };
94
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
95
+ readonly type: "runtime.effect.queued";
96
+ readonly payload: {
97
+ readonly effectId: string;
98
+ readonly attempt: number;
99
+ readonly deadlineAt: number | null;
100
+ };
101
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
102
+ readonly type: "runtime.effect.retry.scheduled";
103
+ readonly payload: {
104
+ readonly effectId: string;
105
+ readonly attempt: number;
106
+ readonly nextAttemptAt: number;
107
+ readonly error: string;
108
+ };
109
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
110
+ readonly type: "runtime.effect.timed_out";
111
+ readonly payload: {
112
+ readonly effectId: string;
113
+ readonly attempt: number;
114
+ readonly timeout: string;
115
+ };
116
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
117
+ readonly type: "runtime.effect.worker.started";
118
+ readonly payload: {
119
+ readonly effectId: string;
120
+ readonly attempt: number;
121
+ readonly deadlineAt: number | null;
122
+ };
123
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
124
+ readonly type: "runtime.run.cancelled";
125
+ readonly payload: {
126
+ readonly reason?: string | undefined;
127
+ };
128
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
129
+ readonly type: "runtime.run.completed";
130
+ readonly payload: {
131
+ readonly output: Schema.Json;
132
+ readonly error: string | null;
133
+ };
134
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
135
+ readonly type: "runtime.run.started";
136
+ readonly payload: {
137
+ readonly rootThreadId?: string | undefined;
138
+ readonly kind: string;
139
+ readonly definitionName: string;
140
+ readonly definitionVersion: string;
141
+ readonly input: Schema.Json;
142
+ readonly requestedThreadId?: string | null | undefined;
143
+ };
144
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
145
+ readonly type: "runtime.signal.received";
146
+ readonly payload: {
147
+ readonly signalType: string;
148
+ };
149
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
150
+ readonly type: "runtime.snapshot.taken";
151
+ readonly payload: {
152
+ readonly seq: number;
153
+ readonly stateHash: string;
154
+ readonly state?: Schema.Json | undefined;
155
+ };
156
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
157
+ readonly type: "runtime.thread.cancel.requested";
158
+ readonly payload: {
159
+ readonly threadId: string;
160
+ };
161
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
162
+ readonly type: "runtime.thread.cancelled";
163
+ readonly payload: {
164
+ readonly threadId: string;
165
+ readonly reason?: string | undefined;
166
+ };
167
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
168
+ readonly type: "runtime.thread.completed";
169
+ readonly payload: {
170
+ readonly threadId: string;
171
+ readonly output: Schema.Json;
172
+ };
173
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
174
+ readonly type: "runtime.thread.failed";
175
+ readonly payload: {
176
+ readonly threadId: string;
177
+ readonly error: string;
178
+ };
179
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
180
+ readonly type: "runtime.thread.started";
181
+ readonly payload: {
182
+ readonly threadId: string;
183
+ readonly kind: string;
184
+ readonly definitionName: string;
185
+ readonly definitionVersion: string;
186
+ readonly input: Schema.Json;
187
+ readonly parentThreadId: string | null;
188
+ };
189
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
190
+ readonly type: "runtime.timer.fired";
191
+ readonly payload: {
192
+ readonly timerId: string;
193
+ readonly waitId: string;
194
+ };
195
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
196
+ readonly type: "runtime.timer.set";
197
+ readonly payload: {
198
+ readonly timerId: string;
199
+ readonly waitId: string;
200
+ readonly wakeAt: number;
201
+ };
202
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
203
+ readonly type: "runtime.wait.registered";
204
+ readonly payload: {
205
+ readonly waitId: string;
206
+ readonly threadId?: string | undefined;
207
+ readonly on: {
208
+ readonly type: string | readonly string[];
209
+ readonly match?: Schema.Json | undefined;
210
+ } | {
211
+ readonly timerAt: number;
212
+ };
213
+ readonly tag?: Schema.Json | undefined;
214
+ };
215
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
216
+ readonly type: "runtime.wait.satisfied";
217
+ readonly payload: {
218
+ readonly waitId: string;
219
+ readonly tag?: Schema.Json | undefined;
220
+ readonly event: {
221
+ readonly id: string;
222
+ readonly type: string;
223
+ readonly payload: Schema.Json;
224
+ };
225
+ };
226
+ })>>;
@@ -0,0 +1,132 @@
1
+ import { Effect, Schema } from 'effect';
2
+ import { stringifyJson, validateInputEffect } from '@swirls/looms/core';
3
+ import { EvaluatorDefinitionsTag } from './definitions-store.js';
4
+ import { EvaluatorTag } from './evaluator.js';
5
+ import { evaluatorModule } from './scope.js';
6
+ const EvaluateInput = Schema.Struct({
7
+ evaluationId: Schema.String,
8
+ definitionName: Schema.optional(Schema.String),
9
+ input: Schema.optional(Schema.Json),
10
+ });
11
+ function errorMessage(cause) {
12
+ return cause instanceof Error ? cause.message : String(cause);
13
+ }
14
+ function capture(run) {
15
+ return Effect.try({
16
+ try: run,
17
+ catch: errorMessage,
18
+ }).pipe(Effect.match({
19
+ onFailure: (error) => ({ ok: false, error }),
20
+ onSuccess: (value) => ({ ok: true, value }),
21
+ }));
22
+ }
23
+ function failed(threadId, error) {
24
+ return [
25
+ {
26
+ type: 'runtime.thread.failed',
27
+ payload: { threadId, error },
28
+ threadId,
29
+ },
30
+ ];
31
+ }
32
+ export const evaluateEffect = evaluatorModule.effect({
33
+ type: 'evaluator.evaluate',
34
+ input: EvaluateInput,
35
+ execute: (input, ctx) => Effect.gen(function* () {
36
+ const threadId = ctx.threadId;
37
+ const definitions = yield* EvaluatorDefinitionsTag;
38
+ const evaluator = yield* EvaluatorTag;
39
+ const definition = input.definitionName ? definitions.get(input.definitionName) : undefined;
40
+ if (!definition) {
41
+ const name = input.definitionName ?? 'unknown';
42
+ return failed(threadId, `Unknown evaluator definition for ${name}`);
43
+ }
44
+ const rawInput = input.input ?? null;
45
+ const validated = definition.input
46
+ ? yield* validateInputEffect(definition.input, rawInput).pipe(Effect.match({
47
+ onFailure: (error) => ({ ok: false, error: error.message }),
48
+ onSuccess: (value) => ({ ok: true, value }),
49
+ }))
50
+ : { ok: true, value: rawInput };
51
+ if (!validated.ok) {
52
+ return failed(threadId, validated.error);
53
+ }
54
+ const subject = validated.value;
55
+ if (Object.keys(definition.questions).length === 0) {
56
+ return failed(threadId, 'Evaluator is missing questions');
57
+ }
58
+ const renderState = definition.state;
59
+ const stateText = renderState
60
+ ? yield* capture(() => renderState(subject))
61
+ : { ok: true, value: stringifyJson(subject) };
62
+ if (!stateText.ok) {
63
+ return failed(threadId, stateText.error);
64
+ }
65
+ const events = [
66
+ {
67
+ type: 'evaluator.requested',
68
+ payload: {
69
+ evaluationId: input.evaluationId,
70
+ definitionName: input.definitionName,
71
+ state: stateText.value,
72
+ model: definition.model,
73
+ },
74
+ threadId,
75
+ },
76
+ ];
77
+ const skipModel = definition.skipModel;
78
+ const skipped = skipModel
79
+ ? yield* capture(() => skipModel(subject))
80
+ : { ok: true, value: undefined };
81
+ if (!skipped.ok) {
82
+ return failed(threadId, skipped.error);
83
+ }
84
+ let answers = {};
85
+ let usage;
86
+ let decision = { route: 'continue', reason: 'unavailable' };
87
+ if (skipped.value) {
88
+ answers = skipped.value.answers ?? {};
89
+ decision = { route: skipped.value.route, reason: skipped.value.reason };
90
+ }
91
+ else {
92
+ const evaluated = yield* evaluator
93
+ .evaluate({
94
+ model: definition.model,
95
+ state: stateText.value,
96
+ questions: definition.questions,
97
+ })
98
+ .pipe(Effect.match({
99
+ onFailure: () => ({ ok: false }),
100
+ onSuccess: (value) => ({ ok: true, value }),
101
+ }));
102
+ if (evaluated.ok) {
103
+ answers = evaluated.value.answers;
104
+ usage = evaluated.value.usage;
105
+ const route = definition.route;
106
+ if (route) {
107
+ const routed = yield* capture(() => route({ answers, input: subject }));
108
+ if (!routed.ok) {
109
+ return failed(threadId, routed.error);
110
+ }
111
+ decision = { route: routed.value.route, reason: routed.value.reason ?? 'model' };
112
+ }
113
+ else {
114
+ decision = { route: 'continue', reason: 'model' };
115
+ }
116
+ }
117
+ }
118
+ events.push({
119
+ type: 'evaluator.evaluated',
120
+ payload: {
121
+ evaluationId: input.evaluationId,
122
+ definitionName: input.definitionName,
123
+ answers,
124
+ route: decision.route,
125
+ reason: decision.reason,
126
+ usage,
127
+ },
128
+ threadId,
129
+ });
130
+ return events;
131
+ }),
132
+ });
@@ -0,0 +1,24 @@
1
+ import { Context, Effect, Layer } from 'effect';
2
+ import type { EvaluatorAdapter } from './types';
3
+ declare const EvaluatorError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
4
+ readonly _tag: "EvaluatorError";
5
+ } & Readonly<A>;
6
+ export declare class EvaluatorError extends EvaluatorError_base<{
7
+ readonly cause: unknown;
8
+ readonly message: string;
9
+ }> {
10
+ constructor(cause: unknown);
11
+ }
12
+ export interface EvaluatorService {
13
+ readonly evaluate: (args: Parameters<EvaluatorAdapter['evaluate']>[0]) => Effect.Effect<Awaited<ReturnType<EvaluatorAdapter['evaluate']>>, EvaluatorError>;
14
+ }
15
+ export declare function evaluatorFromAdapter(adapter: EvaluatorAdapter): EvaluatorService;
16
+ declare const EvaluatorTag_base: Context.ServiceClass<EvaluatorTag, "looms/Evaluator", EvaluatorService>;
17
+ export declare class EvaluatorTag extends EvaluatorTag_base {
18
+ }
19
+ export interface StubEvaluatorPolicy {
20
+ evaluate?: EvaluatorAdapter['evaluate'];
21
+ }
22
+ export declare const makeStubEvaluator: (policy?: StubEvaluatorPolicy) => EvaluatorService;
23
+ export declare const StubEvaluatorLive: (policy?: StubEvaluatorPolicy) => Layer.Layer<EvaluatorTag, never, never>;
24
+ export {};
@@ -0,0 +1,67 @@
1
+ import { Context, Data, Effect, Layer } from 'effect';
2
+ export class EvaluatorError extends Data.TaggedError('EvaluatorError') {
3
+ constructor(cause) {
4
+ super({
5
+ cause,
6
+ message: cause instanceof Error ? cause.message : String(cause),
7
+ });
8
+ this.name = 'EvaluatorError';
9
+ }
10
+ }
11
+ export function evaluatorFromAdapter(adapter) {
12
+ return {
13
+ evaluate: (args) => Effect.tryPromise({
14
+ try: () => adapter.evaluate(args),
15
+ catch: (cause) => new EvaluatorError(cause),
16
+ }),
17
+ };
18
+ }
19
+ export class EvaluatorTag extends Context.Service()('looms/Evaluator') {
20
+ }
21
+ function defaultStubEvaluate(args) {
22
+ const text = args.state.toLowerCase();
23
+ const high = /\b(error|fail|fatal|dispute|urgent)\b/.test(text);
24
+ const low = /\b(routine|resolved|duplicate)\b/.test(text);
25
+ const answers = Object.fromEntries(Object.entries(args.questions).map(([name, question]) => {
26
+ if (question.type === 'boolean') {
27
+ return [
28
+ name,
29
+ {
30
+ type: 'boolean',
31
+ value: high,
32
+ probability: high ? 0.95 : low ? 0.02 : 0.4,
33
+ },
34
+ ];
35
+ }
36
+ if (question.type === 'choice') {
37
+ const allowed = Object.keys(question.criteria);
38
+ const preferred = high ? ['critical', 'high'] : low ? ['low'] : ['normal'];
39
+ const value = preferred.find((item) => allowed.includes(item)) ?? allowed[0] ?? 'low';
40
+ return [
41
+ name,
42
+ {
43
+ type: 'choice',
44
+ value,
45
+ },
46
+ ];
47
+ }
48
+ return [
49
+ name,
50
+ {
51
+ type: 'score',
52
+ value: high ? 4 : low ? 0 : 2,
53
+ },
54
+ ];
55
+ }));
56
+ return {
57
+ answers,
58
+ usage: { input: 0, output: 0 },
59
+ };
60
+ }
61
+ export const makeStubEvaluator = (policy = {}) => ({
62
+ evaluate: (args) => Effect.tryPromise({
63
+ try: () => Promise.resolve((policy.evaluate ?? defaultStubEvaluate)(args)),
64
+ catch: (cause) => new EvaluatorError(cause),
65
+ }),
66
+ });
67
+ export const StubEvaluatorLive = (policy) => Layer.succeed(EvaluatorTag, makeStubEvaluator(policy));
@@ -0,0 +1,59 @@
1
+ import { Schema } from 'effect';
2
+ export declare const EvaluatorRequestedPayloadSchema: Schema.Struct<{
3
+ readonly evaluationId: Schema.String;
4
+ readonly definitionName: Schema.optional<Schema.String>;
5
+ readonly state: Schema.String;
6
+ readonly model: Schema.optional<Schema.String>;
7
+ }>;
8
+ export type EvaluatorRequested = Schema.Schema.Type<typeof EvaluatorRequestedPayloadSchema>;
9
+ export declare const EvaluatorEvaluatedPayloadSchema: Schema.Struct<{
10
+ readonly evaluationId: Schema.String;
11
+ readonly definitionName: Schema.optional<Schema.String>;
12
+ readonly answers: Schema.$Record<Schema.String, Schema.Union<readonly [Schema.Struct<{
13
+ readonly type: Schema.Literal<"boolean">;
14
+ readonly value: Schema.Boolean;
15
+ readonly probability: Schema.Finite;
16
+ }>, Schema.Struct<{
17
+ readonly type: Schema.Literal<"choice">;
18
+ readonly value: Schema.String;
19
+ }>, Schema.Struct<{
20
+ readonly type: Schema.Literal<"score">;
21
+ readonly value: Schema.Finite;
22
+ }>]>>;
23
+ readonly route: Schema.String;
24
+ readonly reason: Schema.Union<readonly [Schema.Literal<"model">, Schema.Literal<"uncertain">, Schema.Literal<"skipped">, Schema.Literal<"unavailable">]>;
25
+ readonly usage: Schema.optional<Schema.Struct<{
26
+ readonly input: Schema.Finite;
27
+ readonly output: Schema.Finite;
28
+ }>>;
29
+ }>;
30
+ export type EvaluatorEvaluated = Schema.Schema.Type<typeof EvaluatorEvaluatedPayloadSchema>;
31
+ export declare const evaluatorCatalog: import("@swirls/looms/core").EventCatalog<"evaluator", {
32
+ requested: Schema.Struct<{
33
+ readonly evaluationId: Schema.String;
34
+ readonly definitionName: Schema.optional<Schema.String>;
35
+ readonly state: Schema.String;
36
+ readonly model: Schema.optional<Schema.String>;
37
+ }>;
38
+ evaluated: Schema.Struct<{
39
+ readonly evaluationId: Schema.String;
40
+ readonly definitionName: Schema.optional<Schema.String>;
41
+ readonly answers: Schema.$Record<Schema.String, Schema.Union<readonly [Schema.Struct<{
42
+ readonly type: Schema.Literal<"boolean">;
43
+ readonly value: Schema.Boolean;
44
+ readonly probability: Schema.Finite;
45
+ }>, Schema.Struct<{
46
+ readonly type: Schema.Literal<"choice">;
47
+ readonly value: Schema.String;
48
+ }>, Schema.Struct<{
49
+ readonly type: Schema.Literal<"score">;
50
+ readonly value: Schema.Finite;
51
+ }>]>>;
52
+ readonly route: Schema.String;
53
+ readonly reason: Schema.Union<readonly [Schema.Literal<"model">, Schema.Literal<"uncertain">, Schema.Literal<"skipped">, Schema.Literal<"unavailable">]>;
54
+ readonly usage: Schema.optional<Schema.Struct<{
55
+ readonly input: Schema.Finite;
56
+ readonly output: Schema.Finite;
57
+ }>>;
58
+ }>;
59
+ }>;
@@ -0,0 +1,21 @@
1
+ import { Schema } from 'effect';
2
+ import { defineEventCatalog } from '@swirls/looms/core';
3
+ import { EvaluatorAnswersSchema, EvaluatorReasonSchema, EvaluatorRouteSchema, EvaluatorUsageSchema, } from './types.js';
4
+ export const EvaluatorRequestedPayloadSchema = Schema.Struct({
5
+ evaluationId: Schema.String,
6
+ definitionName: Schema.optional(Schema.String),
7
+ state: Schema.String,
8
+ model: Schema.optional(Schema.String),
9
+ });
10
+ export const EvaluatorEvaluatedPayloadSchema = Schema.Struct({
11
+ evaluationId: Schema.String,
12
+ definitionName: Schema.optional(Schema.String),
13
+ answers: EvaluatorAnswersSchema,
14
+ route: EvaluatorRouteSchema,
15
+ reason: EvaluatorReasonSchema,
16
+ usage: Schema.optional(EvaluatorUsageSchema),
17
+ });
18
+ export const evaluatorCatalog = defineEventCatalog('evaluator', {
19
+ requested: EvaluatorRequestedPayloadSchema,
20
+ evaluated: EvaluatorEvaluatedPayloadSchema,
21
+ });
@@ -0,0 +1,11 @@
1
+ export * from './types';
2
+ export * from './definitions';
3
+ export * from './definitions-store';
4
+ export * from './events';
5
+ export * from './scope';
6
+ export * from './evaluator';
7
+ export * from './effects';
8
+ export * from './threads';
9
+ export * from './projections';
10
+ export * from './signals';
11
+ export * from './module';
@@ -0,0 +1,11 @@
1
+ export * from './types.js';
2
+ export * from './definitions.js';
3
+ export * from './definitions-store.js';
4
+ export * from './events.js';
5
+ export * from './scope.js';
6
+ export * from './evaluator.js';
7
+ export * from './effects.js';
8
+ export * from './threads.js';
9
+ export * from './projections.js';
10
+ export * from './signals.js';
11
+ export * from './module.js';