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.
- package/Agent.ts +13 -15
- package/CHANGELOG.md +15 -0
- package/Context.ts +44 -0
- package/EventBase.ts +26 -0
- package/Globals.ts +1 -1
- package/L/L.ts +0 -3
- package/L/_infer.ts +26 -22
- package/L/_message.ts +4 -7
- package/L/assistant.ts +2 -9
- package/L/branch.ts +20 -28
- package/L/emit.ts +9 -4
- package/L/fork.ts +18 -16
- package/L/model.ts +2 -2
- package/L/rune.ts +1 -2
- package/LEvent.ts +32 -19
- package/Rune.ts +1 -3
- package/dist/Agent.d.ts +2 -6
- package/dist/Agent.js +10 -10
- package/dist/Agent.js.map +1 -1
- package/dist/Context.d.ts +10 -0
- package/dist/Context.js +37 -0
- package/dist/Context.js.map +1 -0
- package/dist/EventBase.d.ts +10 -0
- package/dist/EventBase.js +20 -0
- package/dist/EventBase.js.map +1 -0
- package/dist/Globals.d.ts +0 -1
- package/dist/L/L.d.ts +0 -3
- package/dist/L/L.js +0 -3
- package/dist/L/L.js.map +1 -1
- package/dist/L/_infer.d.ts +2 -2
- package/dist/L/_infer.js +18 -16
- package/dist/L/_infer.js.map +1 -1
- package/dist/L/_message.js +4 -7
- package/dist/L/_message.js.map +1 -1
- package/dist/L/assistant.js +2 -7
- package/dist/L/assistant.js.map +1 -1
- package/dist/L/branch.js +20 -24
- package/dist/L/branch.js.map +1 -1
- package/dist/L/emit.js +9 -4
- package/dist/L/emit.js.map +1 -1
- package/dist/L/fork.d.ts +1 -7
- package/dist/L/fork.js +19 -10
- package/dist/L/fork.js.map +1 -1
- package/dist/L/model.js +2 -2
- package/dist/L/model.js.map +1 -1
- package/dist/L/rune.d.ts +1 -2
- package/dist/L/rune.js.map +1 -1
- package/dist/LEvent.d.ts +55 -18
- package/dist/LEvent.js +38 -0
- package/dist/LEvent.js.map +1 -1
- package/dist/Rune.d.ts +1 -2
- package/dist/Rune.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/run.d.ts +4 -0
- package/dist/run.js +35 -0
- package/dist/run.js.map +1 -0
- package/dist/state/Fiber.d.ts +9 -0
- package/dist/state/Fiber.js +14 -0
- package/dist/state/Fiber.js.map +1 -0
- package/dist/state/Globals.d.ts +9 -0
- package/dist/state/Globals.js +10 -0
- package/dist/state/Globals.js.map +1 -0
- package/dist/state/MessageRegistry.d.ts +1 -1
- package/dist/state/MessageRegistry.js +3 -3
- package/dist/state/MessageRegistry.js.map +1 -1
- package/dist/state/ModelConfig.d.ts +1 -19
- package/dist/state/ModelConfig.js +13 -14
- package/dist/state/ModelConfig.js.map +1 -1
- package/dist/state/ModelRegistry.d.ts +1 -1
- package/dist/state/ModelRegistry.js +7 -5
- package/dist/state/ModelRegistry.js.map +1 -1
- package/dist/state/StateMap.d.ts +1 -7
- package/dist/state/StateMap.js +1 -9
- package/dist/state/StateMap.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/index.ts +2 -2
- package/package.json +3 -4
- package/run.ts +37 -0
- package/state/Fiber.ts +13 -0
- package/state/Globals.ts +12 -0
- package/state/MessageRegistry.ts +4 -4
- package/state/ModelConfig.ts +13 -15
- package/state/ModelRegistry.ts +10 -8
- package/state/StateMap.ts +0 -15
- package/Fiber.ts +0 -117
- package/L/all.ts +0 -11
- package/L/join.ts +0 -9
- package/L/self.ts +0 -13
- package/L/state.ts +0 -20
- package/dist/Fiber.d.ts +0 -38
- package/dist/Fiber.js +0 -79
- package/dist/Fiber.js.map +0 -1
- package/dist/L/all.d.ts +0 -7
- package/dist/L/all.js +0 -5
- package/dist/L/all.js.map +0 -1
- package/dist/L/join.d.ts +0 -5
- package/dist/L/join.js +0 -5
- package/dist/L/join.js.map +0 -1
- package/dist/L/self.d.ts +0 -6
- package/dist/L/self.js +0 -7
- package/dist/L/self.js.map +0 -1
- package/dist/L/state.d.ts +0 -7
- package/dist/L/state.js +0 -14
- package/dist/L/state.js.map +0 -1
- package/dist/state/Counter.d.ts +0 -6
- package/dist/state/Counter.js +0 -13
- package/dist/state/Counter.js.map +0 -1
- package/dist/state/DefaultStateMap.d.ts +0 -4
- package/dist/state/DefaultStateMap.js +0 -14
- package/dist/state/DefaultStateMap.js.map +0 -1
- package/state/Counter.ts +0 -11
- package/state/DefaultStateMap.ts +0 -14
package/Agent.ts
CHANGED
|
@@ -1,28 +1,26 @@
|
|
|
1
|
-
import {
|
|
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>(
|
|
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
|
|
14
|
-
globals
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
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 {
|
|
1
|
+
import type { SchemaObject } from "liminal-schema"
|
|
2
2
|
import { assert } from "liminal-util"
|
|
3
|
-
import {
|
|
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?:
|
|
13
|
-
const
|
|
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
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
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 {
|
|
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
|
|
11
|
+
const messageRegistry = Context.getOrInit(MessageRegistry.make)
|
|
12
12
|
const message: Message = { role, content }
|
|
13
|
-
yield* emit
|
|
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
|
|
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
|
-
|
|
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 {
|
|
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 {
|
|
5
|
-
import {
|
|
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 =
|
|
20
|
-
const { globals, state } = parent
|
|
18
|
+
const parent = Context.get(Fiber.make)
|
|
21
19
|
if (Array.isArray(value)) {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
|
44
|
-
|
|
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 {
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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 {
|
|
2
|
-
import type { Rune } from "../Rune.ts"
|
|
3
|
-
import type { Runic } from "../Runic.ts"
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
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<
|
|
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
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
|
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: (
|
|
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 {
|
|
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
|
|
7
|
-
|
|
8
|
-
schema?: SchemaRoot
|
|
9
|
-
}
|
|
7
|
+
export const LEventTag: unique symbol = Symbol.for("liminal/LEvent")
|
|
8
|
+
export type LEventTag = typeof LEventTag
|
|
10
9
|
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
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
|
|
17
|
-
|
|
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
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
32
|
+
export class MessageAppended extends EventBase(LEventTag, "message_appended") {
|
|
33
|
+
constructor(readonly message: Message) {
|
|
34
|
+
super()
|
|
35
|
+
}
|
|
24
36
|
}
|
|
25
37
|
|
|
26
|
-
|
|
27
|
-
|
|
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
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>,
|
|
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 {
|
|
2
|
-
|
|
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
|
|
6
|
-
globals
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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,
|
|
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;
|
package/dist/Context.js
ADDED
|
@@ -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"}
|