liminal 0.17.0 → 0.17.2

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 (288) hide show
  1. package/Accumulator.ts +103 -0
  2. package/Actor.ts +124 -0
  3. package/Audition.ts +110 -0
  4. package/CHANGELOG.md +4 -208
  5. package/Client.ts +525 -0
  6. package/ClientHandle.ts +50 -0
  7. package/F.ts +12 -0
  8. package/LICENSE +202 -0
  9. package/Method.ts +37 -0
  10. package/Protocol.ts +120 -0
  11. package/Send.ts +12 -0
  12. package/_constants.ts +1 -0
  13. package/_util/Diagnostic.ts +16 -0
  14. package/_util/Mutex.ts +13 -0
  15. package/_util/phantom.ts +1 -0
  16. package/dist/Accumulator.d.ts +22 -0
  17. package/dist/Accumulator.js +37 -0
  18. package/dist/Accumulator.js.map +1 -0
  19. package/dist/Actor.d.ts +29 -0
  20. package/dist/Actor.js +22 -0
  21. package/dist/Actor.js.map +1 -0
  22. package/dist/Audition.d.ts +17 -0
  23. package/dist/Audition.js +29 -0
  24. package/dist/Audition.js.map +1 -0
  25. package/dist/Client.d.ts +46 -0
  26. package/dist/Client.js +228 -0
  27. package/dist/Client.js.map +1 -0
  28. package/dist/ClientHandle.d.ts +17 -0
  29. package/dist/ClientHandle.js +10 -0
  30. package/dist/ClientHandle.js.map +1 -0
  31. package/dist/F.d.ts +4 -0
  32. package/dist/F.js +2 -0
  33. package/dist/F.js.map +1 -0
  34. package/dist/Method.d.ts +22 -0
  35. package/dist/Method.js +4 -0
  36. package/dist/Method.js.map +1 -0
  37. package/dist/Protocol.d.ts +53 -0
  38. package/dist/Protocol.js +37 -0
  39. package/dist/Protocol.js.map +1 -0
  40. package/dist/Send.d.ts +2 -0
  41. package/dist/Send.js +2 -0
  42. package/dist/Send.js.map +1 -0
  43. package/dist/_constants.d.ts +1 -0
  44. package/dist/_constants.js +2 -0
  45. package/dist/_constants.js.map +1 -0
  46. package/dist/_util/Diagnostic.d.ts +5 -0
  47. package/dist/_util/Diagnostic.js +10 -0
  48. package/dist/_util/Diagnostic.js.map +1 -0
  49. package/dist/_util/Mutex.d.ts +7 -0
  50. package/dist/_util/Mutex.js +9 -0
  51. package/dist/_util/Mutex.js.map +1 -0
  52. package/dist/_util/phantom.d.ts +3 -0
  53. package/dist/_util/phantom.js +2 -0
  54. package/dist/_util/phantom.js.map +1 -0
  55. package/dist/errors.d.ts +23 -0
  56. package/dist/errors.js +15 -0
  57. package/dist/errors.js.map +1 -0
  58. package/dist/experimental/BranchLive.d.ts +3 -0
  59. package/dist/experimental/BranchLive.js +5 -0
  60. package/dist/experimental/BranchLive.js.map +1 -0
  61. package/dist/experimental/L/L.d.ts +11 -0
  62. package/dist/experimental/L/L.js +12 -0
  63. package/dist/experimental/L/L.js.map +1 -0
  64. package/dist/experimental/L/append.d.ts +3 -0
  65. package/dist/experimental/L/append.js +4 -0
  66. package/dist/experimental/L/append.js.map +1 -0
  67. package/dist/experimental/L/assistant.d.ts +3 -0
  68. package/dist/experimental/L/assistant.js +13 -0
  69. package/dist/experimental/L/assistant.js.map +1 -0
  70. package/dist/experimental/L/assistantSchema.d.ts +3 -0
  71. package/dist/experimental/L/assistantSchema.js +13 -0
  72. package/dist/experimental/L/assistantSchema.js.map +1 -0
  73. package/dist/experimental/L/assistantStream.d.ts +3 -0
  74. package/dist/experimental/L/assistantStream.js +5 -0
  75. package/dist/experimental/L/assistantStream.js.map +1 -0
  76. package/dist/experimental/L/branch.d.ts +2 -0
  77. package/dist/experimental/L/branch.js +4 -0
  78. package/dist/experimental/L/branch.js.map +1 -0
  79. package/dist/experimental/L/clear.d.ts +3 -0
  80. package/dist/experimental/L/clear.js +7 -0
  81. package/dist/experimental/L/clear.js.map +1 -0
  82. package/dist/experimental/L/history.d.ts +3 -0
  83. package/dist/experimental/L/history.js +4 -0
  84. package/dist/experimental/L/history.js.map +1 -0
  85. package/dist/experimental/L/init.d.ts +3 -0
  86. package/dist/experimental/L/init.js +4 -0
  87. package/dist/experimental/L/init.js.map +1 -0
  88. package/dist/experimental/L/matrix.d.ts +10 -0
  89. package/dist/experimental/L/matrix.js +6 -0
  90. package/dist/experimental/L/matrix.js.map +1 -0
  91. package/dist/experimental/L/system.d.ts +1 -0
  92. package/dist/experimental/L/system.js +4 -0
  93. package/dist/experimental/L/system.js.map +1 -0
  94. package/dist/experimental/L/user.d.ts +1 -0
  95. package/dist/experimental/L/user.js +6 -0
  96. package/dist/experimental/L/user.js.map +1 -0
  97. package/dist/experimental/Loader.d.ts +16 -0
  98. package/dist/experimental/Loader.js +20 -0
  99. package/dist/experimental/Loader.js.map +1 -0
  100. package/dist/experimental/TaggedTemplateFunction.d.ts +11 -0
  101. package/dist/experimental/TaggedTemplateFunction.js +14 -0
  102. package/dist/experimental/TaggedTemplateFunction.js.map +1 -0
  103. package/dist/experimental/Template.d.ts +28 -0
  104. package/dist/experimental/Template.js +60 -0
  105. package/dist/experimental/Template.js.map +1 -0
  106. package/dist/experimental/index.d.ts +4 -0
  107. package/dist/experimental/index.js +5 -0
  108. package/dist/experimental/index.js.map +1 -0
  109. package/dist/index.d.ts +9 -7
  110. package/dist/index.js +9 -7
  111. package/dist/index.js.map +1 -1
  112. package/dist/tsconfig.tsbuildinfo +1 -1
  113. package/errors.ts +22 -0
  114. package/experimental/BranchLive.ts +6 -0
  115. package/experimental/L/L.ts +11 -0
  116. package/experimental/L/append.ts +12 -0
  117. package/experimental/L/assistant.ts +16 -0
  118. package/experimental/L/assistantSchema.ts +18 -0
  119. package/experimental/L/assistantStream.ts +9 -0
  120. package/experimental/L/branch.ts +5 -0
  121. package/experimental/L/clear.ts +7 -0
  122. package/experimental/L/history.ts +4 -0
  123. package/experimental/L/init.ts +4 -0
  124. package/experimental/L/matrix.ts +26 -0
  125. package/experimental/L/system.ts +5 -0
  126. package/experimental/L/user.ts +10 -0
  127. package/experimental/Loader.ts +35 -0
  128. package/experimental/TaggedTemplateFunction.ts +44 -0
  129. package/experimental/Template.ts +85 -0
  130. package/experimental/index.ts +4 -0
  131. package/index.ts +9 -7
  132. package/package.json +17 -30
  133. package/tsconfig.json +9 -0
  134. package/Digest.ts +0 -30
  135. package/Envelope.ts +0 -16
  136. package/F/F.ts +0 -1
  137. package/L/L.ts +0 -20
  138. package/L/append.ts +0 -14
  139. package/L/assistant.ts +0 -30
  140. package/L/assistantSchema.ts +0 -61
  141. package/L/assistantStream.ts +0 -22
  142. package/L/branch.ts +0 -20
  143. package/L/clear.ts +0 -14
  144. package/L/disable.ts +0 -13
  145. package/L/enable.ts +0 -24
  146. package/L/events.ts +0 -11
  147. package/L/json.ts +0 -12
  148. package/L/line.ts +0 -22
  149. package/L/listen.ts +0 -26
  150. package/L/messages.ts +0 -10
  151. package/L/prev.ts +0 -4
  152. package/L/provide.ts +0 -10
  153. package/L/self.ts +0 -5
  154. package/L/send.ts +0 -56
  155. package/L/system.ts +0 -25
  156. package/L/thread.ts +0 -16
  157. package/L/toolkit.ts +0 -14
  158. package/L/user.test.ts +0 -33
  159. package/L/user.ts +0 -32
  160. package/LEvent.ts +0 -33
  161. package/Thread.ts +0 -67
  162. package/dist/Digest.d.ts +0 -17
  163. package/dist/Digest.js +0 -7
  164. package/dist/Digest.js.map +0 -1
  165. package/dist/Envelope.d.ts +0 -13
  166. package/dist/Envelope.js +0 -2
  167. package/dist/Envelope.js.map +0 -1
  168. package/dist/F/F.d.ts +0 -1
  169. package/dist/F/F.js +0 -2
  170. package/dist/F/F.js.map +0 -1
  171. package/dist/L/L.d.ts +0 -20
  172. package/dist/L/L.js +0 -21
  173. package/dist/L/L.js.map +0 -1
  174. package/dist/L/append.d.ts +0 -5
  175. package/dist/L/append.js +0 -10
  176. package/dist/L/append.js.map +0 -1
  177. package/dist/L/assistant.d.ts +0 -6
  178. package/dist/L/assistant.js +0 -26
  179. package/dist/L/assistant.js.map +0 -1
  180. package/dist/L/assistantSchema.d.ts +0 -13
  181. package/dist/L/assistantSchema.js +0 -44
  182. package/dist/L/assistantSchema.js.map +0 -1
  183. package/dist/L/assistantStream.d.ts +0 -7
  184. package/dist/L/assistantStream.js +0 -17
  185. package/dist/L/assistantStream.js.map +0 -1
  186. package/dist/L/branch.d.ts +0 -3
  187. package/dist/L/branch.js +0 -19
  188. package/dist/L/branch.js.map +0 -1
  189. package/dist/L/clear.d.ts +0 -5
  190. package/dist/L/clear.js +0 -12
  191. package/dist/L/clear.js.map +0 -1
  192. package/dist/L/disable.d.ts +0 -4
  193. package/dist/L/disable.js +0 -10
  194. package/dist/L/disable.js.map +0 -1
  195. package/dist/L/enable.d.ts +0 -5
  196. package/dist/L/enable.js +0 -13
  197. package/dist/L/enable.js.map +0 -1
  198. package/dist/L/events.d.ts +0 -5
  199. package/dist/L/events.js +0 -6
  200. package/dist/L/events.js.map +0 -1
  201. package/dist/L/json.d.ts +0 -5
  202. package/dist/L/json.js +0 -8
  203. package/dist/L/json.js.map +0 -1
  204. package/dist/L/line.d.ts +0 -5
  205. package/dist/L/line.js +0 -11
  206. package/dist/L/line.js.map +0 -1
  207. package/dist/L/listen.d.ts +0 -7
  208. package/dist/L/listen.js +0 -14
  209. package/dist/L/listen.js.map +0 -1
  210. package/dist/L/messages.d.ts +0 -5
  211. package/dist/L/messages.js +0 -5
  212. package/dist/L/messages.js.map +0 -1
  213. package/dist/L/prev.d.ts +0 -5
  214. package/dist/L/prev.js +0 -5
  215. package/dist/L/prev.js.map +0 -1
  216. package/dist/L/provide.d.ts +0 -3
  217. package/dist/L/provide.js +0 -4
  218. package/dist/L/provide.js.map +0 -1
  219. package/dist/L/self.d.ts +0 -3
  220. package/dist/L/self.js +0 -4
  221. package/dist/L/self.js.map +0 -1
  222. package/dist/L/send.d.ts +0 -6
  223. package/dist/L/send.js +0 -34
  224. package/dist/L/send.js.map +0 -1
  225. package/dist/L/system.d.ts +0 -5
  226. package/dist/L/system.js +0 -14
  227. package/dist/L/system.js.map +0 -1
  228. package/dist/L/thread.d.ts +0 -3
  229. package/dist/L/thread.js +0 -15
  230. package/dist/L/thread.js.map +0 -1
  231. package/dist/L/toolkit.d.ts +0 -4
  232. package/dist/L/toolkit.js +0 -9
  233. package/dist/L/toolkit.js.map +0 -1
  234. package/dist/L/user.d.ts +0 -6
  235. package/dist/L/user.js +0 -13
  236. package/dist/L/user.js.map +0 -1
  237. package/dist/L/user.test.d.ts +0 -1
  238. package/dist/L/user.test.js.map +0 -1
  239. package/dist/LEvent.d.ts +0 -154
  240. package/dist/LEvent.js +0 -19
  241. package/dist/LEvent.js.map +0 -1
  242. package/dist/Thread.d.ts +0 -52
  243. package/dist/Thread.js +0 -32
  244. package/dist/Thread.js.map +0 -1
  245. package/dist/patterns/Debate.d.ts +0 -1
  246. package/dist/patterns/Debate.js +0 -3
  247. package/dist/patterns/Debate.js.map +0 -1
  248. package/dist/patterns/Model.d.ts +0 -6
  249. package/dist/patterns/Model.js +0 -14
  250. package/dist/patterns/Model.js.map +0 -1
  251. package/dist/patterns/Route.d.ts +0 -3
  252. package/dist/patterns/Route.js +0 -14
  253. package/dist/patterns/Route.js.map +0 -1
  254. package/dist/util/JsonValue.d.ts +0 -8
  255. package/dist/util/JsonValue.js +0 -109
  256. package/dist/util/JsonValue.js.map +0 -1
  257. package/dist/util/NeverTool.d.ts +0 -3
  258. package/dist/util/NeverTool.js +0 -3
  259. package/dist/util/NeverTool.js.map +0 -1
  260. package/dist/util/Taggable.d.ts +0 -6
  261. package/dist/util/Taggable.js +0 -13
  262. package/dist/util/Taggable.js.map +0 -1
  263. package/dist/util/extract.d.ts +0 -3
  264. package/dist/util/extract.js +0 -2
  265. package/dist/util/extract.js.map +0 -1
  266. package/dist/util/messageCodec.d.ts +0 -98
  267. package/dist/util/messageCodec.js +0 -5
  268. package/dist/util/messageCodec.js.map +0 -1
  269. package/dist/util/normalizeRaw.d.ts +0 -4
  270. package/dist/util/normalizeRaw.js +0 -75
  271. package/dist/util/normalizeRaw.js.map +0 -1
  272. package/dist/util/prefix.d.ts +0 -1
  273. package/dist/util/prefix.js +0 -2
  274. package/dist/util/prefix.js.map +0 -1
  275. package/dist/util/raw.d.ts +0 -6
  276. package/dist/util/raw.js +0 -5
  277. package/dist/util/raw.js.map +0 -1
  278. package/patterns/Debate.ts +0 -2
  279. package/patterns/Model.ts +0 -29
  280. package/patterns/Route.ts +0 -25
  281. package/util/JsonValue.ts +0 -144
  282. package/util/NeverTool.ts +0 -10
  283. package/util/Taggable.ts +0 -33
  284. package/util/extract.ts +0 -7
  285. package/util/messageCodec.ts +0 -5
  286. package/util/normalizeRaw.ts +0 -93
  287. package/util/prefix.ts +0 -1
  288. package/util/raw.ts +0 -27
package/errors.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { Schema as S } from "effect"
2
+
3
+ import type { MethodDefinition } from "./Method.ts"
4
+
5
+ export class AuditionError extends S.TaggedErrorClass<AuditionError>()("AuditionError", {
6
+ value: S.Struct({
7
+ client: S.String,
8
+ routed: S.String,
9
+ }).pipe(S.optional),
10
+ }) {}
11
+
12
+ export class ConnectionError extends S.TaggedErrorClass<ConnectionError>()("ConnectionError", {
13
+ cause: S.Unknown,
14
+ }) {}
15
+
16
+ export type ClientError = AuditionError | ConnectionError
17
+
18
+ export class UnresolvedError extends S.TaggedErrorClass<UnresolvedError>()("UnresolvedError", {}) {}
19
+
20
+ export type FError<MethodDefinitions extends Record<string, MethodDefinition.Any>> = [
21
+ MethodDefinitions[keyof MethodDefinitions]["failure"]["Type"] | ClientError | UnresolvedError,
22
+ ][0]
@@ -0,0 +1,6 @@
1
+ import { Effect, Layer } from "effect"
2
+ import { Chat } from "effect/unstable/ai"
3
+
4
+ import { history } from "./L/history.ts"
5
+
6
+ export const BranchLive = Layer.effect(Chat.Chat, history.pipe(Effect.flatMap(Chat.fromPrompt)))
@@ -0,0 +1,11 @@
1
+ export * from "./append.ts"
2
+ export * from "./assistant.ts"
3
+ export * from "./assistantSchema.ts"
4
+ export * from "./assistantStream.ts"
5
+ export * from "./branch.ts"
6
+ export * from "./clear.ts"
7
+ export * from "./history.ts"
8
+ export * from "./init.ts"
9
+ export * from "./matrix.ts"
10
+ export * from "./system.ts"
11
+ export * from "./user.ts"
@@ -0,0 +1,12 @@
1
+ import { Effect, flow, Ref } from "effect"
2
+ import { Prompt, Chat } from "effect/unstable/ai"
3
+
4
+ export const append = (message: Prompt.Message) =>
5
+ Chat.Chat.asEffect().pipe(
6
+ Effect.flatMap(
7
+ flow(
8
+ ({ history }) => history,
9
+ Ref.update(({ content }) => Prompt.fromMessages([...content, message])),
10
+ ),
11
+ ),
12
+ )
@@ -0,0 +1,16 @@
1
+ import { Effect } from "effect"
2
+ import { Prompt, LanguageModel } from "effect/unstable/ai"
3
+
4
+ import { append } from "./append.ts"
5
+ import { history } from "./history.ts"
6
+
7
+ export const assistant = Effect.gen(function* () {
8
+ const prompt = yield* history
9
+ const { text } = yield* LanguageModel.generateText({ prompt })
10
+ yield* append(
11
+ Prompt.assistantMessage({
12
+ content: [Prompt.textPart({ text })],
13
+ }),
14
+ )
15
+ return text
16
+ })
@@ -0,0 +1,18 @@
1
+ import { Effect, Schema as S } from "effect"
2
+ import { Prompt, LanguageModel } from "effect/unstable/ai"
3
+
4
+ import { append } from "./append.ts"
5
+ import { history } from "./history.ts"
6
+
7
+ export const assistantSchema = Effect.fnUntraced(function* <A, I extends Record<string, unknown>>(
8
+ schema: S.Codec<A, I>,
9
+ ) {
10
+ const prompt = yield* history
11
+ const { text, value } = yield* LanguageModel.generateObject({ prompt, schema })
12
+ yield* append(
13
+ Prompt.assistantMessage({
14
+ content: [Prompt.textPart({ text })],
15
+ }),
16
+ )
17
+ return value
18
+ })
@@ -0,0 +1,9 @@
1
+ import { Effect, Stream } from "effect"
2
+ import { LanguageModel } from "effect/unstable/ai"
3
+
4
+ import { history } from "./history.ts"
5
+
6
+ export const assistantStream = history.pipe(
7
+ Effect.map((prompt) => LanguageModel.streamText({ prompt })),
8
+ Stream.unwrap,
9
+ )
@@ -0,0 +1,5 @@
1
+ import { Effect } from "effect"
2
+
3
+ import { BranchLive } from "../BranchLive.ts"
4
+
5
+ export const branch = Effect.provide(BranchLive)
@@ -0,0 +1,7 @@
1
+ import { Ref, Effect } from "effect"
2
+ import { Chat, Prompt } from "effect/unstable/ai"
3
+
4
+ export const clear = Effect.gen(function* () {
5
+ const { history } = yield* Chat.Chat
6
+ yield* Ref.update(history, () => Prompt.empty)
7
+ })
@@ -0,0 +1,4 @@
1
+ import { flow, Effect, Ref } from "effect"
2
+ import { Chat } from "effect/unstable/ai"
3
+
4
+ export const history = Chat.Chat.asEffect().pipe(Effect.flatMap(flow(({ history }) => history, Ref.get)))
@@ -0,0 +1,4 @@
1
+ import { Effect } from "effect"
2
+ import { Chat } from "effect/unstable/ai"
3
+
4
+ export const init = Effect.provideServiceEffect(Chat.Chat, Chat.empty)
@@ -0,0 +1,26 @@
1
+ import { Effect, Function, Layer } from "effect"
2
+
3
+ import { branch } from "./branch.ts"
4
+
5
+ type LayerRecord = Record<string, Layer.Any>
6
+
7
+ export type MatrixEffect<A, E, R, Layers extends LayerRecord> = Effect.Effect<
8
+ {
9
+ readonly [K in keyof Layers]: A
10
+ },
11
+ E | Layer.Error<Layers[keyof Layers]>,
12
+ R | Layer.Services<Layers[keyof Layers]>
13
+ >
14
+
15
+ export const matrix: {
16
+ <Layers extends LayerRecord>(
17
+ layers: Layers,
18
+ ): <A, E, R>(effect: Effect.Effect<A, E, R>) => MatrixEffect<A, E, R, Layers>
19
+ <Layers extends LayerRecord, A, E, R>(effect: Effect.Effect<A, E, R>, layers: Layers): MatrixEffect<A, E, R, Layers>
20
+ } = Function.dual(2, <Layers extends LayerRecord, A, E, R>(effect: Effect.Effect<A, E, R>, layers: Layers) => {
21
+ return Effect.all(
22
+ Object.fromEntries(
23
+ Object.entries(layers).map(([key, Live]) => [key, effect.pipe(branch, Effect.provide(Live as never))]),
24
+ ),
25
+ )
26
+ })
@@ -0,0 +1,5 @@
1
+ import { Prompt } from "effect/unstable/ai"
2
+
3
+ import { append } from "./append.ts"
4
+
5
+ export const system = (text: string) => append(Prompt.systemMessage({ content: text }))
@@ -0,0 +1,10 @@
1
+ import { Prompt } from "effect/unstable/ai"
2
+
3
+ import { append } from "./append.ts"
4
+
5
+ export const user = (text: string) =>
6
+ append(
7
+ Prompt.userMessage({
8
+ content: [Prompt.textPart({ text })],
9
+ }),
10
+ )
@@ -0,0 +1,35 @@
1
+ import { Path, FileSystem } from "effect"
2
+ import { Option, Layer, Effect, Data, Context } from "effect"
3
+
4
+ export class LoaderError extends Data.TaggedError("LoaderError")<{
5
+ readonly url: string
6
+ }> {}
7
+
8
+ export class Loader extends Context.Service<
9
+ Loader,
10
+ {
11
+ readonly load: (key: string) => Effect.Effect<Option.Option<string>, LoaderError>
12
+ }
13
+ >()("liminal/Loader") {}
14
+
15
+ export const layerFs = Layer.effect(
16
+ Loader,
17
+ Effect.gen(function* () {
18
+ const path = yield* Path.Path
19
+ const fs = yield* FileSystem.FileSystem
20
+
21
+ const load = Effect.fnUntraced(function* (url: string) {
22
+ const { dir, name } = path.parse(new URL(url).pathname)
23
+ const templatePathname = path.join(dir, `${name}.md`)
24
+ if (yield* fs.exists(templatePathname).pipe(Effect.mapError(() => new LoaderError({ url })))) {
25
+ return yield* fs.readFileString(templatePathname).pipe(
26
+ Effect.map(Option.some),
27
+ Effect.mapError(() => new LoaderError({ url })),
28
+ )
29
+ }
30
+ return Option.none()
31
+ })
32
+
33
+ return { load }
34
+ }),
35
+ )
@@ -0,0 +1,44 @@
1
+ import { Effect } from "effect"
2
+
3
+ export type TemplateStringsArrayLike = {
4
+ raw: ReadonlyArray<string> | ArrayLike<string>
5
+ }
6
+
7
+ type ExtractE<T> = [Extract<T, Effect.Effect<any, any, any>>] extends [never]
8
+ ? never
9
+ : Effect.Error<Extract<T, Effect.Effect<any, any, any>>>
10
+
11
+ type ExtractR<T> = [Extract<T, Effect.Effect<any, any, any>>] extends [never]
12
+ ? never
13
+ : Effect.Services<Extract<T, Effect.Effect<any, any, any>>>
14
+
15
+ export const raw = Effect.fnUntraced(function* <Substitutions extends Array<unknown>>(
16
+ template: TemplateStringsArrayLike,
17
+ ...substitutions: Substitutions
18
+ ): Effect.fn.Return<string, ExtractE<Substitutions[number]>, ExtractR<Substitutions[number]>> {
19
+ return String.raw(
20
+ template,
21
+ ...(yield* Effect.all(substitutions.map((v) => (Effect.isEffect(v) ? v : Effect.succeed(v)))) as Effect.Effect<
22
+ Array<string>,
23
+ ExtractE<Substitutions[number]>,
24
+ ExtractR<Substitutions[number]>
25
+ >),
26
+ )
27
+ })
28
+
29
+ export type TaggableHead = TemplateStringsArrayLike | string | Effect.Effect<string, any, any>
30
+
31
+ export type TaggableRest<H extends TaggableHead> = H extends TemplateStringsArrayLike ? Array<unknown> : []
32
+
33
+ export const normalize = Effect.fnUntraced(function* <H extends TaggableHead, ARest extends TaggableRest<H>>(
34
+ a0: H,
35
+ ...aRest: ARest
36
+ ): Effect.fn.Return<string, ExtractE<H | ARest[number]>, ExtractR<H | ARest[number]>> {
37
+ if (Effect.isEffect(a0)) {
38
+ return yield* a0
39
+ }
40
+ if (typeof a0 === "string") {
41
+ return a0
42
+ }
43
+ return yield* raw(a0, ...aRest)
44
+ })
@@ -0,0 +1,85 @@
1
+ import { Layer, Effect, Schema as S, Data, Context } from "effect"
2
+
3
+ import * as Loader from "./Loader.ts"
4
+
5
+ export type Template<A> = (...[value]: A extends undefined ? [] : [value: A]) => string
6
+
7
+ export class TemplateMisalignedError extends Data.TaggedError("TemplateMisalignedError")<{
8
+ readonly url: string
9
+ readonly keys: ReadonlyArray<string>
10
+ }> {}
11
+
12
+ export class NoSuchTemplateError extends Data.TaggedError("NoSuchTemplateError")<{ url: string }> {}
13
+
14
+ export interface TemplateClass<Self, Id extends string, A> extends Context.Service<Self, Template<A>> {
15
+ new (_: never): Context.ServiceClass.Shape<Id, Template<A>>
16
+
17
+ readonly layer: Layer.Layer<Self, NoSuchTemplateError | TemplateMisalignedError | Loader.LoaderError, Loader.Loader>
18
+
19
+ readonly synthesize: (...[value]: A extends undefined ? [] : [value: A]) => Effect.Effect<string, never, Self>
20
+ }
21
+
22
+ export const Service =
23
+ <Self>() =>
24
+ <const Id extends string, const P extends S.Struct.Fields>(
25
+ _id: Id,
26
+ _config: {
27
+ readonly url: string
28
+ readonly payload: P
29
+ },
30
+ ): TemplateClass<Self, Id, S.Struct<P>["Type"]> => {
31
+ throw 0
32
+ }
33
+
34
+ // type Make_ = Template<Payload.FromDefinition<P>>
35
+ // type Self_ = TemplateClass<Self, Id, Payload.FromDefinition<P>>
36
+ // const tag = Context.Service<Self, Make_>()(id)
37
+ // const layer = Effect.gen(function* () {
38
+ // const loader = yield* Loader.Loader
39
+ // const template = yield* loader.load(url).pipe(Effect.map(Option.getOrUndefined))
40
+ // if (!template) {
41
+ // return yield* new NoSuchTemplateError({ url })
42
+ // }
43
+ // const [head, ...rest] = pipe(template, String.trim, String.split("{{"))
44
+ // if ((!payload || !Object.keys(payload).length) && !rest.length) {
45
+ // return () => template
46
+ // }
47
+ // const segments = rest.flatMap((part) => {
48
+ // const i = part.indexOf("}}")
49
+ // if (i === -1) {
50
+ // return []
51
+ // }
52
+ // return {
53
+ // key: part.slice(0, i).trim(),
54
+ // tail: part.slice(i + 2),
55
+ // }
56
+ // })
57
+ // const keys = segments.map(({ key }) => key)
58
+ // if (payload) {
59
+ // const keys_ = new Set(keys)
60
+ // const dne = new Set<string>()
61
+ // const schema = S.Struct(payload)
62
+ // for (const field of schema.ast.propertySignatures) {
63
+ // if (!SchemaAST.isOptional(field.type) && !keys_.has(field.name.toString())) {
64
+ // dne.add(field.name.toString())
65
+ // }
66
+ // }
67
+ // if (dne.size) {
68
+ // return yield* new TemplateMisalignedError({
69
+ // url,
70
+ // keys: [...dne.values()],
71
+ // })
72
+ // }
73
+ // } else if (segments.length) {
74
+ // return yield* new TemplateMisalignedError({ url, keys })
75
+ // }
76
+ // const make: Make_ = (value?) =>
77
+ // value
78
+ // ? head + segments.map(({ key, tail }) => `${(value as Record<typeof key, string>)[key]}${tail}`).join("")
79
+ // : template
80
+ // return make
81
+ // }).pipe(Layer.effect(tag))
82
+
83
+ // const synthesize = (...args) => tag.pipe(Effect.map((make) => make(...args)))
84
+
85
+ // return Object.assign(tag, { synthesize })
@@ -0,0 +1,4 @@
1
+ export * as L from "./L/L.ts"
2
+ export * as Loader from "./Loader.ts"
3
+ export * as Template from "./Template.ts"
4
+ export * from "./BranchLive.ts"
package/index.ts CHANGED
@@ -1,7 +1,9 @@
1
- export * from "./Digest.ts"
2
- export * from "./Envelope.ts"
3
- export * as F from "./F/F.ts"
4
- export * as L from "./L/L.ts"
5
- export * from "./LEvent.ts"
6
- export * from "./Thread.ts"
7
- export * as messageCodec from "./util/messageCodec.ts"
1
+ export * as Accumulator from "./Accumulator.ts"
2
+ export * as Actor from "./Actor.ts"
3
+ export * as Audition from "./Audition.ts"
4
+ export * as Client from "./Client.ts"
5
+ export * as ClientHandle from "./ClientHandle.ts"
6
+ export * as Method from "./Method.ts"
7
+ export * as Protocol from "./Protocol.ts"
8
+ export * from "./F.ts"
9
+ export * from "./errors.ts"
package/package.json CHANGED
@@ -1,43 +1,30 @@
1
1
  {
2
2
  "name": "liminal",
3
- "version": "0.17.0",
3
+ "version": "0.17.2",
4
+ "description": "Actors x Effects",
5
+ "bugs": {
6
+ "url": "https://github.com/crosshatch/liminal/issues"
7
+ },
8
+ "license": "Apache-2.0",
4
9
  "repository": {
5
10
  "type": "git",
6
- "url": "https://github.com/harrysolovay/liminal.git",
11
+ "url": "https://github.com/crosshatch/liminal.git",
7
12
  "directory": "liminal"
8
13
  },
14
+ "type": "module",
15
+ "sideEffects": false,
9
16
  "exports": {
10
17
  ".": {
11
- "import": "./dist/index.js",
12
- "bun": "./index.ts"
13
- },
14
- "./package.json": "./package.json",
15
- "./patterns/Route": {
16
- "import": "./dist/patterns/Route.js",
17
- "bun": "./patterns/Route.ts"
18
+ "types": "./index.ts",
19
+ "default": "./dist/index.js"
18
20
  },
19
- "./patterns/Model": {
20
- "import": "./dist/patterns/Model.js",
21
- "bun": "./patterns/Model.ts"
21
+ "./*": {
22
+ "types": "./*.ts",
23
+ "default": "./dist/*.js"
22
24
  },
23
- "./patterns/Debate": {
24
- "import": "./dist/patterns/Debate.js",
25
- "bun": "./patterns/Debate.ts"
26
- }
25
+ "./package.json": "./package.json"
27
26
  },
28
- "description": "An Effect AI toolkit for conversation state management.",
29
- "homepage": "https://liminal.land",
30
- "license": "Apache-2.0",
31
- "llms": "https://liminal.land/llms.txt",
32
- "llmsFull": "https://liminal.land/llmsFull.txt",
33
- "publishConfig": {
34
- "access": "public",
35
- "provenance": true
36
- },
37
- "sideEffects": false,
38
- "type": "module",
39
27
  "peerDependencies": {
40
- "@effect/ai": "^0.25.2",
41
- "effect": "^3.17.7"
28
+ "effect": "4.0.0-beta.50"
42
29
  }
43
- }
30
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../repos/konfik/tsconfig.base",
3
+ "compilerOptions": {
4
+ "noEmit": false,
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["."],
8
+ "exclude": ["dist"]
9
+ }
package/Digest.ts DELETED
@@ -1,30 +0,0 @@
1
- import * as Effect from "effect/Effect"
2
- import * as Layer from "effect/Layer"
3
- import * as Option from "effect/Option"
4
- import * as Schema from "effect/Schema"
5
- import type { Thread } from "./Thread.ts"
6
- import { prefix } from "./util/prefix.ts"
7
-
8
- export const DigestTypeId: unique symbol = Symbol.for(prefix("Digest"))
9
- export type DigestTypeId = typeof DigestTypeId
10
-
11
- export declare const Digest: <const K extends string, A = string, I = string>(
12
- key: K,
13
- schema?: Schema.Schema<A, I>,
14
- ) => <Self>() => DigestClass<K, A, I, Self>
15
-
16
- export interface DigestClass<K extends string, A, I, Self> extends Effect.Effect<A, never, Self> {
17
- new(_: never): this
18
-
19
- readonly Type: A
20
- readonly Encoded: I
21
- readonly [DigestTypeId]: DigestTypeId
22
- readonly key: K
23
- readonly digest: Effect.Effect<void, never, Self>
24
- readonly layer: <E, R>(
25
- f: (
26
- state: Option.Option<A>,
27
- inbox: Thread,
28
- ) => Effect.Effect<A, E, R>,
29
- ) => Layer.Layer<Self, E, Exclude<R, Self | Thread>>
30
- }
package/Envelope.ts DELETED
@@ -1,16 +0,0 @@
1
- import { Thread } from "./Thread.ts"
2
-
3
- export type Envelope =
4
- & ({
5
- to: Array<Thread>
6
- cc?: never
7
- } | {
8
- to?: never
9
- cc: Array<Thread>
10
- } | {
11
- to: Array<Thread>
12
- cc: Array<Thread>
13
- })
14
- & {
15
- bcc?: Array<Thread>
16
- }
package/F/F.ts DELETED
@@ -1 +0,0 @@
1
- export * from "../L/json.ts"
package/L/L.ts DELETED
@@ -1,20 +0,0 @@
1
- export * from "./append.ts"
2
- export * from "./assistant.ts"
3
- export * from "./assistantSchema.ts"
4
- export * from "./assistantStream.ts"
5
- export * from "./branch.ts"
6
- export * from "./clear.ts"
7
- export * from "./disable.ts"
8
- export * from "./enable.ts"
9
- export * from "./events.ts"
10
- export * from "./line.ts"
11
- export * from "./listen.ts"
12
- export * from "./messages.ts"
13
- export * from "./prev.ts"
14
- export * from "./provide.ts"
15
- export * from "./self.ts"
16
- export * from "./send.ts"
17
- export * from "./system.ts"
18
- export * from "./thread.ts"
19
- export * from "./toolkit.ts"
20
- export * from "./user.ts"
package/L/append.ts DELETED
@@ -1,14 +0,0 @@
1
- import type { Message } from "@effect/ai/AiInput"
2
- import * as Effect from "effect/Effect"
3
- import { MessagesAppendedEvent } from "../LEvent.ts"
4
- import type { Thread } from "../Thread.ts"
5
- import { self } from "./self.ts"
6
-
7
- /** Append messages to the thread. */
8
- export const append: (
9
- ...messages: Array<Message>
10
- ) => Effect.Effect<void, never, Thread> = Effect.fnUntraced(function*(...messages) {
11
- const { state, events } = yield* self
12
- state.messages.push(...messages)
13
- yield* events.publish(MessagesAppendedEvent.make({ messages }))
14
- })
package/L/assistant.ts DELETED
@@ -1,30 +0,0 @@
1
- import type { AiError } from "@effect/ai/AiError"
2
- import { AssistantMessage, TextPart } from "@effect/ai/AiInput"
3
- import { AiLanguageModel } from "@effect/ai/AiLanguageModel"
4
- import * as Effect from "effect/Effect"
5
- import * as Option from "effect/Option"
6
- import type { Thread } from "../Thread.ts"
7
- import { append } from "./append.ts"
8
- import { self } from "./self.ts"
9
- import { toolkit } from "./toolkit.ts"
10
-
11
- /** Infer an assistant message and append it to the thread. */
12
- export const assistant: Effect.Effect<string, AiError, AiLanguageModel | Thread> = Effect.gen(function*() {
13
- const model = yield* AiLanguageModel
14
- const { state: { system, messages: prompt } } = yield* self
15
- let { text, results } = yield* model.generateText({
16
- system: Option.getOrUndefined(system),
17
- prompt,
18
- toolkit,
19
- })
20
- // TODO: this shouldn't be necessary. Bug in Effect AI?
21
- if (!text.trim()) {
22
- text = results.values().next().value?.result as never as string
23
- }
24
- yield* append(
25
- AssistantMessage.make({
26
- parts: [TextPart.make({ text })],
27
- }),
28
- )
29
- return text
30
- })
@@ -1,61 +0,0 @@
1
- import type { AiError } from "@effect/ai/AiError"
2
- import { AssistantMessage, TextPart } from "@effect/ai/AiInput"
3
- import { AiLanguageModel } from "@effect/ai/AiLanguageModel"
4
- import * as Effect from "effect/Effect"
5
- import * as Option from "effect/Option"
6
- import * as Schema from "effect/Schema"
7
- import * as SchemaAST from "effect/SchemaAST"
8
- import type { Thread } from "../Thread.ts"
9
- import { encodeJsonc, type JsonValue } from "../util/JsonValue.ts"
10
- import { append } from "./append.ts"
11
- import { self } from "./self.ts"
12
-
13
- /** Infer a structured assistant message and append its JSON representation to the conversation. */
14
- export const assistantSchema: {
15
- <F extends Record<string, Schema.Schema.AnyNoContext>>(
16
- fields: F,
17
- ): Effect.Effect<{ [K in keyof F]: Schema.Schema.Type<F[K]> }, AiError, AiLanguageModel | Thread>
18
- <O, I extends JsonValue>(
19
- schema: Schema.Schema<O, I, never>,
20
- ): Effect.Effect<O, AiError, AiLanguageModel | Thread>
21
- } = Effect.fnUntraced(function*(schema) {
22
- const model = yield* AiLanguageModel
23
- const { state: { system, messages } } = yield* self
24
-
25
- const isSchema = Schema.isSchema(schema)
26
- const schema_ = isSchema ? schema : Schema.Struct(schema) as Schema.Schema.AnyNoContext
27
- const isObject = !isSchema || isObjectSchema(SchemaAST.encodedAST(schema.ast))
28
- const wrapped = isObject ? schema_ : Schema.Struct({ inner: schema_ })
29
-
30
- const value = yield* model.generateObject({
31
- system: Option.getOrUndefined(system),
32
- schema: wrapped,
33
- prompt: messages,
34
- }).pipe(
35
- Effect.map(({ value }) => isObject ? value : value["inner"]),
36
- )
37
-
38
- yield* append(
39
- AssistantMessage.make({
40
- parts: [
41
- TextPart.make({
42
- text: yield* encodeJsonc(schema_)(value),
43
- }),
44
- ],
45
- }),
46
- )
47
- return value
48
- })
49
-
50
- const isObjectSchema = (ast: SchemaAST.AST): boolean => {
51
- switch (ast._tag) {
52
- case "TypeLiteral": {
53
- return true
54
- }
55
- case "Refinement":
56
- case "Transformation": {
57
- return isObjectSchema(ast.from)
58
- }
59
- }
60
- return false
61
- }
@@ -1,22 +0,0 @@
1
- import type { AiError } from "@effect/ai/AiError"
2
- import { AiLanguageModel } from "@effect/ai/AiLanguageModel"
3
- import type { AiResponse } from "@effect/ai/AiResponse"
4
- import * as Effect from "effect/Effect"
5
- import * as Option from "effect/Option"
6
- import * as Stream from "effect/Stream"
7
- import type { Thread } from "../Thread.ts"
8
- import { self } from "./self.ts"
9
- import { toolkit } from "./toolkit.ts"
10
-
11
- /** Get a stream of an assistant message (does not append the message to the thread). */
12
- export const assistantStream: Stream.Stream<AiResponse, AiError, AiLanguageModel | Thread> = Stream.unwrap(
13
- Effect.gen(function*() {
14
- const model = yield* AiLanguageModel
15
- const { state: { system, messages: prompt } } = yield* self
16
- return model.streamText({
17
- system: Option.getOrUndefined(system),
18
- prompt,
19
- toolkit,
20
- })
21
- }),
22
- )