liminal 0.5.12 → 0.5.14

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 (225) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/Config.ts +13 -0
  3. package/Context.ts +23 -49
  4. package/Definition.ts +32 -0
  5. package/EventBase.ts +1 -1
  6. package/Handler.ts +3 -5
  7. package/L/L.ts +15 -2
  8. package/L/all.ts +35 -0
  9. package/L/assistant.ts +4 -7
  10. package/L/catch.ts +16 -13
  11. package/L/context.ts +12 -0
  12. package/L/continuation.ts +13 -0
  13. package/L/emit.ts +10 -8
  14. package/L/infer.ts +22 -25
  15. package/L/message.ts +7 -9
  16. package/L/model.ts +6 -6
  17. package/L/reflect.ts +12 -0
  18. package/L/run.ts +29 -0
  19. package/L/schema/_schema_common.ts +29 -0
  20. package/L/schema/anyOf.ts +10 -0
  21. package/L/schema/array.ts +13 -0
  22. package/L/schema/boolean.ts +8 -0
  23. package/L/schema/const.ts +18 -0
  24. package/L/schema/enum.ts +18 -0
  25. package/L/schema/integer.ts +6 -0
  26. package/L/schema/null.ts +8 -0
  27. package/L/schema/number.ts +8 -0
  28. package/L/schema/object.ts +21 -0
  29. package/L/schema/string.ts +11 -0
  30. package/L/strand.ts +14 -71
  31. package/L/stream.ts +11 -24
  32. package/L/system.ts +13 -8
  33. package/L/user.ts +13 -8
  34. package/LEvent.ts +9 -17
  35. package/LiminalAssertionError.ts +19 -0
  36. package/Message.ts +3 -2
  37. package/Model.ts +5 -3
  38. package/ModelRegistry.ts +1 -7
  39. package/Rune.test.ts +5 -0
  40. package/Rune.ts +24 -12
  41. package/Schema.ts +185 -0
  42. package/Strand.ts +253 -0
  43. package/Tool.ts +8 -16
  44. package/TypeAdapter.ts +3 -0
  45. package/dist/Config.d.ts +12 -0
  46. package/dist/Config.js +2 -0
  47. package/dist/Config.js.map +1 -0
  48. package/dist/Context.d.ts +11 -12
  49. package/dist/Context.js +15 -42
  50. package/dist/Context.js.map +1 -1
  51. package/dist/Definition.d.ts +10 -0
  52. package/dist/Definition.js +18 -0
  53. package/dist/Definition.js.map +1 -0
  54. package/dist/EventBase.js +1 -1
  55. package/dist/EventBase.js.map +1 -1
  56. package/dist/Handler.d.ts +3 -4
  57. package/dist/Handler.js +1 -2
  58. package/dist/Handler.js.map +1 -1
  59. package/dist/L/L.d.ts +15 -2
  60. package/dist/L/L.js +15 -2
  61. package/dist/L/L.js.map +1 -1
  62. package/dist/L/all.d.ts +10 -0
  63. package/dist/L/all.js +20 -0
  64. package/dist/L/all.js.map +1 -0
  65. package/dist/L/assistant.d.ts +2 -2
  66. package/dist/L/assistant.js +3 -6
  67. package/dist/L/assistant.js.map +1 -1
  68. package/dist/L/catch.d.ts +3 -2
  69. package/dist/L/catch.js +12 -12
  70. package/dist/L/catch.js.map +1 -1
  71. package/dist/L/context.d.ts +2 -0
  72. package/dist/L/context.js +12 -0
  73. package/dist/L/context.js.map +1 -0
  74. package/dist/L/continuation.d.ts +3 -0
  75. package/dist/L/continuation.js +12 -0
  76. package/dist/L/continuation.js.map +1 -0
  77. package/dist/L/emit.d.ts +3 -5
  78. package/dist/L/emit.js +8 -3
  79. package/dist/L/emit.js.map +1 -1
  80. package/dist/L/infer.d.ts +2 -5
  81. package/dist/L/infer.js +21 -20
  82. package/dist/L/infer.js.map +1 -1
  83. package/dist/L/message.d.ts +3 -5
  84. package/dist/L/message.js +5 -5
  85. package/dist/L/message.js.map +1 -1
  86. package/dist/L/model.d.ts +2 -4
  87. package/dist/L/model.js +4 -3
  88. package/dist/L/model.js.map +1 -1
  89. package/dist/L/reflect.d.ts +4 -0
  90. package/dist/L/reflect.js +10 -0
  91. package/dist/L/reflect.js.map +1 -0
  92. package/dist/L/run.d.ts +15 -0
  93. package/dist/L/run.js +16 -0
  94. package/dist/L/run.js.map +1 -0
  95. package/dist/L/schema/_schema_common.d.ts +6 -0
  96. package/dist/L/schema/_schema_common.js +19 -0
  97. package/dist/L/schema/_schema_common.js.map +1 -0
  98. package/dist/L/schema/anyOf.d.ts +5 -0
  99. package/dist/L/schema/anyOf.js +5 -0
  100. package/dist/L/schema/anyOf.js.map +1 -0
  101. package/dist/L/schema/array.d.ts +5 -0
  102. package/dist/L/schema/array.js +8 -0
  103. package/dist/L/schema/array.js.map +1 -0
  104. package/dist/L/schema/boolean.d.ts +6 -0
  105. package/dist/L/schema/boolean.js +4 -0
  106. package/dist/L/schema/boolean.js.map +1 -0
  107. package/dist/L/schema/const.d.ts +8 -0
  108. package/dist/L/schema/const.js +10 -0
  109. package/dist/L/schema/const.js.map +1 -0
  110. package/dist/L/schema/enum.d.ts +8 -0
  111. package/dist/L/schema/enum.js +10 -0
  112. package/dist/L/schema/enum.js.map +1 -0
  113. package/dist/L/schema/integer.d.ts +5 -0
  114. package/dist/L/schema/integer.js +3 -0
  115. package/dist/L/schema/integer.js.map +1 -0
  116. package/dist/L/schema/null.d.ts +6 -0
  117. package/dist/L/schema/null.js +4 -0
  118. package/dist/L/schema/null.js.map +1 -0
  119. package/dist/L/schema/number.d.ts +6 -0
  120. package/dist/L/schema/number.js +4 -0
  121. package/dist/L/schema/number.js.map +1 -0
  122. package/dist/L/schema/object.d.ts +9 -0
  123. package/dist/L/schema/object.js +12 -0
  124. package/dist/L/schema/object.js.map +1 -0
  125. package/dist/L/schema/string.d.ts +8 -0
  126. package/dist/L/schema/string.js +4 -0
  127. package/dist/L/schema/string.js.map +1 -0
  128. package/dist/L/strand.d.ts +4 -15
  129. package/dist/L/strand.js +8 -53
  130. package/dist/L/strand.js.map +1 -1
  131. package/dist/L/stream.d.ts +2 -4
  132. package/dist/L/stream.js +8 -19
  133. package/dist/L/stream.js.map +1 -1
  134. package/dist/L/system.d.ts +2 -4
  135. package/dist/L/system.js +4 -3
  136. package/dist/L/system.js.map +1 -1
  137. package/dist/L/user.d.ts +2 -4
  138. package/dist/L/user.js +4 -3
  139. package/dist/L/user.js.map +1 -1
  140. package/dist/LEvent.d.ts +13 -41
  141. package/dist/LEvent.js +4 -15
  142. package/dist/LEvent.js.map +1 -1
  143. package/dist/LiminalAssertionError.d.ts +8 -0
  144. package/dist/LiminalAssertionError.js +20 -0
  145. package/dist/LiminalAssertionError.js.map +1 -0
  146. package/dist/Message.d.ts +3 -2
  147. package/dist/Model.d.ts +4 -2
  148. package/dist/Model.js +1 -1
  149. package/dist/Model.js.map +1 -1
  150. package/dist/ModelRegistry.d.ts +0 -2
  151. package/dist/ModelRegistry.js +0 -2
  152. package/dist/ModelRegistry.js.map +1 -1
  153. package/dist/Rune.d.ts +19 -7
  154. package/dist/Rune.js +8 -4
  155. package/dist/Rune.js.map +1 -1
  156. package/dist/Rune.test.d.ts +1 -0
  157. package/dist/Rune.test.js +5 -0
  158. package/dist/Rune.test.js.map +1 -0
  159. package/dist/Schema.d.ts +46 -0
  160. package/dist/Schema.js +130 -0
  161. package/dist/Schema.js.map +1 -0
  162. package/dist/Strand.d.ts +57 -0
  163. package/dist/Strand.js +177 -0
  164. package/dist/Strand.js.map +1 -0
  165. package/dist/Tool.d.ts +6 -5
  166. package/dist/Tool.js +3 -4
  167. package/dist/Tool.js.map +1 -1
  168. package/dist/TypeAdapter.d.ts +1 -0
  169. package/dist/TypeAdapter.js +3 -0
  170. package/dist/TypeAdapter.js.map +1 -0
  171. package/dist/errors.d.ts +9 -0
  172. package/dist/errors.js +11 -0
  173. package/dist/errors.js.map +1 -0
  174. package/dist/index.d.ts +6 -5
  175. package/dist/index.js +6 -5
  176. package/dist/index.js.map +1 -1
  177. package/dist/tsconfig.tsbuildinfo +1 -1
  178. package/dist/util/EnsureNarrow.d.ts +1 -0
  179. package/dist/util/EnsureNarrow.js +2 -0
  180. package/dist/util/EnsureNarrow.js.map +1 -0
  181. package/dist/util/JSONValue.d.ts +8 -0
  182. package/dist/util/JSONValue.js +20 -0
  183. package/dist/util/JSONValue.js.map +1 -0
  184. package/dist/util/attachCustomInspect.d.ts +1 -0
  185. package/dist/util/attachCustomInspect.js +11 -0
  186. package/dist/util/attachCustomInspect.js.map +1 -0
  187. package/dist/util/isTemplateStringsArray.d.ts +1 -0
  188. package/dist/util/isTemplateStringsArray.js +4 -0
  189. package/dist/util/isTemplateStringsArray.js.map +1 -0
  190. package/errors.ts +12 -0
  191. package/index.ts +6 -5
  192. package/package.json +3 -10
  193. package/tsconfig.json +1 -5
  194. package/util/EnsureNarrow.ts +1 -0
  195. package/util/JSONValue.ts +20 -0
  196. package/util/attachCustomInspect.ts +14 -0
  197. package/util/isTemplateStringsArray.ts +3 -0
  198. package/Fiber.ts +0 -123
  199. package/L/_common.ts +0 -6
  200. package/L/rune.ts +0 -12
  201. package/MessageRegistry.ts +0 -22
  202. package/Runic.ts +0 -30
  203. package/StrandConfig.ts +0 -12
  204. package/ToolRegistry.ts +0 -10
  205. package/dist/Fiber.d.ts +0 -35
  206. package/dist/Fiber.js +0 -95
  207. package/dist/Fiber.js.map +0 -1
  208. package/dist/L/_common.d.ts +0 -4
  209. package/dist/L/_common.js +0 -7
  210. package/dist/L/_common.js.map +0 -1
  211. package/dist/L/rune.d.ts +0 -3
  212. package/dist/L/rune.js +0 -9
  213. package/dist/L/rune.js.map +0 -1
  214. package/dist/MessageRegistry.d.ts +0 -9
  215. package/dist/MessageRegistry.js +0 -15
  216. package/dist/MessageRegistry.js.map +0 -1
  217. package/dist/Runic.d.ts +0 -9
  218. package/dist/Runic.js +0 -18
  219. package/dist/Runic.js.map +0 -1
  220. package/dist/StrandConfig.d.ts +0 -11
  221. package/dist/StrandConfig.js +0 -2
  222. package/dist/StrandConfig.js.map +0 -1
  223. package/dist/ToolRegistry.d.ts +0 -6
  224. package/dist/ToolRegistry.js +0 -8
  225. package/dist/ToolRegistry.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # liminal
2
2
 
3
+ ## 0.5.14
4
+
5
+ ### Patch Changes
6
+
7
+ - d12049c: Fix L.run return type. Add L.<type> for JSON-schema building.
8
+
9
+ ## 0.5.13
10
+
11
+ ### Patch Changes
12
+
13
+ - bbd0e74: Liminal schema and util now exist within the liminal package itself. Runtime type compatibility changes.
14
+
3
15
  ## 0.5.12
4
16
 
5
17
  ### Patch Changes
package/Config.ts ADDED
@@ -0,0 +1,13 @@
1
+ import type { Message } from "./Message.ts"
2
+ import { ModelRegistry } from "./ModelRegistry.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?: ModelRegistry | undefined
10
+ messages?: Array<Message>
11
+ tools?: Set<Tool> | undefined
12
+ signal?: AbortSignal | undefined
13
+ }
package/Context.ts CHANGED
@@ -1,58 +1,32 @@
1
- import { assert } from "liminal-util"
2
- import { AsyncLocalStorage } from "node:async_hooks"
1
+ import type { Handler } from "./Handler.ts"
2
+ import type { Message } from "./Message.ts"
3
+ import { ModelRegistry } from "./ModelRegistry.ts"
4
+ import type { Tool } from "./Tool.ts"
3
5
 
4
- const storage = new AsyncLocalStorage<Context>()
6
+ export interface Context {
7
+ readonly handler: Handler | undefined
8
+ readonly models: ModelRegistry
9
+ readonly messages: Array<Message>
10
+ readonly tools: Set<Tool>
5
11
 
6
- export class Context extends Map<ContextPart, unknown> {
7
- static get(): Context | undefined {
8
- return storage.getStore()
9
- }
10
-
11
- static ensure(): Context {
12
- const current = Context.get()
13
- assert(current)
14
- return current
15
- }
16
-
17
- run<R>(f: () => R): R {
18
- return storage.run(this, f)
19
- }
20
-
21
- fork(): Context {
22
- const context = new Context()
23
- for (const [handle, value] of this.entries()) {
24
- if (!context.has(handle)) {
25
- context.set(handle, handle.fork(value))
26
- }
27
- }
28
- return context
29
- }
12
+ clone(): Context
30
13
  }
31
14
 
32
- export interface ContextPart<V = any> {
33
- fork(parent?: V): V
34
- get(): V | undefined
35
- getOrInit(): V
36
- debug?: string
37
- }
15
+ export function Context(context?: Omit<Context, "clone">): Context {
16
+ return {
17
+ handler: context?.handler,
18
+ models: context?.models.clone() ?? new ModelRegistry(),
19
+ messages: [],
20
+ tools: new Set(context?.tools),
38
21
 
39
- export function ContextPart<V>(fork: (parent?: V) => V, debug?: string): ContextPart<V> {
40
- const self: ContextPart<V> = {
41
- fork,
42
- get() {
43
- return Context.get()?.get(self) as never
44
- },
45
- getOrInit() {
46
- const context = Context.get()
47
- assert(context)
48
- let value = context.get(this)
49
- if (!value) {
50
- value = this.fork()
51
- context.set(this, value)
22
+ clone(): Context {
23
+ return {
24
+ handler: this.handler,
25
+ models: this.models.clone(),
26
+ messages: [...this.messages],
27
+ tools: new Set(this.tools),
28
+ clone: this.clone,
52
29
  }
53
- return value as never
54
30
  },
55
- ...debug && { debug },
56
31
  }
57
- return self
58
32
  }
package/Definition.ts ADDED
@@ -0,0 +1,32 @@
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
+ export type RuneIterable<Y extends Rune<any> = Rune<any>, T = any> = Iterable<Y, T> | AsyncIterable<Y, T>
5
+ export type Definition<Y extends Rune<any> = Rune<any>, T = any> = RuneIterable<Y, T> | (() => RuneIterable<Y, T>)
6
+
7
+ export namespace Definition {
8
+ export type Y<X extends Definition> = X extends RuneIterable<infer Y> ? Y
9
+ : X extends () => RuneIterable<infer Y> ? Y
10
+ : X extends RuneIterator<infer Y> ? Y
11
+ : never
12
+
13
+ export type T<X extends Definition> = X extends RuneIterable<Rune<any>, infer T> ? T
14
+ : X extends () => RuneIterable<Rune<any>, infer T> ? T
15
+ : X extends RuneIterator<Rune<any>, infer T> ? T
16
+ : never
17
+
18
+ export type E<X extends Definition> = Rune.E<Y<X>>
19
+
20
+ export function unwrap<Y extends Rune<any>, T>(definition: Definition<Y, T>): RuneIterator<Y, T> {
21
+ if (Symbol.iterator in definition) {
22
+ return definition[Symbol.iterator]()
23
+ } else if (Symbol.asyncIterator in definition) {
24
+ return definition[Symbol.asyncIterator]()
25
+ }
26
+ const iterable = definition()
27
+ if (Symbol.iterator in iterable) {
28
+ return iterable[Symbol.iterator]()
29
+ }
30
+ return iterable[Symbol.asyncIterator]()
31
+ }
32
+ }
package/EventBase.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { attachCustomInspect } from "liminal-util"
1
+ import { attachCustomInspect } from "./util/attachCustomInspect.ts"
2
2
 
3
3
  export interface EventBase<B extends symbol = symbol, K extends string = string> {
4
4
  readonly brand: B
package/Handler.ts CHANGED
@@ -1,6 +1,4 @@
1
- import { ContextPart as ContextPart } from "./Context.ts"
2
- import type { Fiber } from "./Fiber.ts"
1
+ import type { Rune } from "./Rune.ts"
2
+ import type { Strand } from "./Strand.ts"
3
3
 
4
- export type Handler<E = any> = [(this: Fiber, event: E) => void][0]
5
-
6
- export const HandlerContext: ContextPart<Handler | undefined> = ContextPart((parent) => parent, "handler")
4
+ export type Handler<Y extends Rune<any> = Rune<any>> = (this: Strand, event: Rune.E<Y>) => void
package/L/L.ts CHANGED
@@ -1,11 +1,24 @@
1
- export * from "liminal-schema/factories"
1
+ export * from "./all.ts"
2
2
  export * from "./assistant.ts"
3
3
  export * from "./catch.ts"
4
+ export * from "./context.ts"
5
+ export * from "./continuation.ts"
4
6
  export * from "./emit.ts"
5
7
  export * from "./infer.ts"
6
8
  export * from "./message.ts"
7
9
  export * from "./model.ts"
8
- export * from "./rune.ts"
10
+ export * from "./reflect.ts"
11
+ export * from "./run.ts"
12
+ export * from "./schema/anyOf.ts"
13
+ export * from "./schema/array.ts"
14
+ export * from "./schema/boolean.ts"
15
+ export * from "./schema/const.ts"
16
+ export * from "./schema/enum.ts"
17
+ export * from "./schema/integer.ts"
18
+ export * from "./schema/null.ts"
19
+ export * from "./schema/number"
20
+ export * from "./schema/object.ts"
21
+ export * from "./schema/string.ts"
9
22
  export * from "./strand.ts"
10
23
  export * from "./stream.ts"
11
24
  export * from "./system.ts"
package/L/all.ts ADDED
@@ -0,0 +1,35 @@
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
+ export function all<A extends Array<Definition>>(
10
+ definitions: A,
11
+ context?: Context,
12
+ ): Generator<Rune<LEvent> | Definition.Y<A[number]>, { [I in keyof A]: Definition.T<A[I]> }>
13
+ export function all<A extends Record<keyof any, Definition>>(
14
+ definitions: A,
15
+ context?: Context,
16
+ ): Generator<Rune<LEvent> | Definition.Y<A[keyof A]>, { [K in keyof A]: Definition.T<A[K]> }>
17
+ export function* all<A extends Array<Definition> | Record<keyof any, Definition>>(
18
+ definitions: A,
19
+ context?: Context,
20
+ ): Generator<Rune<any>, any> { // TODO: although it's somewhat irrelevant, type this
21
+ const parent = yield* reflect
22
+ const strands: Array<Strand> = []
23
+ for (const definition of Array.isArray(definitions) ? definitions : Object.values(definitions)) {
24
+ strands.push(
25
+ new Strand(definition, {
26
+ parent,
27
+ context: context?.clone() ?? parent.context.clone(),
28
+ }),
29
+ )
30
+ }
31
+ const results = yield* continuation("all", () => Promise.all(strands))
32
+ if (Array.isArray(strands)) return results
33
+ const keys = Object.keys(strands)
34
+ return Object.fromEntries(results.map((result, i) => [keys[i], result]))
35
+ }
package/L/assistant.ts CHANGED
@@ -1,21 +1,18 @@
1
- import { type LType, toJSONSchema, validate } from "liminal-schema"
2
1
  import type { LEvent } from "../LEvent.ts"
3
2
  import type { Rune } from "../Rune.ts"
3
+ import { Schema } from "../Schema.ts"
4
4
  import { infer } from "./infer.ts"
5
5
  import { message } from "./message.ts"
6
- import { rune } from "./rune.ts"
7
6
 
8
7
  export interface assistant extends Iterable<Rune<LEvent>, string> {
9
- <T>(type: LType<T>): Generator<Rune<LEvent>, T>
8
+ <T>(schema: Schema<T>): Generator<Rune<LEvent>, T>
10
9
  }
11
10
 
12
11
  export const assistant: assistant = Object.assign(
13
- function*<T>(type: LType<T>): Generator<Rune<LEvent>, T> {
14
- const schema = toJSONSchema(type)
12
+ function* assistant<T>(schema: Schema<T>): Generator<Rune<LEvent>, T> {
15
13
  const inference = yield* infer(schema)
16
14
  yield* message("assistant", [{ part: inference }])
17
- const input = JSON.parse(inference)
18
- return yield* rune(() => validate(type, input), "validate_assistant_message")
15
+ return JSON.parse(inference)
19
16
  },
20
17
  {
21
18
  *[Symbol.iterator]() {
package/L/catch.ts CHANGED
@@ -1,24 +1,27 @@
1
- import { assert } from "liminal-util"
2
- import { Context } from "../Context.ts"
3
- import { Fiber } from "../Fiber.ts"
1
+ import type { Definition } from "../Definition.ts"
2
+ import type { LEvent } from "../LEvent.ts"
4
3
  import type { Rune } from "../Rune.ts"
5
- import type { Runic } from "../Runic.ts"
6
- import { rune } from "./rune.ts"
4
+ import { Strand } from "../Strand.ts"
5
+ import { continuation } from "./continuation.ts"
6
+ import { reflect } from "./reflect.ts"
7
7
 
8
8
  export { catch_ as catch }
9
9
 
10
- function* catch_<Y extends Rune, T>(runic: Runic<Y, T>): Generator<Rune<Y>, CatchResult<T>> {
11
- return yield* rune(async (parent) => {
10
+ function* catch_<Y extends Rune<any>, T>(
11
+ definition: Definition<Y, T>,
12
+ ): Generator<Rune<LEvent> | Rune<Y>, CatchResult<T>> {
13
+ const parent = yield* reflect
14
+ return yield* continuation("catch", async () => {
12
15
  try {
13
- const context = Context.get()
14
- assert(context)
15
- return {
16
- resolved: await context.fork().run(() => new Fiber(runic, { parent }).resolution()),
17
- }
16
+ const resolved = await new Strand(definition, {
17
+ parent,
18
+ context: parent.context.clone(),
19
+ }).then()
20
+ return { resolved }
18
21
  } catch (exception: unknown) {
19
22
  return { rejected: exception }
20
23
  }
21
- }, "catch")
24
+ })
22
25
  }
23
26
  Object.defineProperty(catch_, "name", { value: "catch" })
24
27
 
package/L/context.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { Context } from "../Context.ts"
2
+ import { reflect } from "./reflect.ts"
3
+
4
+ export function* context(partial: Partial<Omit<Context, "clone">>) {
5
+ const { context } = yield* reflect
6
+ return Context({
7
+ handler: partial.handler ?? context.handler,
8
+ messages: partial.messages ?? context.messages,
9
+ models: partial.models ?? context.models,
10
+ tools: partial.tools ?? context.tools,
11
+ })
12
+ }
@@ -0,0 +1,13 @@
1
+ import type { LEvent } from "../LEvent.ts"
2
+ import { Rune, RuneKey } from "../Rune.ts"
3
+
4
+ export function* continuation<R>(debug: string, f: () => R): Generator<Rune<LEvent>, Awaited<R>> {
5
+ return yield {
6
+ [RuneKey]: true,
7
+ value: {
8
+ kind: "continuation",
9
+ debug,
10
+ f,
11
+ },
12
+ }
13
+ }
package/L/emit.ts CHANGED
@@ -1,10 +1,12 @@
1
- import type { EnsureNarrow } from "liminal-util"
2
- import { Fiber } from "../Fiber.ts"
3
- import type { Rune } from "../Rune.ts"
1
+ import { Rune, RuneKey } from "../Rune.ts"
2
+ import type { EnsureNarrow } from "../util/EnsureNarrow.ts"
4
3
 
5
- export interface emit<E> extends Generator<Rune<E>, void> {}
6
-
7
- export function* emit<const E>(event: EnsureNarrow<E>): emit<E> {
8
- const self = yield* Fiber
9
- self.handle(event)
4
+ export function* emit<E>(event: EnsureNarrow<E>): Generator<Rune<E>, void> {
5
+ return yield {
6
+ [RuneKey]: true,
7
+ value: {
8
+ kind: "event",
9
+ event,
10
+ },
11
+ }
10
12
  }
package/L/infer.ts CHANGED
@@ -1,33 +1,30 @@
1
- import type { SchemaObject } from "liminal-schema"
2
- import { assert } from "liminal-util"
3
1
  import { InferenceRequested, Inferred, type LEvent } from "../LEvent.ts"
4
- import { MessageRegistryContext } from "../MessageRegistry.ts"
5
- import { ModelRegistryContext } from "../ModelRegistry.ts"
2
+ import { LiminalAssertionError } from "../LiminalAssertionError.ts"
6
3
  import type { Rune } from "../Rune.ts"
7
- import { RequestCounter } from "./_common.ts"
4
+ import { Schema } from "../Schema.ts"
5
+ import { continuation } from "./continuation.ts"
8
6
  import { emit } from "./emit.ts"
9
- import { rune } from "./rune.ts"
7
+ import { reflect } from "./reflect.ts"
10
8
 
11
- export { infer_ as infer }
12
-
13
- interface infer_ extends Generator<Rune<LEvent>, string> {}
14
-
15
- function* infer_(schema?: SchemaObject): infer_ {
16
- const modelRegistry = ModelRegistryContext.getOrInit()
17
- const model = modelRegistry.peek()
18
- assert(model)
19
- const requestId = RequestCounter.next()
9
+ export function* infer(schema?: Schema): Generator<Rune<LEvent>, string> {
10
+ const { context: { models, messages }, signal } = yield* reflect
11
+ const model = models.peek()
12
+ LiminalAssertionError.assert(model)
13
+ const requestId = crypto.randomUUID()
20
14
  yield* emit(new InferenceRequested(requestId, schema))
21
- const messageRegistry = MessageRegistryContext.getOrInit()
22
- const inference = yield* rune((fiber) =>
23
- model
24
- .seal({
25
- messages: messageRegistry.messages,
26
- schema,
27
- signal: fiber.controller.signal,
28
- })
29
- .resolve(), "infer")
15
+ let inference = yield* continuation("infer", () =>
16
+ model.seal({
17
+ messages,
18
+ ...schema && {
19
+ schema: schema.type === "object"
20
+ ? schema
21
+ : Schema.wrap(schema),
22
+ },
23
+ signal,
24
+ }).resolve())
25
+ if (schema?.type && schema.type !== "object") {
26
+ inference = JSON.stringify(JSON.parse(inference).value)
27
+ }
30
28
  yield* emit(new Inferred(requestId, inference))
31
29
  return inference
32
30
  }
33
- Object.defineProperty(infer_, "name", { value: "infer" })
package/L/message.ts CHANGED
@@ -1,14 +1,12 @@
1
- import { type LEvent, MessageAppended } from "../LEvent.ts"
2
- import type { ContentPart, Message, MessageRole } from "../Message.ts"
3
- import { MessageRegistryContext } from "../MessageRegistry.ts"
1
+ import { LEvent, MessageAppended } from "../LEvent.ts"
2
+ import type { Content, Message, MessageRole } from "../Message.ts"
4
3
  import type { Rune } from "../Rune.ts"
5
4
  import { emit } from "./emit.ts"
5
+ import { reflect } from "./reflect.ts"
6
6
 
7
- export interface message extends Generator<Rune<LEvent>, void> {}
8
-
9
- export function* message(role: MessageRole, content: Array<ContentPart>): message {
10
- const messageRegistry = MessageRegistryContext.getOrInit()
11
- const message: Message = { role, content }
7
+ export function* message(role: MessageRole, content: Array<Content>): Generator<Rune<LEvent>, void> {
8
+ const { context: { messages } } = yield* reflect
9
+ const message: Message = { role, parts: content }
12
10
  yield* emit(new MessageAppended(message))
13
- messageRegistry.append(message)
11
+ messages.push(message)
14
12
  }
package/L/model.ts CHANGED
@@ -1,13 +1,13 @@
1
+ import type { LEvent } from "../LEvent.ts"
1
2
  import { ModelRegistered } from "../LEvent.ts"
2
3
  import type { Model } from "../Model.ts"
3
- import { ModelRegistryContext } from "../ModelRegistry.ts"
4
4
  import type { Rune } from "../Rune.ts"
5
5
  import { emit } from "./emit.ts"
6
+ import { reflect } from "./reflect.ts"
6
7
 
7
- export interface model extends Generator<Rune<ModelRegistered>, void> {}
8
-
9
- export function* model(model: Model): model {
10
- const registry = ModelRegistryContext.getOrInit()
11
- registry.register(model)
8
+ export function* model(model: Model): Generator<Rune<LEvent>, Model> {
9
+ const { context: { models } } = yield* reflect
10
+ models.register(model)
12
11
  yield* emit(new ModelRegistered(model))
12
+ return model
13
13
  }
package/L/reflect.ts ADDED
@@ -0,0 +1,12 @@
1
+ import type { LEvent } from "../LEvent.ts"
2
+ import { Rune, RuneKey } from "../Rune.ts"
3
+ import type { Strand } from "../Strand.ts"
4
+
5
+ export const reflect: Iterable<Rune<LEvent>, Strand> = {
6
+ *[Symbol.iterator]() {
7
+ return yield {
8
+ [RuneKey]: true,
9
+ value: { kind: "reflect" },
10
+ }
11
+ },
12
+ }
package/L/run.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { Context } from "../Context.ts"
2
+ import type { Definition } from "../Definition.ts"
3
+ import type { Handler } from "../Handler.ts"
4
+ import type { Message } from "../Message.ts"
5
+ import { ModelRegistry } from "../ModelRegistry.ts"
6
+ import type { Rune } from "../Rune.ts"
7
+ import { Strand } from "../Strand.ts"
8
+ import type { Tool } from "../Tool.ts"
9
+
10
+ export interface RunConfig<Y extends Rune<any>> {
11
+ handler?: Handler<Y> | undefined
12
+ models?: ModelRegistry | undefined
13
+ messages?: Array<Message> | undefined
14
+ tools?: Set<Tool> | undefined
15
+ signal?: AbortSignal | undefined
16
+ }
17
+
18
+ export function run<Y extends Rune<any>, T>(definition: Definition<Y, T>, config?: RunConfig<Y>): Strand<Y, T> {
19
+ const context = Context({
20
+ handler: config?.handler,
21
+ models: config?.models ?? new ModelRegistry(),
22
+ messages: config?.messages ?? [],
23
+ tools: config?.tools ?? new Set(),
24
+ })
25
+ return new Strand(definition, {
26
+ signal: config?.signal,
27
+ ...config && { context },
28
+ })
29
+ }
@@ -0,0 +1,29 @@
1
+ import type { Schema } from "../../Schema.ts"
2
+ import { isTemplateStringsArray } from "../../util/isTemplateStringsArray.ts"
3
+
4
+ export function make<S extends Schema>(schema: Omit<S, "T">, description?: string): S & TypeBase {
5
+ const schema_ = {
6
+ ...schema,
7
+ ...description && { description },
8
+ }
9
+
10
+ const Type = Object.assign(function describe(e0?: TemplateStringsArray | string, ...rest: Array<string>) {
11
+ const junction = isTemplateStringsArray(e0) ? String.raw(e0, ...rest) : e0
12
+ return make(
13
+ schema_,
14
+ description ? `${description}${junction ? `\n\n${junction}` : ""}` : junction,
15
+ )
16
+ }, schema_)
17
+ Object.defineProperty(Type, "toJSON", {
18
+ value() {
19
+ return schema_
20
+ },
21
+ enumerable: false,
22
+ })
23
+ return Type as never
24
+ }
25
+
26
+ export interface TypeBase {
27
+ (template: TemplateStringsArray, ...substitutions: Array<string>): this
28
+ (...values: Array<string>): this
29
+ }
@@ -0,0 +1,10 @@
1
+ import type { Schema, SchemaAnyOf } from "../../Schema.ts"
2
+ import { make } from "./_schema_common.ts"
3
+
4
+ export interface anyOf<XA extends Array<Schema> = Array<Schema>> extends SchemaAnyOf<XA[number]["T"]> {
5
+ anyOf: XA
6
+ }
7
+
8
+ export function anyOf<XA extends Array<Schema>>(anyOf: XA): anyOf<XA> {
9
+ return make({ anyOf })
10
+ }
@@ -0,0 +1,13 @@
1
+ import type { Schema, SchemaArray } from "../../Schema.ts"
2
+ import { make } from "./_schema_common.ts"
3
+
4
+ export interface LArray<X extends Schema = Schema> extends SchemaArray<Array<X["T"]>> {
5
+ items: X
6
+ }
7
+
8
+ export function array<X extends Schema>(items: X): LArray<X> {
9
+ return make({
10
+ type: "array",
11
+ items,
12
+ })
13
+ }
@@ -0,0 +1,8 @@
1
+ import type { SchemaBoolean } from "../../Schema.ts"
2
+ import { make, type TypeBase } from "./_schema_common.ts"
3
+
4
+ interface boolean_ extends SchemaBoolean<boolean>, TypeBase {}
5
+
6
+ const boolean_: boolean_ = make({ type: "boolean" })
7
+
8
+ export { boolean_ as boolean }
@@ -0,0 +1,18 @@
1
+ import type { SchemaString } from "../../Schema.ts"
2
+ import { make, type TypeBase } from "./_schema_common.ts"
3
+
4
+ interface const_<K extends string> extends SchemaString<K>, TypeBase {
5
+ enum?: never
6
+ const: K
7
+ }
8
+
9
+ function const_<K extends string>(value: K): const_<K> {
10
+ return make({
11
+ type: "string",
12
+ const: value,
13
+ })
14
+ }
15
+
16
+ Object.defineProperty(const_, "name", { value: "const" })
17
+
18
+ export { const_ as const }
@@ -0,0 +1,18 @@
1
+ import type { SchemaString } from "../../Schema.ts"
2
+ import { make, type TypeBase } from "./_schema_common.ts"
3
+
4
+ interface enum_<V extends Array<string> = Array<string>> extends SchemaString<V[number]>, TypeBase {
5
+ enum: V
6
+ const?: never
7
+ }
8
+
9
+ function enum_<const V extends Array<string>>(...values: V): enum_<V> {
10
+ return make({
11
+ type: "string",
12
+ enum: values,
13
+ })
14
+ }
15
+
16
+ Object.defineProperty(enum_, "name", { value: "enum" })
17
+
18
+ export { enum_ as enum }
@@ -0,0 +1,6 @@
1
+ import type { SchemaInteger } from "../../Schema.ts"
2
+ import { make, type TypeBase } from "./_schema_common.ts"
3
+
4
+ export interface integer extends SchemaInteger<number>, TypeBase {}
5
+
6
+ export const integer: integer = make({ type: "integer" })
@@ -0,0 +1,8 @@
1
+ import type { SchemaNull } from "../../Schema.ts"
2
+ import { make, type TypeBase } from "./_schema_common.ts"
3
+
4
+ export interface null_ extends SchemaNull<null>, TypeBase {}
5
+
6
+ const null_: null_ = make({ type: "null" })
7
+
8
+ export { null_ as null }
@@ -0,0 +1,8 @@
1
+ import type { SchemaNumber } from "../../Schema.ts"
2
+ import { make, type TypeBase } from "./_schema_common.ts"
3
+
4
+ interface number_ extends SchemaNumber<number>, TypeBase {}
5
+
6
+ const number_: number_ = make({ type: "number" })
7
+
8
+ export { number_ as number }
@@ -0,0 +1,21 @@
1
+ import type { Schema, SchemaObject } from "../../Schema.ts"
2
+ import { make } from "./_schema_common.ts"
3
+
4
+ interface object_<F extends Record<string, Schema> = Record<string, Schema>>
5
+ extends SchemaObject<{ [K in keyof F]: F[K]["T"] }>
6
+ {
7
+ properties: F
8
+ required: Array<Extract<keyof F, string>>
9
+ }
10
+
11
+ function object_<XR extends Record<string, Schema>>(properties: XR): object_<XR> {
12
+ return make({
13
+ type: "object",
14
+ properties,
15
+ additionalProperties: false,
16
+ required: Object.keys(properties) as never,
17
+ })
18
+ }
19
+
20
+ Object.defineProperty(object_, "name", { value: "object" })
21
+ export { object_ as object }