liminal 0.5.5 → 0.5.6
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 +19 -9
- package/CHANGELOG.md +6 -0
- package/Context.ts +36 -28
- package/Fiber.ts +54 -0
- package/Handler.ts +6 -0
- package/L/L.ts +0 -1
- package/L/_infer.ts +14 -20
- package/L/_message.ts +5 -2
- package/L/branch.ts +14 -18
- package/L/emit.ts +9 -7
- package/L/model.ts +10 -4
- package/L/rune.ts +2 -1
- package/LEvent.ts +15 -1
- package/MessageRegistry.ts +21 -0
- package/{state/ModelRegistry.ts → ModelRegistry.ts} +19 -12
- package/Rune.ts +3 -1
- package/Runic.ts +11 -3
- package/RuntimeEvent.ts +6 -0
- package/dist/Agent.d.ts +11 -3
- package/dist/Agent.js +10 -8
- package/dist/Agent.js.map +1 -1
- package/dist/Context.d.ts +11 -9
- package/dist/Context.js +29 -25
- package/dist/Context.js.map +1 -1
- package/dist/Fiber.d.ts +12 -0
- package/dist/Fiber.js +41 -0
- package/dist/Fiber.js.map +1 -0
- package/dist/Handler.d.ts +4 -1
- package/dist/Handler.js +2 -1
- package/dist/Handler.js.map +1 -1
- package/dist/L/L.d.ts +0 -1
- package/dist/L/L.js +0 -1
- package/dist/L/L.js.map +1 -1
- package/dist/L/_infer.js +15 -17
- package/dist/L/_infer.js.map +1 -1
- package/dist/L/_message.js +5 -2
- package/dist/L/_message.js.map +1 -1
- package/dist/L/branch.js +12 -18
- package/dist/L/branch.js.map +1 -1
- package/dist/L/emit.js +9 -7
- package/dist/L/emit.js.map +1 -1
- package/dist/L/model.d.ts +2 -1
- package/dist/L/model.js +9 -3
- package/dist/L/model.js.map +1 -1
- package/dist/L/rune.d.ts +2 -1
- package/dist/L/rune.js +1 -0
- package/dist/L/rune.js.map +1 -1
- package/dist/LEvent.d.ts +12 -1
- package/dist/LEvent.js +7 -0
- package/dist/LEvent.js.map +1 -1
- package/dist/MessageRegistry.d.ts +9 -0
- package/dist/MessageRegistry.js +15 -0
- package/dist/MessageRegistry.js.map +1 -0
- package/dist/{state/ModelRegistry.d.ts → ModelRegistry.d.ts} +4 -2
- package/dist/{state/ModelRegistry.js → ModelRegistry.js} +12 -6
- package/dist/ModelRegistry.js.map +1 -0
- package/dist/Rune.d.ts +2 -1
- package/dist/Rune.js.map +1 -1
- package/dist/Runic.d.ts +3 -3
- package/dist/Runic.js.map +1 -1
- package/dist/RuntimeEvent.d.ts +5 -0
- package/dist/RuntimeEvent.js +2 -0
- package/dist/RuntimeEvent.js.map +1 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/index.ts +4 -3
- package/package.json +1 -1
- package/Globals.ts +0 -10
- package/L/fork.ts +0 -20
- package/L/namespace.ts +0 -11
- package/dist/Globals.d.ts +0 -6
- package/dist/Globals.js +0 -2
- package/dist/Globals.js.map +0 -1
- package/dist/L/fork.d.ts +0 -1
- package/dist/L/fork.js +0 -20
- package/dist/L/fork.js.map +0 -1
- package/dist/L/namespace.d.ts +0 -1
- package/dist/L/namespace.js +0 -12
- package/dist/L/namespace.js.map +0 -1
- package/dist/run.d.ts +0 -4
- package/dist/run.js +0 -35
- package/dist/run.js.map +0 -1
- package/dist/state/Fiber.d.ts +0 -9
- package/dist/state/Fiber.js +0 -14
- package/dist/state/Fiber.js.map +0 -1
- package/dist/state/Globals.d.ts +0 -9
- package/dist/state/Globals.js +0 -10
- package/dist/state/Globals.js.map +0 -1
- package/dist/state/MessageRegistry.d.ts +0 -7
- package/dist/state/MessageRegistry.js +0 -14
- package/dist/state/MessageRegistry.js.map +0 -1
- package/dist/state/ModelConfig.d.ts +0 -31
- package/dist/state/ModelConfig.js +0 -17
- package/dist/state/ModelConfig.js.map +0 -1
- package/dist/state/ModelRegistry.js.map +0 -1
- package/dist/state/StateMap.d.ts +0 -1
- package/dist/state/StateMap.js +0 -2
- package/dist/state/StateMap.js.map +0 -1
- package/run.ts +0 -37
- package/state/Fiber.ts +0 -13
- package/state/Globals.ts +0 -12
- package/state/MessageRegistry.ts +0 -17
- package/state/ModelConfig.ts +0 -47
- package/state/StateMap.ts +0 -0
package/Agent.ts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import { Context } from "./Context.ts"
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
2
|
+
import { Fiber } from "./Fiber.ts"
|
|
3
|
+
import { HandlerContext } from "./Handler.ts"
|
|
4
|
+
import { MessageRegistry, MessageRegistryContext } from "./MessageRegistry.ts"
|
|
5
|
+
import { ModelRegistry, ModelRegistryContext } from "./ModelRegistry.ts"
|
|
6
|
+
import type { Rune } from "./Rune.ts"
|
|
4
7
|
import type { Runic } from "./Runic.ts"
|
|
5
|
-
import {
|
|
6
|
-
|
|
8
|
+
import type { RuntimeEvent } from "./RuntimeEvent.ts"
|
|
9
|
+
|
|
10
|
+
export interface AgentConfig<E> {
|
|
11
|
+
handler?: ((event: RuntimeEvent<E>) => void) | undefined
|
|
12
|
+
models?: ModelRegistry
|
|
13
|
+
messages?: MessageRegistry
|
|
14
|
+
signal?: AbortSignal | undefined
|
|
15
|
+
}
|
|
7
16
|
|
|
8
17
|
export interface Agent<out T, out E> extends PromiseLike<T> {
|
|
9
18
|
T: T
|
|
@@ -12,15 +21,16 @@ export interface Agent<out T, out E> extends PromiseLike<T> {
|
|
|
12
21
|
|
|
13
22
|
export function Agent<Y extends Rune, T>(
|
|
14
23
|
runic: Runic<Y, T>,
|
|
15
|
-
|
|
24
|
+
config?: AgentConfig<Rune.E<Y>>,
|
|
16
25
|
): Agent<T, Rune.E<Y>> {
|
|
17
26
|
return {
|
|
18
27
|
then(onfulfilled, onrejected) {
|
|
19
|
-
const
|
|
20
|
-
[
|
|
21
|
-
[
|
|
28
|
+
const rootCtx = new Context([
|
|
29
|
+
[HandlerContext, config?.handler],
|
|
30
|
+
[ModelRegistryContext, config?.models ?? new ModelRegistry()],
|
|
31
|
+
[MessageRegistryContext, config?.messages ?? new MessageRegistry()],
|
|
22
32
|
])
|
|
23
|
-
return
|
|
33
|
+
return rootCtx.run(() => Fiber(runic).resolve().then(onfulfilled, onrejected))
|
|
24
34
|
},
|
|
25
35
|
} satisfies Omit<Agent<T, Rune.E<Y>>, "E" | "T"> as never
|
|
26
36
|
}
|
package/CHANGELOG.md
CHANGED
package/Context.ts
CHANGED
|
@@ -1,44 +1,52 @@
|
|
|
1
1
|
import { assert } from "liminal-util"
|
|
2
2
|
import { AsyncLocalStorage } from "node:async_hooks"
|
|
3
3
|
|
|
4
|
-
|
|
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
|
-
}
|
|
4
|
+
const storage = new AsyncLocalStorage<Context>()
|
|
16
5
|
|
|
17
|
-
|
|
18
|
-
|
|
6
|
+
export class Context extends Map<ContextHandle, unknown> {
|
|
7
|
+
static ensure(): Context {
|
|
8
|
+
const context = storage.getStore()
|
|
19
9
|
assert(context)
|
|
20
10
|
return context
|
|
21
11
|
}
|
|
22
12
|
|
|
23
|
-
|
|
24
|
-
return
|
|
13
|
+
get<V>(context: ContextHandle<V>): V | undefined {
|
|
14
|
+
return super.get(context) as never
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
getOrInit<V>(context: ContextHandle<V>, init: () => V): V {
|
|
18
|
+
if (this.has(context)) {
|
|
19
|
+
return this.get(context) as V
|
|
20
|
+
}
|
|
21
|
+
const instance = init()
|
|
22
|
+
this.set(context, instance)
|
|
23
|
+
return instance
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return value
|
|
26
|
+
set<V>(context: ContextHandle<V>, value: V): this {
|
|
27
|
+
super.set(context, value)
|
|
28
|
+
return this
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
run<R>(callback: () => R): R {
|
|
32
|
+
return storage.run(this, callback)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
clone(overrides?: Iterable<[ContextHandle, unknown]>): Context {
|
|
36
|
+
const context = new Context(overrides)
|
|
37
|
+
for (const [handle, value] of this.entries()) {
|
|
38
|
+
if (!context.has(handle)) {
|
|
39
|
+
context.set(handle, handle.clone?.(value) ?? value)
|
|
40
|
+
}
|
|
39
41
|
}
|
|
40
|
-
return
|
|
42
|
+
return context
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
|
|
44
|
-
export type
|
|
46
|
+
export type ContextHandle<V = any> = {
|
|
47
|
+
clone?: ((value: V) => V) | undefined
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function ContextHandle<V>(clone?: (value: V) => V): ContextHandle<V> {
|
|
51
|
+
return { clone }
|
|
52
|
+
}
|
package/Fiber.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Context } from "./Context.ts"
|
|
2
|
+
import type { Rune } from "./Rune.ts"
|
|
3
|
+
import { Runic } from "./Runic.ts"
|
|
4
|
+
|
|
5
|
+
export interface FiberInfo {
|
|
6
|
+
readonly index: number
|
|
7
|
+
readonly parent?: FiberInfo
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let nextIndex = 0
|
|
11
|
+
|
|
12
|
+
export interface Fiber<T = any> {
|
|
13
|
+
parent?: Fiber | undefined
|
|
14
|
+
info: FiberInfo
|
|
15
|
+
resolve(): Promise<T>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function Fiber<T = any>(runic: Runic<Rune, T>, parent?: Fiber): Fiber<T> {
|
|
19
|
+
let index = nextIndex++
|
|
20
|
+
let pending: Promise<T> | undefined
|
|
21
|
+
const context = Context.ensure()
|
|
22
|
+
return {
|
|
23
|
+
parent,
|
|
24
|
+
info: {
|
|
25
|
+
index,
|
|
26
|
+
...parent && { parent: parent.info },
|
|
27
|
+
},
|
|
28
|
+
resolve,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function resolve(this: Fiber<T>): Promise<T> {
|
|
32
|
+
if (!pending) {
|
|
33
|
+
const { promise, resolve, reject } = Promise.withResolvers<T>()
|
|
34
|
+
pending = promise
|
|
35
|
+
const iterator = Runic.unwrap(runic)
|
|
36
|
+
let nextArg: unknown
|
|
37
|
+
context.run(async () => {
|
|
38
|
+
try {
|
|
39
|
+
let current = await iterator.next()
|
|
40
|
+
while (!current.done) {
|
|
41
|
+
const rune = current.value
|
|
42
|
+
nextArg = await rune(this)
|
|
43
|
+
current = await iterator.next(nextArg)
|
|
44
|
+
}
|
|
45
|
+
const { value } = current
|
|
46
|
+
resolve(value)
|
|
47
|
+
} catch (error) {
|
|
48
|
+
reject(error)
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
return pending
|
|
53
|
+
}
|
|
54
|
+
}
|
package/Handler.ts
CHANGED
package/L/L.ts
CHANGED
package/L/_infer.ts
CHANGED
|
@@ -1,39 +1,33 @@
|
|
|
1
1
|
import type { SchemaObject } from "liminal-schema"
|
|
2
2
|
import { assert } from "liminal-util"
|
|
3
|
-
import { Context } from "../Context.ts"
|
|
3
|
+
import { Context, ContextHandle } from "../Context.ts"
|
|
4
4
|
import { InferenceRequested, Inferred, type LEvent } from "../LEvent.ts"
|
|
5
|
+
import { MessageRegistryContext } from "../MessageRegistry.ts"
|
|
6
|
+
import { ModelRegistryContext } from "../ModelRegistry.ts"
|
|
5
7
|
import type { Rune } from "../Rune.ts"
|
|
6
|
-
import { MessageRegistry } from "../state/MessageRegistry.ts"
|
|
7
|
-
import { ModelRegistry } from "../state/ModelRegistry.ts"
|
|
8
8
|
import { emit } from "./emit.ts"
|
|
9
9
|
import { rune } from "./rune.ts"
|
|
10
10
|
|
|
11
11
|
export function* _infer(schema?: SchemaObject): Generator<Rune<LEvent>, string> {
|
|
12
|
-
const
|
|
12
|
+
const context = Context.ensure()
|
|
13
|
+
const modelRegistry = context.get(ModelRegistryContext)
|
|
14
|
+
assert(modelRegistry)
|
|
13
15
|
const model = modelRegistry.peek()
|
|
14
16
|
assert(model)
|
|
15
|
-
const
|
|
16
|
-
const counter = Context.getOrInit(InferenceRequestCounter)
|
|
17
|
-
const requestId = counter.next()
|
|
17
|
+
const requestId = context.getOrInit(InferenceRequestCounterContext, () => new InferenceRequestCounter()).next()
|
|
18
18
|
yield* emit(new InferenceRequested(requestId, schema))
|
|
19
|
+
const messageRegistry = context.get(MessageRegistryContext)
|
|
20
|
+
assert(messageRegistry)
|
|
19
21
|
const inference = yield* rune(() => model.resolve(messageRegistry.messages, schema))
|
|
20
22
|
yield* emit(new Inferred(requestId, inference))
|
|
21
23
|
return inference
|
|
22
24
|
}
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return this.count++
|
|
29
|
-
},
|
|
30
|
-
clone() {
|
|
31
|
-
return InferenceRequestCounter(this)
|
|
32
|
-
},
|
|
26
|
+
class InferenceRequestCounter {
|
|
27
|
+
count: number = 0
|
|
28
|
+
next(): number {
|
|
29
|
+
return this.count++
|
|
33
30
|
}
|
|
34
31
|
}
|
|
35
32
|
|
|
36
|
-
|
|
37
|
-
count: number
|
|
38
|
-
next(): number
|
|
39
|
-
}
|
|
33
|
+
const InferenceRequestCounterContext: ContextHandle<InferenceRequestCounter> = ContextHandle()
|
package/L/_message.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
+
import { assert } from "liminal-util"
|
|
1
2
|
import { Context } from "../Context.ts"
|
|
2
3
|
import { type LEvent, MessageAppended } from "../LEvent.ts"
|
|
3
4
|
import type { ContentPart, Message, MessageRole } from "../Message.ts"
|
|
5
|
+
import { MessageRegistryContext } from "../MessageRegistry.ts"
|
|
4
6
|
import type { Rune } from "../Rune.ts"
|
|
5
|
-
import { MessageRegistry } from "../state/MessageRegistry.ts"
|
|
6
7
|
import { emit } from "./emit.ts"
|
|
7
8
|
|
|
8
9
|
export interface _message extends Generator<Rune<LEvent>, void> {}
|
|
9
10
|
|
|
10
11
|
export function* _message(role: MessageRole, content: Array<ContentPart>): _message {
|
|
11
|
-
const
|
|
12
|
+
const context = Context.ensure()
|
|
13
|
+
const messageRegistry = context.get(MessageRegistryContext)
|
|
14
|
+
assert(messageRegistry)
|
|
12
15
|
const message: Message = { role, content }
|
|
13
16
|
yield* emit(new MessageAppended(message))
|
|
14
17
|
messageRegistry.append(message)
|
package/L/branch.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { Context } from "../Context.ts"
|
|
2
|
-
import {
|
|
2
|
+
import { Fiber } from "../Fiber.ts"
|
|
3
3
|
import { type Rune } from "../Rune.ts"
|
|
4
4
|
import type { Runic } from "../Runic.ts"
|
|
5
|
-
import { Fiber } from "../state/Fiber.ts"
|
|
6
5
|
import { rune } from "./rune.ts"
|
|
7
6
|
|
|
8
7
|
export interface branch<Y extends Rune, T> extends Generator<Y, T> {}
|
|
@@ -15,23 +14,20 @@ export function branch<XR extends Record<keyof any, Runic>>(
|
|
|
15
14
|
runics: XR,
|
|
16
15
|
): branch<Runic.Y<XR[keyof XR]> | Rune<never>, { [K in keyof XR]: Runic.T<XR[K]> }>
|
|
17
16
|
export function* branch(value: Runic | Array<Runic> | Record<keyof any, Runic>): branch<Rune, any> {
|
|
18
|
-
const
|
|
17
|
+
const context = Context.ensure()
|
|
18
|
+
const parent = yield* rune((fiber) => fiber)
|
|
19
19
|
if (Array.isArray(value)) {
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
context.set(Fiber.make, new Fiber(parent))
|
|
23
|
-
return () => run(runic, context)
|
|
24
|
-
})
|
|
25
|
-
return yield* rune(() => Promise.all(runners.map((runner) => runner())))
|
|
20
|
+
const fibers = value.map((runic) => context.clone().run(() => Fiber(runic, parent)))
|
|
21
|
+
return yield* rune(() => Promise.all(fibers.map(({ resolve }) => resolve())))
|
|
26
22
|
} else if (typeof value === "object") {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
const entries = Object.entries(value)
|
|
24
|
+
const fibers = entries.map(([_key, runic]) => context.clone().run(() => Fiber(runic, parent)))
|
|
25
|
+
return yield* rune(() =>
|
|
26
|
+
Promise
|
|
27
|
+
.all(fibers.map(({ resolve }, i) => resolve().then((value) => [entries[i]![0], value])))
|
|
28
|
+
.then(Object.fromEntries)
|
|
29
|
+
)
|
|
33
30
|
}
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
return yield* rune(() => run(typeof value === "function" ? value() : value, context))
|
|
31
|
+
const fiber = context.clone().run(() => Fiber(typeof value === "function" ? value() : value, parent))
|
|
32
|
+
return yield* rune(() => fiber.resolve())
|
|
37
33
|
}
|
package/L/emit.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import type { EnsureNarrow } from "liminal-util"
|
|
2
2
|
import { Context } from "../Context.ts"
|
|
3
|
+
import { HandlerContext } from "../Handler.ts"
|
|
3
4
|
import type { Rune } from "../Rune.ts"
|
|
4
|
-
import {
|
|
5
|
-
import { Globals } from "../state/Globals.ts"
|
|
5
|
+
import { rune } from "./rune.ts"
|
|
6
6
|
|
|
7
7
|
export interface emit<E> extends Generator<Rune<E>, void> {}
|
|
8
8
|
|
|
9
9
|
export function* emit<const E>(event: EnsureNarrow<E>): emit<E> {
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
const context = Context.ensure()
|
|
11
|
+
const handler = context.get(HandlerContext)
|
|
12
|
+
const fiber = yield* rune((fiber) => fiber)
|
|
13
|
+
handler?.({
|
|
14
|
+
...fiber.info,
|
|
15
|
+
...fiber.parent && { parent: fiber.parent.info },
|
|
16
|
+
event,
|
|
15
17
|
})
|
|
16
18
|
}
|
package/L/model.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
|
+
import { assert } from "liminal-util"
|
|
1
2
|
import { Context } from "../Context.ts"
|
|
3
|
+
import { ModelRegistered } from "../LEvent.ts"
|
|
2
4
|
import type { Model } from "../Model.ts"
|
|
5
|
+
import { ModelRegistryContext } from "../ModelRegistry.ts"
|
|
3
6
|
import type { Rune } from "../Rune.ts"
|
|
4
|
-
import {
|
|
7
|
+
import { emit } from "./emit.ts"
|
|
5
8
|
|
|
6
|
-
export interface model extends Generator<Rune<
|
|
9
|
+
export interface model extends Generator<Rune<ModelRegistered>, void> {}
|
|
7
10
|
|
|
8
11
|
export function* model(model: Model): model {
|
|
9
|
-
const
|
|
10
|
-
|
|
12
|
+
const context = Context.ensure()
|
|
13
|
+
const registry = context.get(ModelRegistryContext)
|
|
14
|
+
assert(registry)
|
|
15
|
+
registry.register(model)
|
|
16
|
+
yield* emit(new ModelRegistered(model))
|
|
11
17
|
}
|
package/L/rune.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { Fiber } from "../Fiber.ts"
|
|
1
2
|
import { type Rune, RuneKey } from "../Rune.ts"
|
|
2
3
|
|
|
3
4
|
export interface rune<T> extends Iterable<Rune<never>, T> {
|
|
4
5
|
<E>(): Generator<Rune<E>, T>
|
|
5
6
|
}
|
|
6
7
|
|
|
7
|
-
export function rune<R>(source: () => R): rune<Awaited<R>> {
|
|
8
|
+
export function rune<R>(source: (fiber: Fiber) => R): rune<Awaited<R>> {
|
|
8
9
|
return Object.assign(
|
|
9
10
|
function*<E>(): Generator<Rune<E>, Awaited<R>> {
|
|
10
11
|
return yield Object.assign(source, { [RuneKey]: {} as never })
|
package/LEvent.ts
CHANGED
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
import type { SchemaObject } from "liminal-schema"
|
|
2
2
|
import { EventBase } from "./EventBase.ts"
|
|
3
3
|
import type { Message } from "./Message.ts"
|
|
4
|
+
import type { Model } from "./Model.ts"
|
|
4
5
|
|
|
5
|
-
export type LEvent =
|
|
6
|
+
export type LEvent =
|
|
7
|
+
| FiberCreated
|
|
8
|
+
| FiberResolved
|
|
9
|
+
| FiberStarted
|
|
10
|
+
| InferenceRequested
|
|
11
|
+
| Inferred
|
|
12
|
+
| MessageAppended
|
|
13
|
+
| ModelRegistered
|
|
6
14
|
|
|
7
15
|
export const LEventTag: unique symbol = Symbol.for("liminal/LEvent")
|
|
8
16
|
export type LEventTag = typeof LEventTag
|
|
9
17
|
|
|
18
|
+
export class ModelRegistered extends EventBase(LEventTag, "model_registered") {
|
|
19
|
+
constructor(readonly model: Model) {
|
|
20
|
+
super()
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
10
24
|
export class InferenceRequested extends EventBase(LEventTag, "inference_requested") {
|
|
11
25
|
declare schema?: SchemaObject
|
|
12
26
|
constructor(
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ContextHandle } from "./Context.ts"
|
|
2
|
+
import type { Message } from "./Message.ts"
|
|
3
|
+
|
|
4
|
+
export class MessageRegistry {
|
|
5
|
+
messages: Array<Message>
|
|
6
|
+
constructor(messages?: Array<Message>) {
|
|
7
|
+
this.messages = messages ?? []
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
append(message: Message): void {
|
|
11
|
+
this.messages.push(message)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
clone(): MessageRegistry {
|
|
15
|
+
return new MessageRegistry([...this.messages])
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const MessageRegistryContext: ContextHandle<MessageRegistry> = ContextHandle(({ messages }) =>
|
|
20
|
+
new MessageRegistry([...messages])
|
|
21
|
+
)
|
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ContextHandle } from "./Context.ts"
|
|
2
|
+
import type { Model } from "./Model.ts"
|
|
2
3
|
|
|
3
4
|
/** An intrusive list for storing `Model`s. */
|
|
4
5
|
export class ModelRegistry {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
declare head?: ModelRegistryNode | undefined
|
|
7
|
+
declare tail?: ModelRegistryNode | undefined
|
|
8
|
+
|
|
9
|
+
constructor(models?: Array<Model>) {
|
|
10
|
+
if (models) {
|
|
11
|
+
for (const model of models) {
|
|
12
|
+
this.register(model)
|
|
10
13
|
}
|
|
11
14
|
}
|
|
12
|
-
return instance
|
|
13
15
|
}
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
declare tail?: ModelRegistryNode | undefined
|
|
17
|
-
|
|
18
|
-
peek(): Model | undefined {
|
|
17
|
+
peek() {
|
|
19
18
|
return this.tail?.model
|
|
20
19
|
}
|
|
21
20
|
|
|
@@ -33,7 +32,7 @@ export class ModelRegistry {
|
|
|
33
32
|
return node
|
|
34
33
|
}
|
|
35
34
|
|
|
36
|
-
remove(node: ModelRegistryNode)
|
|
35
|
+
remove(node: ModelRegistryNode) {
|
|
37
36
|
if (node.prev) {
|
|
38
37
|
node.prev.next = node.next
|
|
39
38
|
}
|
|
@@ -55,3 +54,11 @@ export interface ModelRegistryNode {
|
|
|
55
54
|
model: Model
|
|
56
55
|
next?: ModelRegistryNode | undefined
|
|
57
56
|
}
|
|
57
|
+
|
|
58
|
+
export const ModelRegistryContext: ContextHandle<ModelRegistry> = ContextHandle(({ head }) => {
|
|
59
|
+
const instance = new ModelRegistry()
|
|
60
|
+
for (let node = head; node; node = node.next) {
|
|
61
|
+
instance.register(node.model)
|
|
62
|
+
}
|
|
63
|
+
return instance
|
|
64
|
+
})
|
package/Rune.ts
CHANGED
package/Runic.ts
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import type { Rune } from "./Rune.ts"
|
|
2
2
|
|
|
3
3
|
export type RuneIterator<Y extends Rune = Rune, T = any> = Iterator<Y, T> | AsyncIterator<Y, T>
|
|
4
|
-
export type RuneIterable<Y extends Rune, T> = Iterable<Y, T> | AsyncIterable<Y, T>
|
|
4
|
+
export type RuneIterable<Y extends Rune = Rune, T = any> = Iterable<Y, T> | AsyncIterable<Y, T>
|
|
5
5
|
export type Runic<Y extends Rune = Rune, T = any> = RuneIterable<Y, T> | (() => RuneIterable<Y, T>)
|
|
6
|
+
|
|
6
7
|
export namespace Runic {
|
|
7
|
-
export type Y<
|
|
8
|
-
|
|
8
|
+
export type Y<X extends Runic> = 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 Runic> = X extends RuneIterable<Rune, infer T> ? T
|
|
14
|
+
: X extends () => RuneIterable<Rune, infer T> ? T
|
|
15
|
+
: X extends RuneIterator<Rune, infer T> ? T
|
|
16
|
+
: never
|
|
9
17
|
|
|
10
18
|
export function unwrap<Y extends Rune, T>(runic: Runic<Y, T>): RuneIterator<Y, T> {
|
|
11
19
|
if (Symbol.iterator in runic) {
|
package/RuntimeEvent.ts
ADDED
package/dist/Agent.d.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { MessageRegistry } from "./MessageRegistry.ts";
|
|
2
|
+
import { ModelRegistry } from "./ModelRegistry.ts";
|
|
3
|
+
import type { Rune } from "./Rune.ts";
|
|
2
4
|
import type { Runic } from "./Runic.ts";
|
|
3
|
-
import {
|
|
5
|
+
import type { RuntimeEvent } from "./RuntimeEvent.ts";
|
|
6
|
+
export interface AgentConfig<E> {
|
|
7
|
+
handler?: ((event: RuntimeEvent<E>) => void) | undefined;
|
|
8
|
+
models?: ModelRegistry;
|
|
9
|
+
messages?: MessageRegistry;
|
|
10
|
+
signal?: AbortSignal | undefined;
|
|
11
|
+
}
|
|
4
12
|
export interface Agent<out T, out E> extends PromiseLike<T> {
|
|
5
13
|
T: T;
|
|
6
14
|
E: E;
|
|
7
15
|
}
|
|
8
|
-
export declare function Agent<Y extends Rune, T>(runic: Runic<Y, T>,
|
|
16
|
+
export declare function Agent<Y extends Rune, T>(runic: Runic<Y, T>, config?: AgentConfig<Rune.E<Y>>): Agent<T, Rune.E<Y>>;
|
package/dist/Agent.js
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { Context } from "./Context.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
2
|
+
import { Fiber } from "./Fiber.js";
|
|
3
|
+
import { HandlerContext } from "./Handler.js";
|
|
4
|
+
import { MessageRegistry, MessageRegistryContext } from "./MessageRegistry.js";
|
|
5
|
+
import { ModelRegistry, ModelRegistryContext } from "./ModelRegistry.js";
|
|
6
|
+
export function Agent(runic, config) {
|
|
6
7
|
return {
|
|
7
8
|
then(onfulfilled, onrejected) {
|
|
8
|
-
const
|
|
9
|
-
[
|
|
10
|
-
[
|
|
9
|
+
const rootCtx = new Context([
|
|
10
|
+
[HandlerContext, config?.handler],
|
|
11
|
+
[ModelRegistryContext, config?.models ?? new ModelRegistry()],
|
|
12
|
+
[MessageRegistryContext, config?.messages ?? new MessageRegistry()],
|
|
11
13
|
]);
|
|
12
|
-
return
|
|
14
|
+
return rootCtx.run(() => Fiber(runic).resolve().then(onfulfilled, onrejected));
|
|
13
15
|
},
|
|
14
16
|
};
|
|
15
17
|
}
|
package/dist/Agent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Agent.js","sourceRoot":"","sources":["../Agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"Agent.js","sourceRoot":"","sources":["../Agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAA;AAC9E,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAiBxE,MAAM,UAAU,KAAK,CACnB,KAAkB,EAClB,MAA+B;IAE/B,OAAO;QACL,IAAI,CAAC,WAAW,EAAE,UAAU;YAC1B,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC;gBAC1B,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,CAAC;gBACjC,CAAC,oBAAoB,EAAE,MAAM,EAAE,MAAM,IAAI,IAAI,aAAa,EAAE,CAAC;gBAC7D,CAAC,sBAAsB,EAAE,MAAM,EAAE,QAAQ,IAAI,IAAI,eAAe,EAAE,CAAC;aACpE,CAAC,CAAA;YACF,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC,CAAA;QAChF,CAAC;KACsD,CAAA;AAC3D,CAAC"}
|
package/dist/Context.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
static getOrInit<T>(factory: StateFactory<T>): T;
|
|
1
|
+
export declare class Context extends Map<ContextHandle, unknown> {
|
|
2
|
+
static ensure(): Context;
|
|
3
|
+
get<V>(context: ContextHandle<V>): V | undefined;
|
|
4
|
+
getOrInit<V>(context: ContextHandle<V>, init: () => V): V;
|
|
5
|
+
set<V>(context: ContextHandle<V>, value: V): this;
|
|
6
|
+
run<R>(callback: () => R): R;
|
|
7
|
+
clone(overrides?: Iterable<[ContextHandle, unknown]>): Context;
|
|
9
8
|
}
|
|
10
|
-
export type
|
|
9
|
+
export type ContextHandle<V = any> = {
|
|
10
|
+
clone?: ((value: V) => V) | undefined;
|
|
11
|
+
};
|
|
12
|
+
export declare function ContextHandle<V>(clone?: (value: V) => V): ContextHandle<V>;
|