liminal 0.5.3 → 0.5.5

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 (114) hide show
  1. package/Agent.ts +13 -15
  2. package/CHANGELOG.md +15 -0
  3. package/Context.ts +44 -0
  4. package/EventBase.ts +26 -0
  5. package/Globals.ts +1 -1
  6. package/L/L.ts +0 -3
  7. package/L/_infer.ts +26 -22
  8. package/L/_message.ts +4 -7
  9. package/L/assistant.ts +2 -9
  10. package/L/branch.ts +20 -28
  11. package/L/emit.ts +9 -4
  12. package/L/fork.ts +18 -16
  13. package/L/model.ts +2 -2
  14. package/L/rune.ts +1 -2
  15. package/LEvent.ts +32 -19
  16. package/Rune.ts +1 -3
  17. package/dist/Agent.d.ts +2 -6
  18. package/dist/Agent.js +10 -10
  19. package/dist/Agent.js.map +1 -1
  20. package/dist/Context.d.ts +10 -0
  21. package/dist/Context.js +37 -0
  22. package/dist/Context.js.map +1 -0
  23. package/dist/EventBase.d.ts +10 -0
  24. package/dist/EventBase.js +20 -0
  25. package/dist/EventBase.js.map +1 -0
  26. package/dist/Globals.d.ts +0 -1
  27. package/dist/L/L.d.ts +0 -3
  28. package/dist/L/L.js +0 -3
  29. package/dist/L/L.js.map +1 -1
  30. package/dist/L/_infer.d.ts +2 -2
  31. package/dist/L/_infer.js +18 -16
  32. package/dist/L/_infer.js.map +1 -1
  33. package/dist/L/_message.js +4 -7
  34. package/dist/L/_message.js.map +1 -1
  35. package/dist/L/assistant.js +2 -7
  36. package/dist/L/assistant.js.map +1 -1
  37. package/dist/L/branch.js +20 -24
  38. package/dist/L/branch.js.map +1 -1
  39. package/dist/L/emit.js +9 -4
  40. package/dist/L/emit.js.map +1 -1
  41. package/dist/L/fork.d.ts +1 -7
  42. package/dist/L/fork.js +19 -10
  43. package/dist/L/fork.js.map +1 -1
  44. package/dist/L/model.js +2 -2
  45. package/dist/L/model.js.map +1 -1
  46. package/dist/L/rune.d.ts +1 -2
  47. package/dist/L/rune.js.map +1 -1
  48. package/dist/LEvent.d.ts +55 -18
  49. package/dist/LEvent.js +38 -0
  50. package/dist/LEvent.js.map +1 -1
  51. package/dist/Rune.d.ts +1 -2
  52. package/dist/Rune.js.map +1 -1
  53. package/dist/index.d.ts +2 -2
  54. package/dist/index.js +2 -2
  55. package/dist/index.js.map +1 -1
  56. package/dist/run.d.ts +4 -0
  57. package/dist/run.js +35 -0
  58. package/dist/run.js.map +1 -0
  59. package/dist/state/Fiber.d.ts +9 -0
  60. package/dist/state/Fiber.js +14 -0
  61. package/dist/state/Fiber.js.map +1 -0
  62. package/dist/state/Globals.d.ts +9 -0
  63. package/dist/state/Globals.js +10 -0
  64. package/dist/state/Globals.js.map +1 -0
  65. package/dist/state/MessageRegistry.d.ts +1 -1
  66. package/dist/state/MessageRegistry.js +3 -3
  67. package/dist/state/MessageRegistry.js.map +1 -1
  68. package/dist/state/ModelConfig.d.ts +1 -19
  69. package/dist/state/ModelConfig.js +13 -14
  70. package/dist/state/ModelConfig.js.map +1 -1
  71. package/dist/state/ModelRegistry.d.ts +1 -1
  72. package/dist/state/ModelRegistry.js +7 -5
  73. package/dist/state/ModelRegistry.js.map +1 -1
  74. package/dist/state/StateMap.d.ts +1 -7
  75. package/dist/state/StateMap.js +1 -9
  76. package/dist/state/StateMap.js.map +1 -1
  77. package/dist/tsconfig.tsbuildinfo +1 -1
  78. package/index.ts +2 -2
  79. package/package.json +3 -4
  80. package/run.ts +37 -0
  81. package/state/Fiber.ts +13 -0
  82. package/state/Globals.ts +12 -0
  83. package/state/MessageRegistry.ts +4 -4
  84. package/state/ModelConfig.ts +13 -15
  85. package/state/ModelRegistry.ts +10 -8
  86. package/state/StateMap.ts +0 -15
  87. package/Fiber.ts +0 -117
  88. package/L/all.ts +0 -11
  89. package/L/join.ts +0 -9
  90. package/L/self.ts +0 -13
  91. package/L/state.ts +0 -20
  92. package/dist/Fiber.d.ts +0 -38
  93. package/dist/Fiber.js +0 -79
  94. package/dist/Fiber.js.map +0 -1
  95. package/dist/L/all.d.ts +0 -7
  96. package/dist/L/all.js +0 -5
  97. package/dist/L/all.js.map +0 -1
  98. package/dist/L/join.d.ts +0 -5
  99. package/dist/L/join.js +0 -5
  100. package/dist/L/join.js.map +0 -1
  101. package/dist/L/self.d.ts +0 -6
  102. package/dist/L/self.js +0 -7
  103. package/dist/L/self.js.map +0 -1
  104. package/dist/L/state.d.ts +0 -7
  105. package/dist/L/state.js +0 -14
  106. package/dist/L/state.js.map +0 -1
  107. package/dist/state/Counter.d.ts +0 -6
  108. package/dist/state/Counter.js +0 -13
  109. package/dist/state/Counter.js.map +0 -1
  110. package/dist/state/DefaultStateMap.d.ts +0 -4
  111. package/dist/state/DefaultStateMap.js +0 -14
  112. package/dist/state/DefaultStateMap.js.map +0 -1
  113. package/state/Counter.ts +0 -11
  114. package/state/DefaultStateMap.ts +0 -14
package/Agent.ts CHANGED
@@ -1,28 +1,26 @@
1
- import { Fiber, type FiberInfo } from "./Fiber.ts"
1
+ import { Context } from "./Context.ts"
2
+ import { run } from "./run.ts"
2
3
  import type { Rune, RuneKey } from "./Rune.ts"
3
4
  import type { Runic } from "./Runic.ts"
5
+ import { Fiber } from "./state/Fiber.ts"
6
+ import { Globals } from "./state/Globals.ts"
4
7
 
5
8
  export interface Agent<out T, out E> extends PromiseLike<T> {
6
9
  T: T
7
10
  E: E
8
11
  }
9
12
 
10
- export function Agent<Y extends Rune, T>(runic: Runic<Y, T>, config?: AgentConfig<Y, T>): Agent<T, Rune.E<Y>> {
13
+ export function Agent<Y extends Rune, T>(
14
+ runic: Runic<Y, T>,
15
+ globals?: Partial<Globals<Y[RuneKey]>>,
16
+ ): Agent<T, Rune.E<Y>> {
11
17
  return {
12
18
  then(onfulfilled, onrejected) {
13
- const root = Fiber({
14
- globals: {
15
- handler: config?.handler ?? (() => {}),
16
- },
17
- runic,
18
- signal: config?.signal,
19
- })
20
- return root.run().then(onfulfilled, onrejected)
19
+ const context = new Context([
20
+ [Globals.make, Globals.make(globals)],
21
+ [Fiber.make, Fiber.make()],
22
+ ])
23
+ return Context.storage.run(context, () => run(runic).then(onfulfilled, onrejected))
21
24
  },
22
25
  } satisfies Omit<Agent<T, Rune.E<Y>>, "E" | "T"> as never
23
26
  }
24
-
25
- export interface AgentConfig<Y extends Rune, _T> {
26
- handler: (event: Y[RuneKey], info: FiberInfo) => void
27
- signal?: AbortSignal
28
- }
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # liminal
2
2
 
3
+ ## 0.5.5
4
+
5
+ ### Patch Changes
6
+
7
+ - 43afd27: Fixing misc. bugs and refactoring context management.
8
+
9
+ ## 0.5.4
10
+
11
+ ### Patch Changes
12
+
13
+ - 284adab: Reintroduce AI SDK adapter and begin simplifying runic execution.
14
+ - Updated dependencies [284adab]
15
+ - liminal-schema@0.0.2
16
+ - liminal-util@0.0.2
17
+
3
18
  ## 0.5.3
4
19
 
5
20
  ### Patch Changes
package/Context.ts ADDED
@@ -0,0 +1,44 @@
1
+ import { assert } from "liminal-util"
2
+ import { AsyncLocalStorage } from "node:async_hooks"
3
+
4
+ export class Context extends Map<StateFactory, unknown> {
5
+ static storage: AsyncLocalStorage<Context> = new AsyncLocalStorage<Context>()
6
+
7
+ static make(context?: Context): Context {
8
+ const instance = new Context()
9
+ if (context) {
10
+ for (const [key, value] of context.entries()) {
11
+ instance.set(key, key(value))
12
+ }
13
+ }
14
+ return instance
15
+ }
16
+
17
+ static unwrap(): Context {
18
+ const context = this.storage.getStore()
19
+ assert(context)
20
+ return context
21
+ }
22
+
23
+ static get<T>(factory: StateFactory<T>): T | undefined {
24
+ return this.unwrap().get(factory) as never
25
+ }
26
+
27
+ static getAssert<T>(factory: StateFactory<T>): T {
28
+ const value = this.get(factory)
29
+ assert(value)
30
+ return value
31
+ }
32
+
33
+ static getOrInit<T>(factory: StateFactory<T>): T {
34
+ let context = this.unwrap()
35
+ let instance = context.get(factory)
36
+ if (!instance) {
37
+ instance = factory()
38
+ context.set(factory, instance)
39
+ }
40
+ return instance as never
41
+ }
42
+ }
43
+
44
+ export type StateFactory<T = any> = (instance?: T) => T
package/EventBase.ts ADDED
@@ -0,0 +1,26 @@
1
+ import { inspect, type InspectOptions } from "node:util"
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
+ readonly brand = brand
11
+ readonly type = type
12
+
13
+ // Allows us to circumvent a false positive ts error related to private symbols.
14
+ // https://github.com/microsoft/TypeScript/issues/58496
15
+ static {
16
+ Object.defineProperties(this.prototype, {
17
+ [inspect.custom]: {
18
+ value(depth: number, options: InspectOptions) {
19
+ const { brand: _0, type: _1, ...rest } = this
20
+ return `${this.constructor.name} ` + inspect(rest, { ...options, depth })
21
+ },
22
+ },
23
+ })
24
+ }
25
+ }
26
+ }
package/Globals.ts CHANGED
@@ -3,7 +3,7 @@ export interface Globals {
3
3
  }
4
4
 
5
5
  export interface EventInfo {
6
- fiber: number
6
+ // fiber: number
7
7
  timestamp: number
8
8
  // group
9
9
  // depth
package/L/L.ts CHANGED
@@ -5,9 +5,6 @@ export * from "./assistant.ts"
5
5
  export * from "./branch.ts"
6
6
  export * from "./emit.ts"
7
7
  export * from "./fork.ts"
8
- export * from "./join.ts"
9
8
  export * from "./model.ts"
10
- export * from "./self.ts"
11
- export * from "./state.ts"
12
9
  export * from "./system.ts"
13
10
  export * from "./user.ts"
package/L/_infer.ts CHANGED
@@ -1,35 +1,39 @@
1
- import type { SchemaRoot } from "liminal-schema"
1
+ import type { SchemaObject } from "liminal-schema"
2
2
  import { assert } from "liminal-util"
3
- import { type InferenceRequested, type Inferred, type LEvent, LEventTag } from "../LEvent.ts"
3
+ import { Context } from "../Context.ts"
4
+ import { InferenceRequested, Inferred, type LEvent } from "../LEvent.ts"
4
5
  import type { Rune } from "../Rune.ts"
5
- import { Counter } from "../state/Counter.ts"
6
6
  import { MessageRegistry } from "../state/MessageRegistry.ts"
7
7
  import { ModelRegistry } from "../state/ModelRegistry.ts"
8
8
  import { emit } from "./emit.ts"
9
9
  import { rune } from "./rune.ts"
10
- import { state } from "./state.ts"
11
10
 
12
- export function* _infer(schema?: SchemaRoot): Generator<Rune<LEvent>, string> {
13
- const [modelRegistry, { messages }, counter] = yield* state(
14
- ModelRegistry,
15
- MessageRegistry,
16
- InferenceRequestCounter,
17
- )
11
+ export function* _infer(schema?: SchemaObject): Generator<Rune<LEvent>, string> {
12
+ const modelRegistry = Context.getOrInit(ModelRegistry.make)
18
13
  const model = modelRegistry.peek()
19
14
  assert(model)
15
+ const messageRegistry = Context.getOrInit(MessageRegistry.make)
16
+ const counter = Context.getOrInit(InferenceRequestCounter)
20
17
  const requestId = counter.next()
21
- yield* emit<InferenceRequested>({
22
- [LEventTag]: "inference_requested",
23
- ...schema && { schema },
24
- requestId,
25
- })
26
- const inference = yield* rune(() => model.resolve(messages, schema))
27
- yield* emit<Inferred>({
28
- [LEventTag]: "inferred",
29
- inference,
30
- requestId,
31
- })
18
+ yield* emit(new InferenceRequested(requestId, schema))
19
+ const inference = yield* rune(() => model.resolve(messageRegistry.messages, schema))
20
+ yield* emit(new Inferred(requestId, inference))
32
21
  return inference
33
22
  }
34
23
 
35
- class InferenceRequestCounter extends Counter {}
24
+ function InferenceRequestCounter(instance?: Counter) {
25
+ return {
26
+ count: instance?.count ?? 0,
27
+ next() {
28
+ return this.count++
29
+ },
30
+ clone() {
31
+ return InferenceRequestCounter(this)
32
+ },
33
+ }
34
+ }
35
+
36
+ interface Counter {
37
+ count: number
38
+ next(): number
39
+ }
package/L/_message.ts CHANGED
@@ -1,18 +1,15 @@
1
- import { type LEvent, LEventTag, type MessageAppended } from "../LEvent.ts"
1
+ import { Context } from "../Context.ts"
2
+ import { type LEvent, MessageAppended } from "../LEvent.ts"
2
3
  import type { ContentPart, Message, MessageRole } from "../Message.ts"
3
4
  import type { Rune } from "../Rune.ts"
4
5
  import { MessageRegistry } from "../state/MessageRegistry.ts"
5
6
  import { emit } from "./emit.ts"
6
- import { state } from "./state.ts"
7
7
 
8
8
  export interface _message extends Generator<Rune<LEvent>, void> {}
9
9
 
10
10
  export function* _message(role: MessageRole, content: Array<ContentPart>): _message {
11
- const [messageRegistry] = yield* state(MessageRegistry)
11
+ const messageRegistry = Context.getOrInit(MessageRegistry.make)
12
12
  const message: Message = { role, content }
13
- yield* emit<MessageAppended>({
14
- [LEventTag]: "message_appended",
15
- message,
16
- })
13
+ yield* emit(new MessageAppended(message))
17
14
  messageRegistry.append(message)
18
15
  }
package/L/assistant.ts CHANGED
@@ -1,7 +1,4 @@
1
- import type { StandardSchemaV1 } from "@standard-schema/spec"
2
- import { type LType, toJSONSchema } from "liminal-schema"
3
- import type { json } from "liminal-util"
4
- import { LiminalAssertionError } from "liminal-util"
1
+ import { type LType, toJSONSchema, validate } from "liminal-schema"
5
2
  import type { LEvent } from "../LEvent.ts"
6
3
  import type { Rune } from "../Rune.ts"
7
4
  import { _infer } from "./_infer.ts"
@@ -18,11 +15,7 @@ export const assistant: assistant = Object.assign(
18
15
  const inference = yield* _infer(schema)
19
16
  yield* _message("assistant", [{ part: inference }])
20
17
  const input = JSON.parse(inference)
21
- const result = yield* rune(() => (type as StandardSchemaV1<json.ValueObject, T>)["~standard"].validate(input))
22
- if (result.issues) {
23
- throw new LiminalAssertionError(JSON.stringify(result.issues, null, 2))
24
- }
25
- return result.value
18
+ return yield* rune(() => validate(type, input))
26
19
  },
27
20
  {
28
21
  *[Symbol.iterator]() {
package/L/branch.ts CHANGED
@@ -1,10 +1,9 @@
1
- import { Fiber } from "../Fiber.ts"
1
+ import { Context } from "../Context.ts"
2
+ import { run } from "../run.ts"
2
3
  import { type Rune } from "../Rune.ts"
3
4
  import type { Runic } from "../Runic.ts"
4
- import { all } from "./all.ts"
5
- import { fork } from "./fork.ts"
6
- import { join } from "./join.ts"
7
- import { self } from "./self.ts"
5
+ import { Fiber } from "../state/Fiber.ts"
6
+ import { rune } from "./rune.ts"
8
7
 
9
8
  export interface branch<Y extends Rune, T> extends Generator<Y, T> {}
10
9
 
@@ -16,30 +15,23 @@ export function branch<XR extends Record<keyof any, Runic>>(
16
15
  runics: XR,
17
16
  ): branch<Runic.Y<XR[keyof XR]> | Rune<never>, { [K in keyof XR]: Runic.T<XR[K]> }>
18
17
  export function* branch(value: Runic | Array<Runic> | Record<keyof any, Runic>): branch<Rune, any> {
19
- const parent = yield* self
20
- const { globals, state } = parent
18
+ const parent = Context.get(Fiber.make)
21
19
  if (Array.isArray(value)) {
22
- const fibers = value.map((runic) =>
23
- Fiber({
24
- globals,
25
- parent,
26
- runic,
27
- state: state.clone(),
28
- })
29
- )
30
- return yield* join(yield* all(...fibers))
20
+ const runners = value.map((runic) => {
21
+ const context = Context.make(Context.unwrap())
22
+ context.set(Fiber.make, new Fiber(parent))
23
+ return () => run(runic, context)
24
+ })
25
+ return yield* rune(() => Promise.all(runners.map((runner) => runner())))
31
26
  } else if (typeof value === "object") {
32
- const fibers = Object.values(value).map((runic) =>
33
- Fiber({
34
- globals,
35
- parent,
36
- runic,
37
- state: state.clone(),
38
- })
39
- )
40
- const resolved = yield* join(yield* all(...fibers))
41
- return Object.fromEntries(Object.keys(value).map((key, i) => [key, resolved[i]]))
27
+ const runners = Object.entries(value).map(([key, runic]) => {
28
+ const context = Context.make(Context.unwrap())
29
+ context.set(Fiber.make, new Fiber(parent))
30
+ return async () => [key, await run(runic, context)]
31
+ })
32
+ return yield* rune(() => Promise.all(runners.map((runner) => runner())).then(Object.fromEntries))
42
33
  }
43
- const fiber = yield* fork(typeof value === "function" ? value() : value, state.clone())
44
- return yield* join(fiber)
34
+ const context = Context.make(Context.unwrap())
35
+ context.set(Fiber.make, new Fiber(parent))
36
+ return yield* rune(() => run(typeof value === "function" ? value() : value, context))
45
37
  }
package/L/emit.ts CHANGED
@@ -1,11 +1,16 @@
1
1
  import type { EnsureNarrow } from "liminal-util"
2
+ import { Context } from "../Context.ts"
2
3
  import type { Rune } from "../Rune.ts"
3
- import { rune } from "./rune.ts"
4
+ import { Fiber } from "../state/Fiber.ts"
5
+ import { Globals } from "../state/Globals.ts"
4
6
 
5
7
  export interface emit<E> extends Generator<Rune<E>, void> {}
6
8
 
7
9
  export function* emit<const E>(event: EnsureNarrow<E>): emit<E> {
8
- yield* rune((fiber) => {
9
- fiber.handler(event)
10
- })<E>()
10
+ const globals = Context.getAssert(Globals.make)
11
+ const fiber = Context.getAssert(Fiber.make)
12
+ globals.handler?.(event, {
13
+ fiber: fiber.index,
14
+ timestamp: Date.now(),
15
+ })
11
16
  }
package/L/fork.ts CHANGED
@@ -1,18 +1,20 @@
1
- import { Fiber } from "../Fiber.ts"
2
- import type { Rune } from "../Rune.ts"
3
- import type { Runic } from "../Runic.ts"
4
- import type { StateMap } from "../state/StateMap.ts"
5
- import { rune } from "./rune.ts"
1
+ // import type { FiberConfig } from "../Fiber.ts"
2
+ // import type { Rune } from "../Rune.ts"
3
+ // import type { Runic } from "../Runic.ts"
4
+ // import { runic as runic_ } from "./runic.ts"
5
+ // import { self } from "./self.ts"
6
6
 
7
- export interface fork<Y extends Rune, T> extends Generator<Rune<never> | Y, Fiber<T>> {}
7
+ // export interface fork<Y extends Rune, T> extends Generator<Rune<any> | Y, FiberConfig<T>> {}
8
8
 
9
- export function* fork<Y extends Rune, T>(runic: Runic<Y, T>, state?: StateMap): fork<Y, T> {
10
- return yield* rune((parent) =>
11
- Fiber({
12
- globals: parent.globals,
13
- parent,
14
- runic,
15
- state: state?.clone(),
16
- })
17
- )
18
- }
9
+ // export function* fork<Y extends Rune, T>(runic: Runic<Y, T>): fork<Y, T> {
10
+ // const parent = yield* self
11
+ // const { globals, state, signal } = parent
12
+ // const controller = new AbortController()
13
+ // const fiberConfig: FiberConfig = {
14
+ // T: null!,
15
+ // globals,
16
+ // signal: controller.signal,
17
+ // state: state?.clone(),
18
+ // }
19
+ // return (yield* runic_(runic, fiberConfig)) as never
20
+ // }
package/L/model.ts CHANGED
@@ -1,11 +1,11 @@
1
+ import { Context } from "../Context.ts"
1
2
  import type { Model } from "../Model.ts"
2
3
  import type { Rune } from "../Rune.ts"
3
4
  import { ModelRegistry } from "../state/ModelRegistry.ts"
4
- import { state } from "./state.ts"
5
5
 
6
6
  export interface model extends Generator<Rune<never>, void> {}
7
7
 
8
8
  export function* model(model: Model): model {
9
- const [modelRegistry] = yield* state(ModelRegistry)
9
+ const modelRegistry = Context.getOrInit(ModelRegistry.make)
10
10
  modelRegistry.register(model)
11
11
  }
package/L/rune.ts CHANGED
@@ -1,11 +1,10 @@
1
- import type { Fiber } from "../Fiber.ts"
2
1
  import { type Rune, RuneKey } from "../Rune.ts"
3
2
 
4
3
  export interface rune<T> extends Iterable<Rune<never>, T> {
5
4
  <E>(): Generator<Rune<E>, T>
6
5
  }
7
6
 
8
- export function rune<R>(source: (fiber: Fiber) => R): rune<Awaited<R>> {
7
+ export function rune<R>(source: () => R): rune<Awaited<R>> {
9
8
  return Object.assign(
10
9
  function*<E>(): Generator<Rune<E>, Awaited<R>> {
11
10
  return yield Object.assign(source, { [RuneKey]: {} as never })
package/LEvent.ts CHANGED
@@ -1,35 +1,48 @@
1
- import type { SchemaRoot } from "liminal-schema"
1
+ import type { SchemaObject } from "liminal-schema"
2
+ import { EventBase } from "./EventBase.ts"
2
3
  import type { Message } from "./Message.ts"
3
4
 
4
5
  export type LEvent = InferenceRequested | Inferred | MessageAppended | FiberCreated | FiberStarted | FiberResolved
5
6
 
6
- export interface InferenceRequested extends LEventBase<"inference_requested"> {
7
- requestId: number
8
- schema?: SchemaRoot
9
- }
7
+ export const LEventTag: unique symbol = Symbol.for("liminal/LEvent")
8
+ export type LEventTag = typeof LEventTag
10
9
 
11
- export interface Inferred extends LEventBase<"inferred"> {
12
- requestId: number
13
- inference: string
10
+ export class InferenceRequested extends EventBase(LEventTag, "inference_requested") {
11
+ declare schema?: SchemaObject
12
+ constructor(
13
+ readonly requestId: number,
14
+ schema?: SchemaObject | undefined,
15
+ ) {
16
+ super()
17
+ if (schema) {
18
+ this.schema = schema
19
+ }
20
+ }
14
21
  }
15
22
 
16
- export interface MessageAppended extends LEventBase<"message_appended"> {
17
- message: Message
23
+ export class Inferred extends EventBase(LEventTag, "inferred") {
24
+ constructor(
25
+ readonly requestId: number,
26
+ readonly inference: string,
27
+ ) {
28
+ super()
29
+ }
18
30
  }
19
31
 
20
- export interface FiberCreated extends LEventBase<"fiber_created"> {}
21
- export interface FiberStarted extends LEventBase<"fiber_started"> {}
22
- export interface FiberResolved extends LEventBase<"fiber_resolved"> {
23
- value: any
32
+ export class MessageAppended extends EventBase(LEventTag, "message_appended") {
33
+ constructor(readonly message: Message) {
34
+ super()
35
+ }
24
36
  }
25
37
 
26
- interface LEventBase<K extends string> {
27
- [LEventTag]: K
38
+ export class FiberCreated extends EventBase(LEventTag, "fiber_created") {}
39
+ export class FiberStarted extends EventBase(LEventTag, "fiber_started") {}
40
+ export class FiberResolved extends EventBase(LEventTag, "fiber_resolved") {
41
+ constructor(readonly value: any) {
42
+ super()
43
+ }
28
44
  }
29
45
 
30
- export const LEventTag: unique symbol = Symbol.for("liminal/LEvent")
31
- export type LEventTag = typeof LEventTag
32
-
33
46
  export function isLEvent(value: unknown): value is LEvent {
34
47
  return typeof value === "object" && value !== null && LEventTag in value
35
48
  }
package/Rune.ts CHANGED
@@ -1,7 +1,5 @@
1
- import type { Fiber } from "./Fiber.ts"
2
-
3
1
  export interface Rune<out E = any> {
4
- (fiber: Fiber): any
2
+ (): any
5
3
  [RuneKey]: E
6
4
  }
7
5
 
package/dist/Agent.d.ts CHANGED
@@ -1,12 +1,8 @@
1
- import { type FiberInfo } from "./Fiber.ts";
2
1
  import type { Rune, RuneKey } from "./Rune.ts";
3
2
  import type { Runic } from "./Runic.ts";
3
+ import { Globals } from "./state/Globals.ts";
4
4
  export interface Agent<out T, out E> extends PromiseLike<T> {
5
5
  T: T;
6
6
  E: E;
7
7
  }
8
- export declare function Agent<Y extends Rune, T>(runic: Runic<Y, T>, config?: AgentConfig<Y, T>): Agent<T, Rune.E<Y>>;
9
- export interface AgentConfig<Y extends Rune, _T> {
10
- handler: (event: Y[RuneKey], info: FiberInfo) => void;
11
- signal?: AbortSignal;
12
- }
8
+ export declare function Agent<Y extends Rune, T>(runic: Runic<Y, T>, globals?: Partial<Globals<Y[RuneKey]>>): Agent<T, Rune.E<Y>>;
package/dist/Agent.js CHANGED
@@ -1,15 +1,15 @@
1
- import { Fiber } from "./Fiber.js";
2
- export function Agent(runic, config) {
1
+ import { Context } from "./Context.js";
2
+ import { run } from "./run.js";
3
+ import { Fiber } from "./state/Fiber.js";
4
+ import { Globals } from "./state/Globals.js";
5
+ export function Agent(runic, globals) {
3
6
  return {
4
7
  then(onfulfilled, onrejected) {
5
- const root = Fiber({
6
- globals: {
7
- handler: config?.handler ?? (() => { }),
8
- },
9
- runic,
10
- signal: config?.signal,
11
- });
12
- return root.run().then(onfulfilled, onrejected);
8
+ const context = new Context([
9
+ [Globals.make, Globals.make(globals)],
10
+ [Fiber.make, Fiber.make()],
11
+ ]);
12
+ return Context.storage.run(context, () => run(runic).then(onfulfilled, onrejected));
13
13
  },
14
14
  };
15
15
  }
package/dist/Agent.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Agent.js","sourceRoot":"","sources":["../Agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAkB,MAAM,YAAY,CAAA;AASlD,MAAM,UAAU,KAAK,CAAoB,KAAkB,EAAE,MAA0B;IACrF,OAAO;QACL,IAAI,CAAC,WAAW,EAAE,UAAU;YAC1B,MAAM,IAAI,GAAG,KAAK,CAAC;gBACjB,OAAO,EAAE;oBACP,OAAO,EAAE,MAAM,EAAE,OAAO,IAAI,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC;iBACvC;gBACD,KAAK;gBACL,MAAM,EAAE,MAAM,EAAE,MAAM;aACvB,CAAC,CAAA;YACF,OAAO,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAA;QACjD,CAAC;KACsD,CAAA;AAC3D,CAAC"}
1
+ {"version":3,"file":"Agent.js","sourceRoot":"","sources":["../Agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAG9B,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAO5C,MAAM,UAAU,KAAK,CACnB,KAAkB,EAClB,OAAsC;IAEtC,OAAO;QACL,IAAI,CAAC,WAAW,EAAE,UAAU;YAC1B,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC;gBAC1B,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBACrC,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC;aAC3B,CAAC,CAAA;YACF,OAAO,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC,CAAA;QACrF,CAAC;KACsD,CAAA;AAC3D,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ export declare class Context extends Map<StateFactory, unknown> {
3
+ static storage: AsyncLocalStorage<Context>;
4
+ static make(context?: Context): Context;
5
+ static unwrap(): Context;
6
+ static get<T>(factory: StateFactory<T>): T | undefined;
7
+ static getAssert<T>(factory: StateFactory<T>): T;
8
+ static getOrInit<T>(factory: StateFactory<T>): T;
9
+ }
10
+ export type StateFactory<T = any> = (instance?: T) => T;
@@ -0,0 +1,37 @@
1
+ import { assert } from "liminal-util";
2
+ import { AsyncLocalStorage } from "node:async_hooks";
3
+ export class Context extends Map {
4
+ static storage = new AsyncLocalStorage();
5
+ static make(context) {
6
+ const instance = new Context();
7
+ if (context) {
8
+ for (const [key, value] of context.entries()) {
9
+ instance.set(key, key(value));
10
+ }
11
+ }
12
+ return instance;
13
+ }
14
+ static unwrap() {
15
+ const context = this.storage.getStore();
16
+ assert(context);
17
+ return context;
18
+ }
19
+ static get(factory) {
20
+ return this.unwrap().get(factory);
21
+ }
22
+ static getAssert(factory) {
23
+ const value = this.get(factory);
24
+ assert(value);
25
+ return value;
26
+ }
27
+ static getOrInit(factory) {
28
+ let context = this.unwrap();
29
+ let instance = context.get(factory);
30
+ if (!instance) {
31
+ instance = factory();
32
+ context.set(factory, instance);
33
+ }
34
+ return instance;
35
+ }
36
+ }
37
+ //# sourceMappingURL=Context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Context.js","sourceRoot":"","sources":["../Context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAEpD,MAAM,OAAO,OAAQ,SAAQ,GAA0B;IACrD,MAAM,CAAC,OAAO,GAA+B,IAAI,iBAAiB,EAAW,CAAA;IAE7E,MAAM,CAAC,IAAI,CAAC,OAAiB;QAC3B,MAAM,QAAQ,GAAG,IAAI,OAAO,EAAE,CAAA;QAC9B,IAAI,OAAO,EAAE,CAAC;YACZ,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;gBAC7C,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,CAAA;YAC/B,CAAC;QACH,CAAC;QACD,OAAO,QAAQ,CAAA;IACjB,CAAC;IAED,MAAM,CAAC,MAAM;QACX,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAA;QACvC,MAAM,CAAC,OAAO,CAAC,CAAA;QACf,OAAO,OAAO,CAAA;IAChB,CAAC;IAED,MAAM,CAAC,GAAG,CAAI,OAAwB;QACpC,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,OAAO,CAAU,CAAA;IAC5C,CAAC;IAED,MAAM,CAAC,SAAS,CAAI,OAAwB;QAC1C,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QAC/B,MAAM,CAAC,KAAK,CAAC,CAAA;QACb,OAAO,KAAK,CAAA;IACd,CAAC;IAED,MAAM,CAAC,SAAS,CAAI,OAAwB;QAC1C,IAAI,OAAO,GAAG,IAAI,CAAC,MAAM,EAAE,CAAA;QAC3B,IAAI,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QACnC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,QAAQ,GAAG,OAAO,EAAE,CAAA;YACpB,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAA;QAChC,CAAC;QACD,OAAO,QAAiB,CAAA;IAC1B,CAAC"}
@@ -0,0 +1,10 @@
1
+ export interface EventBase<B extends symbol = symbol, K extends string = string> {
2
+ readonly brand: B;
3
+ readonly type: K;
4
+ }
5
+ export declare function EventBase<B extends symbol, K extends string>(brand: B, type: K): {
6
+ new (): {
7
+ readonly brand: B;
8
+ readonly type: K;
9
+ };
10
+ };
@@ -0,0 +1,20 @@
1
+ import { inspect } from "node:util";
2
+ export function EventBase(brand, type) {
3
+ return class {
4
+ brand = brand;
5
+ type = type;
6
+ // Allows us to circumvent a false positive ts error related to private symbols.
7
+ // https://github.com/microsoft/TypeScript/issues/58496
8
+ static {
9
+ Object.defineProperties(this.prototype, {
10
+ [inspect.custom]: {
11
+ value(depth, options) {
12
+ const { brand: _0, type: _1, ...rest } = this;
13
+ return `${this.constructor.name} ` + inspect(rest, { ...options, depth });
14
+ },
15
+ },
16
+ });
17
+ }
18
+ };
19
+ }
20
+ //# sourceMappingURL=EventBase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventBase.js","sourceRoot":"","sources":["../EventBase.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAuB,MAAM,WAAW,CAAA;AAOxD,MAAM,UAAU,SAAS,CAAqC,KAAQ,EAAE,IAAO;IAC7E,OAAO;QACI,KAAK,GAAG,KAAK,CAAA;QACb,IAAI,GAAG,IAAI,CAAA;QAEpB,gFAAgF;QAChF,uDAAuD;QACvD;YACE,MAAM,CAAC,gBAAgB,CAAC,IAAI,CAAC,SAAS,EAAE;gBACtC,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;oBAChB,KAAK,CAAC,KAAa,EAAE,OAAuB;wBAC1C,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,IAAI,EAAE,GAAG,IAAI,CAAA;wBAC7C,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,GAAG,GAAG,OAAO,CAAC,IAAI,EAAE,EAAE,GAAG,OAAO,EAAE,KAAK,EAAE,CAAC,CAAA;oBAC3E,CAAC;iBACF;aACF,CAAC,CAAA;QACJ,CAAC;KACF,CAAA;AACH,CAAC"}
package/dist/Globals.d.ts CHANGED
@@ -2,6 +2,5 @@ export interface Globals {
2
2
  handler<E = any>(event: E, info: EventInfo): void;
3
3
  }
4
4
  export interface EventInfo {
5
- fiber: number;
6
5
  timestamp: number;
7
6
  }