liminal 0.5.17 → 0.6.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 (222) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/Context.ts +11 -47
  3. package/L.ts +4 -0
  4. package/LEvent.ts +20 -53
  5. package/_emit.ts +16 -0
  6. package/assistant.ts +53 -0
  7. package/dist/Context.d.ts +15 -30
  8. package/dist/Context.js +9 -22
  9. package/dist/Context.js.map +1 -1
  10. package/dist/L.d.ts +4 -0
  11. package/dist/L.js +5 -0
  12. package/dist/L.js.map +1 -0
  13. package/dist/LEvent.d.ts +29 -62
  14. package/dist/LEvent.js +14 -46
  15. package/dist/LEvent.js.map +1 -1
  16. package/dist/_emit.d.ts +2 -0
  17. package/dist/_emit.js +16 -0
  18. package/dist/_emit.js.map +1 -0
  19. package/dist/assistant.d.ts +8 -0
  20. package/dist/assistant.js +46 -0
  21. package/dist/assistant.js.map +1 -0
  22. package/dist/index.d.ts +2 -13
  23. package/dist/index.js +2 -13
  24. package/dist/index.js.map +1 -1
  25. package/dist/messages.d.ts +4 -0
  26. package/dist/messages.js +4 -0
  27. package/dist/messages.js.map +1 -0
  28. package/dist/set.d.ts +4 -0
  29. package/dist/set.js +9 -0
  30. package/dist/set.js.map +1 -0
  31. package/dist/strand.d.ts +16 -0
  32. package/dist/strand.js +16 -0
  33. package/dist/strand.js.map +1 -0
  34. package/dist/todo.test.js +3 -0
  35. package/dist/todo.test.js.map +1 -0
  36. package/dist/tsconfig.tsbuildinfo +1 -1
  37. package/dist/user.d.ts +6 -0
  38. package/dist/user.js +18 -0
  39. package/dist/user.js.map +1 -0
  40. package/dist/util/JSONValue.d.ts +0 -3
  41. package/dist/util/JSONValue.js +1 -31
  42. package/dist/util/JSONValue.js.map +1 -1
  43. package/index.ts +2 -13
  44. package/messages.ts +5 -0
  45. package/package.json +4 -3
  46. package/set.ts +10 -0
  47. package/strand.ts +38 -0
  48. package/todo.test.ts +3 -0
  49. package/user.ts +24 -0
  50. package/util/JSONValue.ts +0 -30
  51. package/Adapter.ts +0 -27
  52. package/AdapterRegistry.ts +0 -78
  53. package/Config.ts +0 -13
  54. package/Definition.ts +0 -48
  55. package/EventBase.ts +0 -23
  56. package/Handler.ts +0 -4
  57. package/L/L.ts +0 -24
  58. package/L/all.ts +0 -39
  59. package/L/assistant.ts +0 -28
  60. package/L/catch.ts +0 -38
  61. package/L/continuation.ts +0 -14
  62. package/L/emit.ts +0 -16
  63. package/L/focus.ts +0 -17
  64. package/L/infer.ts +0 -34
  65. package/L/message.ts +0 -16
  66. package/L/reflect.ts +0 -13
  67. package/L/run.ts +0 -29
  68. package/L/schema/_schema_common.ts +0 -30
  69. package/L/schema/anyOf.ts +0 -10
  70. package/L/schema/array.ts +0 -13
  71. package/L/schema/boolean.ts +0 -8
  72. package/L/schema/const.ts +0 -18
  73. package/L/schema/enum.ts +0 -18
  74. package/L/schema/integer.ts +0 -6
  75. package/L/schema/null.ts +0 -8
  76. package/L/schema/number.ts +0 -8
  77. package/L/schema/object.ts +0 -21
  78. package/L/schema/string.ts +0 -11
  79. package/L/strand.ts +0 -22
  80. package/L/stream.ts +0 -16
  81. package/L/system.ts +0 -19
  82. package/L/user.ts +0 -19
  83. package/LiminalAssertionError.ts +0 -19
  84. package/Message.ts +0 -16
  85. package/Rune.test.ts +0 -5
  86. package/Rune.ts +0 -60
  87. package/Schema.ts +0 -185
  88. package/Strand.ts +0 -284
  89. package/Tool.ts +0 -26
  90. package/TypeAdapter.ts +0 -3
  91. package/dist/Adapter.d.ts +0 -18
  92. package/dist/Adapter.js +0 -13
  93. package/dist/Adapter.js.map +0 -1
  94. package/dist/AdapterRegistry.d.ts +0 -27
  95. package/dist/AdapterRegistry.js +0 -62
  96. package/dist/AdapterRegistry.js.map +0 -1
  97. package/dist/Config.d.ts +0 -12
  98. package/dist/Config.js +0 -2
  99. package/dist/Config.js.map +0 -1
  100. package/dist/Definition.d.ts +0 -24
  101. package/dist/Definition.js +0 -29
  102. package/dist/Definition.js.map +0 -1
  103. package/dist/EventBase.d.ts +0 -11
  104. package/dist/EventBase.js +0 -16
  105. package/dist/EventBase.js.map +0 -1
  106. package/dist/Handler.d.ts +0 -3
  107. package/dist/Handler.js +0 -2
  108. package/dist/Handler.js.map +0 -1
  109. package/dist/L/L.d.ts +0 -24
  110. package/dist/L/L.js +0 -25
  111. package/dist/L/L.js.map +0 -1
  112. package/dist/L/all.d.ts +0 -14
  113. package/dist/L/all.js +0 -20
  114. package/dist/L/all.js.map +0 -1
  115. package/dist/L/assistant.d.ts +0 -11
  116. package/dist/L/assistant.js +0 -15
  117. package/dist/L/assistant.js.map +0 -1
  118. package/dist/L/catch.d.ts +0 -16
  119. package/dist/L/catch.js +0 -25
  120. package/dist/L/catch.js.map +0 -1
  121. package/dist/L/continuation.d.ts +0 -4
  122. package/dist/L/continuation.js +0 -13
  123. package/dist/L/continuation.js.map +0 -1
  124. package/dist/L/emit.d.ts +0 -7
  125. package/dist/L/emit.js +0 -15
  126. package/dist/L/emit.js.map +0 -1
  127. package/dist/L/focus.d.ts +0 -8
  128. package/dist/L/focus.js +0 -14
  129. package/dist/L/focus.js.map +0 -1
  130. package/dist/L/infer.d.ts +0 -8
  131. package/dist/L/infer.js +0 -30
  132. package/dist/L/infer.js.map +0 -1
  133. package/dist/L/message.d.ts +0 -8
  134. package/dist/L/message.js +0 -14
  135. package/dist/L/message.js.map +0 -1
  136. package/dist/L/reflect.d.ts +0 -5
  137. package/dist/L/reflect.js +0 -11
  138. package/dist/L/reflect.js.map +0 -1
  139. package/dist/L/run.d.ts +0 -14
  140. package/dist/L/run.js +0 -16
  141. package/dist/L/run.js.map +0 -1
  142. package/dist/L/schema/_schema_common.d.ts +0 -6
  143. package/dist/L/schema/_schema_common.js +0 -20
  144. package/dist/L/schema/_schema_common.js.map +0 -1
  145. package/dist/L/schema/anyOf.d.ts +0 -5
  146. package/dist/L/schema/anyOf.js +0 -5
  147. package/dist/L/schema/anyOf.js.map +0 -1
  148. package/dist/L/schema/array.d.ts +0 -5
  149. package/dist/L/schema/array.js +0 -8
  150. package/dist/L/schema/array.js.map +0 -1
  151. package/dist/L/schema/boolean.d.ts +0 -6
  152. package/dist/L/schema/boolean.js +0 -4
  153. package/dist/L/schema/boolean.js.map +0 -1
  154. package/dist/L/schema/const.d.ts +0 -8
  155. package/dist/L/schema/const.js +0 -10
  156. package/dist/L/schema/const.js.map +0 -1
  157. package/dist/L/schema/enum.d.ts +0 -8
  158. package/dist/L/schema/enum.js +0 -10
  159. package/dist/L/schema/enum.js.map +0 -1
  160. package/dist/L/schema/integer.d.ts +0 -5
  161. package/dist/L/schema/integer.js +0 -3
  162. package/dist/L/schema/integer.js.map +0 -1
  163. package/dist/L/schema/null.d.ts +0 -6
  164. package/dist/L/schema/null.js +0 -4
  165. package/dist/L/schema/null.js.map +0 -1
  166. package/dist/L/schema/number.d.ts +0 -6
  167. package/dist/L/schema/number.js +0 -4
  168. package/dist/L/schema/number.js.map +0 -1
  169. package/dist/L/schema/object.d.ts +0 -9
  170. package/dist/L/schema/object.js +0 -12
  171. package/dist/L/schema/object.js.map +0 -1
  172. package/dist/L/schema/string.d.ts +0 -8
  173. package/dist/L/schema/string.js +0 -4
  174. package/dist/L/schema/string.js.map +0 -1
  175. package/dist/L/strand.d.ts +0 -9
  176. package/dist/L/strand.js +0 -16
  177. package/dist/L/strand.js.map +0 -1
  178. package/dist/L/stream.d.ts +0 -4
  179. package/dist/L/stream.js +0 -15
  180. package/dist/L/stream.js.map +0 -1
  181. package/dist/L/system.d.ts +0 -4
  182. package/dist/L/system.js +0 -9
  183. package/dist/L/system.js.map +0 -1
  184. package/dist/L/user.d.ts +0 -4
  185. package/dist/L/user.js +0 -9
  186. package/dist/L/user.js.map +0 -1
  187. package/dist/LiminalAssertionError.d.ts +0 -8
  188. package/dist/LiminalAssertionError.js +0 -20
  189. package/dist/LiminalAssertionError.js.map +0 -1
  190. package/dist/Message.d.ts +0 -14
  191. package/dist/Message.js +0 -2
  192. package/dist/Message.js.map +0 -1
  193. package/dist/Rune.d.ts +0 -46
  194. package/dist/Rune.js +0 -11
  195. package/dist/Rune.js.map +0 -1
  196. package/dist/Rune.test.js +0 -5
  197. package/dist/Rune.test.js.map +0 -1
  198. package/dist/Schema.d.ts +0 -46
  199. package/dist/Schema.js +0 -130
  200. package/dist/Schema.js.map +0 -1
  201. package/dist/Strand.d.ts +0 -81
  202. package/dist/Strand.js +0 -200
  203. package/dist/Strand.js.map +0 -1
  204. package/dist/Tool.d.ts +0 -10
  205. package/dist/Tool.js +0 -19
  206. package/dist/Tool.js.map +0 -1
  207. package/dist/TypeAdapter.d.ts +0 -1
  208. package/dist/TypeAdapter.js +0 -3
  209. package/dist/TypeAdapter.js.map +0 -1
  210. package/dist/errors.d.ts +0 -9
  211. package/dist/errors.js +0 -11
  212. package/dist/errors.js.map +0 -1
  213. package/dist/util/EnsureNarrow.d.ts +0 -1
  214. package/dist/util/EnsureNarrow.js +0 -2
  215. package/dist/util/EnsureNarrow.js.map +0 -1
  216. package/dist/util/attachCustomInspect.d.ts +0 -1
  217. package/dist/util/attachCustomInspect.js +0 -11
  218. package/dist/util/attachCustomInspect.js.map +0 -1
  219. package/errors.ts +0 -12
  220. package/util/EnsureNarrow.ts +0 -1
  221. package/util/attachCustomInspect.ts +0 -14
  222. /package/dist/{Rune.test.d.ts → todo.test.d.ts} +0 -0
package/dist/user.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { Effect } from "effect";
2
+ import { Handler, MessagesRef } from "./Context.ts";
3
+ export declare const user: {
4
+ (template: TemplateStringsArray, ...substitutions: Array<unknown>): Effect.Effect<void, never, MessagesRef | Handler>;
5
+ (message: string): Effect.Effect<void, never, MessagesRef | Handler>;
6
+ };
package/dist/user.js ADDED
@@ -0,0 +1,18 @@
1
+ import { AiInput } from "@effect/ai";
2
+ import { Effect, Ref } from "effect";
3
+ import { _emit } from "./_emit.js";
4
+ import { Handler, MessagesRef } from "./Context.js";
5
+ import { MessageAppended } from "./LEvent.js";
6
+ import { isTemplateStringsArray } from "./util/isTemplateStringsArray.js";
7
+ export const user = Effect.fn(function* (e0, ...eRest) {
8
+ const message = new AiInput.UserMessage({
9
+ parts: [
10
+ new AiInput.TextPart({
11
+ text: isTemplateStringsArray(e0) ? String.raw({ raw: e0 }, ...eRest) : e0,
12
+ }),
13
+ ],
14
+ });
15
+ yield* _emit(new MessageAppended({ message }));
16
+ yield* Ref.update(yield* MessagesRef, (messages) => [...messages, message]);
17
+ });
18
+ //# sourceMappingURL=user.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user.js","sourceRoot":"","sources":["../user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAA;AACpC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,sBAAsB,EAAE,MAAM,kCAAkC,CAAA;AAEzE,MAAM,CAAC,MAAM,IAAI,GAMb,MAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,EAAC,EAAE,EAAE,GAAG,KAAK;IAClC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,WAAW,CAAC;QACtC,KAAK,EAAE;YACL,IAAI,OAAO,CAAC,QAAQ,CAAC;gBACnB,IAAI,EAAE,sBAAsB,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE;aAC1E,CAAC;SACH;KACF,CAAC,CAAA;IACF,KAAK,CAAC,CAAC,KAAK,CAAC,IAAI,eAAe,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAA;IAC9C,KAAK,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,GAAG,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAA;AAC7E,CAAC,CAAU,CAAA"}
@@ -1,7 +1,4 @@
1
1
  export type JSONValue = null | boolean | number | string | JSONValueArray | JSONValueObject;
2
- export declare namespace JSONValue {
3
- function assert(value: unknown, visited?: Set<object>): asserts value is JSONValue;
4
- }
5
2
  export type JSONValueArray = Array<JSONValue>;
6
3
  export type JSONValueObject = {
7
4
  [key: string]: JSONValue;
@@ -1,32 +1,2 @@
1
- import { LiminalAssertionError } from "liminal";
2
- export var JSONValue;
3
- (function (JSONValue) {
4
- function assert(value, visited = new Set()) {
5
- if (value === null)
6
- return;
7
- const t = typeof value;
8
- if (typeof value !== "object") {
9
- LiminalAssertionError.assert(t === "string" || t === "number" || t === "boolean");
10
- return;
11
- }
12
- LiminalAssertionError.assert(!visited.has(value));
13
- visited.add(value);
14
- try {
15
- if (Array.isArray(value)) {
16
- for (let i = 0; i < value.length; i++) {
17
- assert(value[i], new Set(visited));
18
- }
19
- }
20
- else {
21
- for (const key in value) {
22
- assert(value[key], new Set(visited));
23
- }
24
- }
25
- }
26
- finally {
27
- visited.delete(value);
28
- }
29
- }
30
- JSONValue.assert = assert;
31
- })(JSONValue || (JSONValue = {}));
1
+ export {};
32
2
  //# sourceMappingURL=JSONValue.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"JSONValue.js","sourceRoot":"","sources":["../../util/JSONValue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAA;AAG/C,MAAM,KAAW,SAAS,CA2BzB;AA3BD,WAAiB,SAAS;IACxB,SAAgB,MAAM,CACpB,KAAc,EACd,UAAuB,IAAI,GAAG,EAAU;QAExC,IAAI,KAAK,KAAK,IAAI;YAAE,OAAM;QAC1B,MAAM,CAAC,GAAG,OAAO,KAAK,CAAA;QACtB,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,qBAAqB,CAAC,MAAM,CAAC,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,SAAS,CAAC,CAAA;YACjF,OAAM;QACR,CAAC;QACD,qBAAqB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAA;QACjD,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;QAClB,IAAI,CAAC;YACH,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;gBACzB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;oBACtC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC,CAAA;gBACpC,CAAC;YACH,CAAC;iBAAM,CAAC;gBACN,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE,CAAC;oBACxB,MAAM,CAAC,KAAK,CAAC,GAAY,CAAC,EAAE,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC,CAAA;gBAC/C,CAAC;YACH,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QACvB,CAAC;IACH,CAAC;IAzBe,gBAAM,SAyBrB,CAAA;AACH,CAAC,EA3BgB,SAAS,KAAT,SAAS,QA2BzB"}
1
+ {"version":3,"file":"JSONValue.js","sourceRoot":"","sources":["../../util/JSONValue.ts"],"names":[],"mappings":""}
package/index.ts CHANGED
@@ -1,15 +1,4 @@
1
- export * from "./Adapter.ts"
2
- export * from "./AdapterRegistry.ts"
3
- export * from "./Config.ts"
4
1
  export * from "./Context.ts"
5
- export * from "./Definition.ts"
6
- export * from "./EventBase.ts"
7
- export * as L from "./L/L.ts"
2
+ export * as L from "./L.ts"
8
3
  export * from "./LEvent.ts"
9
- export * from "./LiminalAssertionError.ts"
10
- export * from "./Message.ts"
11
- export * from "./Rune.ts"
12
- export * from "./Schema.ts"
13
- export * from "./Strand.ts"
14
- export * from "./Tool.ts"
15
- export * from "./TypeAdapter.ts"
4
+ export * from "./strand.ts"
package/messages.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type { AiInput } from "@effect/ai"
2
+ import { Effect, Ref } from "effect"
3
+ import { MessagesRef } from "./Context.ts"
4
+
5
+ export const messages: Effect.Effect<Array<AiInput.Message>, never, MessagesRef> = Effect.flatMap(MessagesRef, Ref.get)
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.5.17",
6
+ "version": "0.6.0",
7
7
  "license": "Apache-2.0",
8
8
  "repository": {
9
9
  "type": "git",
@@ -15,7 +15,8 @@
15
15
  "exports": {
16
16
  ".": "./dist/index.js"
17
17
  },
18
- "devDependencies": {
19
- "conditional-type-checks": "^1.0.6"
18
+ "peerDependencies": {
19
+ "@effect/ai": "^0.18.2",
20
+ "effect": "^3.16.3"
20
21
  }
21
22
  }
package/set.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { AiInput } from "@effect/ai"
2
+ import { Effect, Ref } from "effect"
3
+ import { MessagesRef } from "./Context.ts"
4
+
5
+ export const set = Effect.fn(function*(messages: Iterable<AiInput.Message>) {
6
+ const messagesRef = yield* MessagesRef
7
+ const previous = yield* Ref.get(messagesRef)
8
+ yield* Ref.set(messagesRef, [...messages])
9
+ return previous
10
+ })
package/strand.ts ADDED
@@ -0,0 +1,38 @@
1
+ import type { AiInput, AiTool, AiToolkit } from "@effect/ai"
2
+ import { Effect, Option, Ref } from "effect"
3
+ import { Handler, MessagesRef, System, Toolkit } from "./Context.ts"
4
+ import { LEvent } from "./LEvent.ts"
5
+
6
+ export interface StrandOptions<E, R, in out T extends AiTool.Any> {
7
+ /** The system prompt. */
8
+ system?: string | undefined
9
+ /** The initial list of AI input messages. */
10
+ messages?: Iterable<AiInput.Message> | undefined
11
+ /** The liminal event handler. */
12
+ handler?: ((event: LEvent) => Effect.Effect<any, E, R>) | undefined
13
+ /** The tools to use for by strand. */
14
+ tools?: AiToolkit.AiToolkit<T> | undefined
15
+ }
16
+
17
+ /** Create an isolated clone of the current conversation to provide for an effect. */
18
+ export const strand: <HE = never, HR = never, T extends AiTool.Any = never>(
19
+ options?: StrandOptions<HE, HR, T>,
20
+ ) => <A, E, R>(
21
+ effect: Effect.Effect<A, E, R>,
22
+ ) => Effect.Effect<A, E | HE, Exclude<R, MessagesRef | System | Handler> | T> = (options) => (effect) =>
23
+ Effect.gen(function*() {
24
+ const messagesRef = options?.messages
25
+ ? Ref.unsafeMake([...options.messages])
26
+ : yield* Option.match(yield* Effect.serviceOption(MessagesRef), {
27
+ *onSome(ref) {
28
+ return Ref.unsafeMake([...yield* Ref.get(ref)])
29
+ },
30
+ onNone: () => Ref.make([] as Array<AiInput.Message>),
31
+ })
32
+ return yield* (effect.pipe(
33
+ Effect.provideService(MessagesRef, messagesRef),
34
+ Effect.provideService(System, options?.system),
35
+ Effect.provideService(Handler, options?.handler),
36
+ Effect.provideService(Toolkit, options?.tools),
37
+ ))
38
+ }) as never // TODO
package/todo.test.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { it } from "vitest"
2
+
3
+ it("works", () => {})
package/user.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { AiInput } from "@effect/ai"
2
+ import { Effect, Ref } from "effect"
3
+ import { _emit } from "./_emit.ts"
4
+ import { Handler, MessagesRef } from "./Context.ts"
5
+ import { MessageAppended } from "./LEvent.ts"
6
+ import { isTemplateStringsArray } from "./util/isTemplateStringsArray.ts"
7
+
8
+ export const user: {
9
+ (
10
+ template: TemplateStringsArray,
11
+ ...substitutions: Array<unknown>
12
+ ): Effect.Effect<void, never, MessagesRef | Handler>
13
+ (message: string): Effect.Effect<void, never, MessagesRef | Handler>
14
+ } = Effect.fn(function*(e0, ...eRest) {
15
+ const message = new AiInput.UserMessage({
16
+ parts: [
17
+ new AiInput.TextPart({
18
+ text: isTemplateStringsArray(e0) ? String.raw({ raw: e0 }, ...eRest) : e0,
19
+ }),
20
+ ],
21
+ })
22
+ yield* _emit(new MessageAppended({ message }))
23
+ yield* Ref.update(yield* MessagesRef, (messages) => [...messages, message])
24
+ }) as never
package/util/JSONValue.ts CHANGED
@@ -1,34 +1,4 @@
1
- import { LiminalAssertionError } from "liminal"
2
-
3
1
  export type JSONValue = null | boolean | number | string | JSONValueArray | JSONValueObject
4
- export namespace JSONValue {
5
- export function assert(
6
- value: unknown,
7
- visited: Set<object> = new Set<object>(),
8
- ): asserts value is JSONValue {
9
- if (value === null) return
10
- const t = typeof value
11
- if (typeof value !== "object") {
12
- LiminalAssertionError.assert(t === "string" || t === "number" || t === "boolean")
13
- return
14
- }
15
- LiminalAssertionError.assert(!visited.has(value))
16
- visited.add(value)
17
- try {
18
- if (Array.isArray(value)) {
19
- for (let i = 0; i < value.length; i++) {
20
- assert(value[i], new Set(visited))
21
- }
22
- } else {
23
- for (const key in value) {
24
- assert(value[key as never], new Set(visited))
25
- }
26
- }
27
- } finally {
28
- visited.delete(value)
29
- }
30
- }
31
- }
32
2
 
33
3
  export type JSONValueArray = Array<JSONValue>
34
4
 
package/Adapter.ts DELETED
@@ -1,27 +0,0 @@
1
- import type { Message } from "./Message.ts"
2
- import type { SchemaObject } from "./Schema.ts"
3
- import type { Tool } from "./Tool.ts"
4
- import { attachCustomInspect } from "./util/attachCustomInspect.ts"
5
-
6
- export class Adapter {
7
- constructor(
8
- readonly name: string,
9
- readonly seal: (envelope: Envelope) => SealedEnvelope,
10
- ) {}
11
-
12
- static {
13
- attachCustomInspect(this, ({ name }) => ({ name }))
14
- }
15
- }
16
-
17
- export interface Envelope {
18
- messages: Array<Message>
19
- schema?: SchemaObject | undefined
20
- signal: AbortSignal
21
- tools?: Set<Tool> | undefined
22
- }
23
-
24
- export interface SealedEnvelope {
25
- resolve: () => Promise<string>
26
- stream: () => ReadableStream<string>
27
- }
@@ -1,78 +0,0 @@
1
- import type { Adapter } from "./Adapter.ts"
2
- import { LiminalAssertionError } from "./LiminalAssertionError.ts"
3
-
4
- /**
5
- * An intrusive doubly-linked list for storing `Adapter`s.
6
- * Provides efficient insertion, removal, and lookups.
7
- */
8
- export class AdapterRegistry {
9
- declare head?: ModelRegistryNode | undefined
10
- declare tail?: ModelRegistryNode | undefined
11
-
12
- /** Returns the most recently registered adapter */
13
- peek() {
14
- return this.tail?.adapter
15
- }
16
-
17
- /** Ensure */
18
- ensure(): Adapter {
19
- LiminalAssertionError.assert(
20
- this.tail,
21
- "No conversation adapter registered. Use `L.focus` to focus a conversation adapter.",
22
- )
23
- return this.tail.adapter
24
- }
25
-
26
- /**
27
- * Registers a new adapter and returns the created node
28
- * @param value The adapter to register
29
- */
30
- register(value: Adapter): ModelRegistryNode {
31
- const node: ModelRegistryNode = {
32
- prev: this.tail,
33
- adapter: value,
34
- }
35
- if (this.tail) {
36
- this.tail.next = node
37
- } else {
38
- this.head = node
39
- }
40
- this.tail = node
41
- return node
42
- }
43
-
44
- /** Remove a adapter from the registry. */
45
- remove(node: ModelRegistryNode) {
46
- if (node.prev) {
47
- node.prev.next = node.next
48
- }
49
- if (node.next) {
50
- node.next.prev = node.prev
51
- }
52
- if (node === this.head) {
53
- this.head = node.next
54
- }
55
- if (node === this.tail) {
56
- this.tail = node.prev
57
- }
58
- node.prev = undefined
59
- delete node.next
60
- }
61
-
62
- /** Creates a deep copy of this registry. */
63
- clone() {
64
- const instance = new AdapterRegistry()
65
- for (let node = this.head; node; node = node.next) {
66
- if (node.adapter) {
67
- instance.register(node.adapter)
68
- }
69
- }
70
- return instance
71
- }
72
- }
73
-
74
- export interface ModelRegistryNode {
75
- prev: ModelRegistryNode | undefined
76
- adapter: Adapter
77
- next?: ModelRegistryNode | undefined
78
- }
package/Config.ts DELETED
@@ -1,13 +0,0 @@
1
- import { AdapterRegistry } from "./AdapterRegistry.ts"
2
- import type { Message } from "./Message.ts"
3
- import type { Rune } from "./Rune.ts"
4
- import type { Strand } from "./Strand.ts"
5
- import type { Tool } from "./Tool.ts"
6
-
7
- export interface Config<Y extends Rune<any> = Rune<any>, T = any> {
8
- handler?: ((this: Strand<Y, T>, event: Rune.E<Y>) => void) | undefined
9
- models?: AdapterRegistry | undefined
10
- messages?: Array<Message>
11
- tools?: Set<Tool> | undefined
12
- signal?: AbortSignal | undefined
13
- }
package/Definition.ts DELETED
@@ -1,48 +0,0 @@
1
- import type { Rune } from "./Rune.ts"
2
-
3
- export type RuneIterator<Y extends Rune<any> = Rune<any>, T = any> = Iterator<Y, T> | AsyncIterator<Y, T>
4
-
5
- export type RuneIterable<Y extends Rune<any> = Rune<any>, T = any> = Iterable<Y, T> | AsyncIterable<Y, T>
6
-
7
- export type Definition<Y extends Rune<any> = Rune<any>, T = any> = RuneIterable<Y, T> | (() => RuneIterable<Y, T>)
8
-
9
- export namespace Definition {
10
- /** Extracts the Rune type from a Definition. */
11
- export type Y<X extends Definition> = X extends RuneIterable<infer Y> ? Y
12
- : X extends () => RuneIterable<infer Y> ? Y
13
- : X extends RuneIterator<infer Y> ? Y
14
- : never
15
-
16
- /** Extracts the result type from a Definition. */
17
- export type T<X extends Definition> = X extends RuneIterable<Rune<any>, infer T> ? T
18
- : X extends () => RuneIterable<Rune<any>, infer T> ? T
19
- : X extends RuneIterator<Rune<any>, infer T> ? T
20
- : never
21
-
22
- /** Extracts the event type from a Definition. */
23
- export type E<X extends Definition> = Rune.E<Y<X>>
24
-
25
- /**
26
- * Converts a Definition into a RuneIterator.
27
- *
28
- * This function handles all the different forms a Definition can take:
29
- * - Synchronous iterables
30
- * - Asynchronous iterables
31
- * - Factory functions returning iterables
32
- *
33
- * @param definition The definition to unwrap.
34
- * @returns A RuneIterator ready for consumption.
35
- */
36
- export function unwrap<Y extends Rune<any>, T>(definition: Definition<Y, T>): RuneIterator<Y, T> {
37
- if (Symbol.iterator in definition) {
38
- return definition[Symbol.iterator]()
39
- } else if (Symbol.asyncIterator in definition) {
40
- return definition[Symbol.asyncIterator]()
41
- }
42
- const iterable = definition()
43
- if (Symbol.iterator in iterable) {
44
- return iterable[Symbol.iterator]()
45
- }
46
- return iterable[Symbol.asyncIterator]()
47
- }
48
- }
package/EventBase.ts DELETED
@@ -1,23 +0,0 @@
1
- import { attachCustomInspect } from "./util/attachCustomInspect.ts"
2
-
3
- export interface EventBase<B extends symbol = symbol, K extends string = string> {
4
- readonly brand: B
5
- readonly type: K
6
- }
7
-
8
- export function EventBase<B extends symbol, K extends string>(brand: B, type: K) {
9
- return class implements EventBase<B, K> {
10
- static is<T>(this: new(...args: any) => T, value: unknown): value is T {
11
- return typeof value === "object" && value !== null
12
- && "brand" in value && value.brand === brand
13
- && "type" in value && value.type === type
14
- }
15
-
16
- readonly brand = brand
17
- readonly type = type
18
-
19
- static {
20
- attachCustomInspect(this, ({ brand: _0, type: _1, ...rest }) => rest)
21
- }
22
- }
23
- }
package/Handler.ts DELETED
@@ -1,4 +0,0 @@
1
- import type { Rune } from "./Rune.ts"
2
- import type { Strand } from "./Strand.ts"
3
-
4
- export type Handler<Y extends Rune<any> = Rune<any>> = (this: Strand, event: Rune.E<Y>) => void
package/L/L.ts DELETED
@@ -1,24 +0,0 @@
1
- export * from "./all.ts"
2
- export * from "./assistant.ts"
3
- export * from "./catch.ts"
4
- export * from "./continuation.ts"
5
- export * from "./emit.ts"
6
- export * from "./focus.ts"
7
- export * from "./infer.ts"
8
- export * from "./message.ts"
9
- export * from "./reflect.ts"
10
- export * from "./run.ts"
11
- export * from "./schema/anyOf.ts"
12
- export * from "./schema/array.ts"
13
- export * from "./schema/boolean.ts"
14
- export * from "./schema/const.ts"
15
- export * from "./schema/enum.ts"
16
- export * from "./schema/integer.ts"
17
- export * from "./schema/null.ts"
18
- export * from "./schema/number"
19
- export * from "./schema/object.ts"
20
- export * from "./schema/string.ts"
21
- export * from "./strand.ts"
22
- export * from "./stream.ts"
23
- export * from "./system.ts"
24
- export * from "./user.ts"
package/L/all.ts DELETED
@@ -1,39 +0,0 @@
1
- import type { Context } from "../Context.ts"
2
- import { Definition } from "../Definition.ts"
3
- import type { LEvent } from "../LEvent.ts"
4
- import type { Rune } from "../Rune.ts"
5
- import { Strand } from "../Strand.ts"
6
- import { continuation } from "./continuation.ts"
7
- import { reflect } from "./reflect.ts"
8
-
9
- /**
10
- * Runs multiple definitions in parallel and collects their results.
11
- * Can be used with an array or object of definitions.
12
- */
13
- export function all<A extends Array<Definition>>(
14
- definitions: A,
15
- context?: Context,
16
- ): Generator<Rune<LEvent> | Definition.Y<A[number]>, { [I in keyof A]: Definition.T<A[I]> }>
17
- export function all<A extends Record<keyof any, Definition>>(
18
- definitions: A,
19
- context?: Context,
20
- ): Generator<Rune<LEvent> | Definition.Y<A[keyof A]>, { [K in keyof A]: Definition.T<A[K]> }>
21
- export function* all<A extends Array<Definition> | Record<keyof any, Definition>>(
22
- definitions: A,
23
- context?: Context,
24
- ): Generator<Rune<any>, any> { // TODO: although it's somewhat irrelevant, type this
25
- const parent = yield* reflect
26
- const strands: Array<Strand> = []
27
- for (const definition of Array.isArray(definitions) ? definitions : Object.values(definitions)) {
28
- strands.push(
29
- new Strand(definition, {
30
- parent,
31
- context: context?.clone() ?? parent.context.clone(),
32
- }),
33
- )
34
- }
35
- const results = yield* continuation("all", () => Promise.all(strands))
36
- if (Array.isArray(strands)) return results
37
- const keys = Object.keys(strands)
38
- return Object.fromEntries(results.map((result, i) => [keys[i], result]))
39
- }
package/L/assistant.ts DELETED
@@ -1,28 +0,0 @@
1
- import type { LEvent } from "../LEvent.ts"
2
- import type { Rune } from "../Rune.ts"
3
- import { Schema } from "../Schema.ts"
4
- import { infer } from "./infer.ts"
5
- import { message } from "./message.ts"
6
-
7
- /**
8
- * Produces an assistant message when yielded. Can be called with a schema
9
- * for structured output. Otherwise produces a string.
10
- */
11
- export interface assistant extends Iterable<Rune<LEvent>, string> {
12
- <T>(schema: Schema<T>): Generator<Rune<LEvent>, T>
13
- }
14
-
15
- export const assistant: assistant = Object.assign(
16
- function* assistant<T>(schema: Schema<T>): Generator<Rune<LEvent>, T> {
17
- const inference = yield* infer(schema)
18
- yield* message("assistant", [{ part: inference }])
19
- return JSON.parse(inference)
20
- },
21
- {
22
- *[Symbol.iterator]() {
23
- const inference = yield* infer()
24
- yield* message("assistant", [{ part: inference }])
25
- return inference
26
- },
27
- },
28
- )
package/L/catch.ts DELETED
@@ -1,38 +0,0 @@
1
- import type { Definition } from "../Definition.ts"
2
- import type { LEvent } from "../LEvent.ts"
3
- import type { Rune } from "../Rune.ts"
4
- import { Strand } from "../Strand.ts"
5
- import { continuation } from "./continuation.ts"
6
- import { reflect } from "./reflect.ts"
7
-
8
- export { catch_ as catch }
9
-
10
- /**
11
- * Error handling utility that catches exceptions from running a definition.
12
- * Returns a result object indicating whether the operation succeeded or failed.
13
- */
14
- function* catch_<Y extends Rune<any>, T>(
15
- definition: Definition<Y, T>,
16
- ): Generator<Rune<LEvent> | Rune<Y>, CatchResult<T>> {
17
- const parent = yield* reflect
18
- return yield* continuation("catch", async () => {
19
- try {
20
- const resolved = await new Strand(definition, {
21
- parent,
22
- context: parent.context.clone(),
23
- }).then()
24
- return { resolved }
25
- } catch (exception: unknown) {
26
- return { rejected: exception }
27
- }
28
- })
29
- }
30
- Object.defineProperty(catch_, "name", { value: "catch" })
31
-
32
- export type CatchResult<T> = {
33
- resolved: T
34
- rejected?: never
35
- } | {
36
- resolved?: never
37
- rejected: unknown
38
- }
package/L/continuation.ts DELETED
@@ -1,14 +0,0 @@
1
- import type { LEvent } from "../LEvent.ts"
2
- import { Rune, RuneKey } from "../Rune.ts"
3
-
4
- /** Yields a continuation rune that executes a function and passes its result to the next iteration. */
5
- export function* continuation<R>(debug: string, f: () => R): Generator<Rune<LEvent>, Awaited<R>> {
6
- return yield {
7
- [RuneKey]: true,
8
- instruction: {
9
- kind: "continue",
10
- debug,
11
- f,
12
- },
13
- }
14
- }
package/L/emit.ts DELETED
@@ -1,16 +0,0 @@
1
- import { Rune, RuneKey } from "../Rune.ts"
2
- import type { EnsureNarrow } from "../util/EnsureNarrow.ts"
3
-
4
- /**
5
- * Emits an event to be supplied to handlers.
6
- * Used for broadcasting state changes and triggering side effects.
7
- */
8
- export function* emit<E>(event: EnsureNarrow<E>): Generator<Rune<E>, void> {
9
- return yield {
10
- [RuneKey]: true,
11
- instruction: {
12
- kind: "emit",
13
- event,
14
- },
15
- }
16
- }
package/L/focus.ts DELETED
@@ -1,17 +0,0 @@
1
- import type { Adapter } from "../Adapter.ts"
2
- import type { LEvent } from "../LEvent.ts"
3
- import { AdapterFocused } from "../LEvent.ts"
4
- import type { Rune } from "../Rune.ts"
5
- import { emit } from "./emit.ts"
6
- import { reflect } from "./reflect.ts"
7
-
8
- /**
9
- * Registers a model in the current context and emits a model registration event.
10
- * Returns the registered model instance.
11
- */
12
- export function* focus(adapter: Adapter): Generator<Rune<LEvent>, Adapter> {
13
- const { context: { adapters } } = yield* reflect
14
- adapters.register(adapter)
15
- yield* emit(new AdapterFocused(adapter))
16
- return adapter
17
- }
package/L/infer.ts DELETED
@@ -1,34 +0,0 @@
1
- import { InferenceRequested, Inferred, type LEvent } from "../LEvent.ts"
2
- import type { Rune } from "../Rune.ts"
3
- import { Schema } from "../Schema.ts"
4
- import { continuation } from "./continuation.ts"
5
- import { emit } from "./emit.ts"
6
- import { reflect } from "./reflect.ts"
7
-
8
- /**
9
- * Requests an inference from the current model, optionally with a schema.
10
- * Emits inference-related events and returns the model's response as a string.
11
- */
12
- export function* infer(schema?: Schema): Generator<Rune<LEvent>, string> {
13
- const { context: { adapters: models, messages }, signal } = yield* reflect
14
- const model = models.ensure()
15
- const requestId = crypto.randomUUID()
16
- yield* emit(new InferenceRequested(requestId, schema))
17
- let inference = yield* continuation(
18
- "infer",
19
- model
20
- .seal({
21
- messages,
22
- ...schema && {
23
- schema: schema.type === "object" ? schema : Schema.wrap(schema),
24
- },
25
- signal,
26
- })
27
- .resolve,
28
- )
29
- if (schema?.type && schema.type !== "object") {
30
- inference = JSON.stringify(JSON.parse(inference).value)
31
- }
32
- yield* emit(new Inferred(requestId, inference))
33
- return inference
34
- }