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.
- package/CHANGELOG.md +12 -0
- package/Config.ts +13 -0
- package/Context.ts +23 -49
- package/Definition.ts +32 -0
- package/EventBase.ts +1 -1
- package/Handler.ts +3 -5
- package/L/L.ts +15 -2
- package/L/all.ts +35 -0
- package/L/assistant.ts +4 -7
- package/L/catch.ts +16 -13
- package/L/context.ts +12 -0
- package/L/continuation.ts +13 -0
- package/L/emit.ts +10 -8
- package/L/infer.ts +22 -25
- package/L/message.ts +7 -9
- package/L/model.ts +6 -6
- package/L/reflect.ts +12 -0
- package/L/run.ts +29 -0
- package/L/schema/_schema_common.ts +29 -0
- package/L/schema/anyOf.ts +10 -0
- package/L/schema/array.ts +13 -0
- package/L/schema/boolean.ts +8 -0
- package/L/schema/const.ts +18 -0
- package/L/schema/enum.ts +18 -0
- package/L/schema/integer.ts +6 -0
- package/L/schema/null.ts +8 -0
- package/L/schema/number.ts +8 -0
- package/L/schema/object.ts +21 -0
- package/L/schema/string.ts +11 -0
- package/L/strand.ts +14 -71
- package/L/stream.ts +11 -24
- package/L/system.ts +13 -8
- package/L/user.ts +13 -8
- package/LEvent.ts +9 -17
- package/LiminalAssertionError.ts +19 -0
- package/Message.ts +3 -2
- package/Model.ts +5 -3
- package/ModelRegistry.ts +1 -7
- package/Rune.test.ts +5 -0
- package/Rune.ts +24 -12
- package/Schema.ts +185 -0
- package/Strand.ts +253 -0
- package/Tool.ts +8 -16
- package/TypeAdapter.ts +3 -0
- package/dist/Config.d.ts +12 -0
- package/dist/Config.js +2 -0
- package/dist/Config.js.map +1 -0
- package/dist/Context.d.ts +11 -12
- package/dist/Context.js +15 -42
- package/dist/Context.js.map +1 -1
- package/dist/Definition.d.ts +10 -0
- package/dist/Definition.js +18 -0
- package/dist/Definition.js.map +1 -0
- package/dist/EventBase.js +1 -1
- package/dist/EventBase.js.map +1 -1
- package/dist/Handler.d.ts +3 -4
- package/dist/Handler.js +1 -2
- package/dist/Handler.js.map +1 -1
- package/dist/L/L.d.ts +15 -2
- package/dist/L/L.js +15 -2
- package/dist/L/L.js.map +1 -1
- package/dist/L/all.d.ts +10 -0
- package/dist/L/all.js +20 -0
- package/dist/L/all.js.map +1 -0
- package/dist/L/assistant.d.ts +2 -2
- package/dist/L/assistant.js +3 -6
- package/dist/L/assistant.js.map +1 -1
- package/dist/L/catch.d.ts +3 -2
- package/dist/L/catch.js +12 -12
- package/dist/L/catch.js.map +1 -1
- package/dist/L/context.d.ts +2 -0
- package/dist/L/context.js +12 -0
- package/dist/L/context.js.map +1 -0
- package/dist/L/continuation.d.ts +3 -0
- package/dist/L/continuation.js +12 -0
- package/dist/L/continuation.js.map +1 -0
- package/dist/L/emit.d.ts +3 -5
- package/dist/L/emit.js +8 -3
- package/dist/L/emit.js.map +1 -1
- package/dist/L/infer.d.ts +2 -5
- package/dist/L/infer.js +21 -20
- package/dist/L/infer.js.map +1 -1
- package/dist/L/message.d.ts +3 -5
- package/dist/L/message.js +5 -5
- package/dist/L/message.js.map +1 -1
- package/dist/L/model.d.ts +2 -4
- package/dist/L/model.js +4 -3
- package/dist/L/model.js.map +1 -1
- package/dist/L/reflect.d.ts +4 -0
- package/dist/L/reflect.js +10 -0
- package/dist/L/reflect.js.map +1 -0
- package/dist/L/run.d.ts +15 -0
- package/dist/L/run.js +16 -0
- package/dist/L/run.js.map +1 -0
- package/dist/L/schema/_schema_common.d.ts +6 -0
- package/dist/L/schema/_schema_common.js +19 -0
- package/dist/L/schema/_schema_common.js.map +1 -0
- package/dist/L/schema/anyOf.d.ts +5 -0
- package/dist/L/schema/anyOf.js +5 -0
- package/dist/L/schema/anyOf.js.map +1 -0
- package/dist/L/schema/array.d.ts +5 -0
- package/dist/L/schema/array.js +8 -0
- package/dist/L/schema/array.js.map +1 -0
- package/dist/L/schema/boolean.d.ts +6 -0
- package/dist/L/schema/boolean.js +4 -0
- package/dist/L/schema/boolean.js.map +1 -0
- package/dist/L/schema/const.d.ts +8 -0
- package/dist/L/schema/const.js +10 -0
- package/dist/L/schema/const.js.map +1 -0
- package/dist/L/schema/enum.d.ts +8 -0
- package/dist/L/schema/enum.js +10 -0
- package/dist/L/schema/enum.js.map +1 -0
- package/dist/L/schema/integer.d.ts +5 -0
- package/dist/L/schema/integer.js +3 -0
- package/dist/L/schema/integer.js.map +1 -0
- package/dist/L/schema/null.d.ts +6 -0
- package/dist/L/schema/null.js +4 -0
- package/dist/L/schema/null.js.map +1 -0
- package/dist/L/schema/number.d.ts +6 -0
- package/dist/L/schema/number.js +4 -0
- package/dist/L/schema/number.js.map +1 -0
- package/dist/L/schema/object.d.ts +9 -0
- package/dist/L/schema/object.js +12 -0
- package/dist/L/schema/object.js.map +1 -0
- package/dist/L/schema/string.d.ts +8 -0
- package/dist/L/schema/string.js +4 -0
- package/dist/L/schema/string.js.map +1 -0
- package/dist/L/strand.d.ts +4 -15
- package/dist/L/strand.js +8 -53
- package/dist/L/strand.js.map +1 -1
- package/dist/L/stream.d.ts +2 -4
- package/dist/L/stream.js +8 -19
- package/dist/L/stream.js.map +1 -1
- package/dist/L/system.d.ts +2 -4
- package/dist/L/system.js +4 -3
- package/dist/L/system.js.map +1 -1
- package/dist/L/user.d.ts +2 -4
- package/dist/L/user.js +4 -3
- package/dist/L/user.js.map +1 -1
- package/dist/LEvent.d.ts +13 -41
- package/dist/LEvent.js +4 -15
- package/dist/LEvent.js.map +1 -1
- package/dist/LiminalAssertionError.d.ts +8 -0
- package/dist/LiminalAssertionError.js +20 -0
- package/dist/LiminalAssertionError.js.map +1 -0
- package/dist/Message.d.ts +3 -2
- package/dist/Model.d.ts +4 -2
- package/dist/Model.js +1 -1
- package/dist/Model.js.map +1 -1
- package/dist/ModelRegistry.d.ts +0 -2
- package/dist/ModelRegistry.js +0 -2
- package/dist/ModelRegistry.js.map +1 -1
- package/dist/Rune.d.ts +19 -7
- package/dist/Rune.js +8 -4
- package/dist/Rune.js.map +1 -1
- package/dist/Rune.test.d.ts +1 -0
- package/dist/Rune.test.js +5 -0
- package/dist/Rune.test.js.map +1 -0
- package/dist/Schema.d.ts +46 -0
- package/dist/Schema.js +130 -0
- package/dist/Schema.js.map +1 -0
- package/dist/Strand.d.ts +57 -0
- package/dist/Strand.js +177 -0
- package/dist/Strand.js.map +1 -0
- package/dist/Tool.d.ts +6 -5
- package/dist/Tool.js +3 -4
- package/dist/Tool.js.map +1 -1
- package/dist/TypeAdapter.d.ts +1 -0
- package/dist/TypeAdapter.js +3 -0
- package/dist/TypeAdapter.js.map +1 -0
- package/dist/errors.d.ts +9 -0
- package/dist/errors.js +11 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +6 -5
- package/dist/index.js +6 -5
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/util/EnsureNarrow.d.ts +1 -0
- package/dist/util/EnsureNarrow.js +2 -0
- package/dist/util/EnsureNarrow.js.map +1 -0
- package/dist/util/JSONValue.d.ts +8 -0
- package/dist/util/JSONValue.js +20 -0
- package/dist/util/JSONValue.js.map +1 -0
- package/dist/util/attachCustomInspect.d.ts +1 -0
- package/dist/util/attachCustomInspect.js +11 -0
- package/dist/util/attachCustomInspect.js.map +1 -0
- package/dist/util/isTemplateStringsArray.d.ts +1 -0
- package/dist/util/isTemplateStringsArray.js +4 -0
- package/dist/util/isTemplateStringsArray.js.map +1 -0
- package/errors.ts +12 -0
- package/index.ts +6 -5
- package/package.json +3 -10
- package/tsconfig.json +1 -5
- package/util/EnsureNarrow.ts +1 -0
- package/util/JSONValue.ts +20 -0
- package/util/attachCustomInspect.ts +14 -0
- package/util/isTemplateStringsArray.ts +3 -0
- package/Fiber.ts +0 -123
- package/L/_common.ts +0 -6
- package/L/rune.ts +0 -12
- package/MessageRegistry.ts +0 -22
- package/Runic.ts +0 -30
- package/StrandConfig.ts +0 -12
- package/ToolRegistry.ts +0 -10
- package/dist/Fiber.d.ts +0 -35
- package/dist/Fiber.js +0 -95
- package/dist/Fiber.js.map +0 -1
- package/dist/L/_common.d.ts +0 -4
- package/dist/L/_common.js +0 -7
- package/dist/L/_common.js.map +0 -1
- package/dist/L/rune.d.ts +0 -3
- package/dist/L/rune.js +0 -9
- package/dist/L/rune.js.map +0 -1
- package/dist/MessageRegistry.d.ts +0 -9
- package/dist/MessageRegistry.js +0 -15
- package/dist/MessageRegistry.js.map +0 -1
- package/dist/Runic.d.ts +0 -9
- package/dist/Runic.js +0 -18
- package/dist/Runic.js.map +0 -1
- package/dist/StrandConfig.d.ts +0 -11
- package/dist/StrandConfig.js +0 -2
- package/dist/StrandConfig.js.map +0 -1
- package/dist/ToolRegistry.d.ts +0 -6
- package/dist/ToolRegistry.js +0 -8
- 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 {
|
|
2
|
-
import {
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
package/Handler.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
1
|
+
import type { Rune } from "./Rune.ts"
|
|
2
|
+
import type { Strand } from "./Strand.ts"
|
|
3
3
|
|
|
4
|
-
export type Handler<
|
|
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 "
|
|
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 "./
|
|
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>(
|
|
8
|
+
<T>(schema: Schema<T>): Generator<Rune<LEvent>, T>
|
|
10
9
|
}
|
|
11
10
|
|
|
12
11
|
export const assistant: assistant = Object.assign(
|
|
13
|
-
function
|
|
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
|
-
|
|
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 {
|
|
2
|
-
import {
|
|
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
|
|
6
|
-
import {
|
|
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
|
|
11
|
-
|
|
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
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
}
|
|
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
|
|
2
|
-
import {
|
|
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
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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 {
|
|
5
|
-
import { ModelRegistryContext } from "../ModelRegistry.ts"
|
|
2
|
+
import { LiminalAssertionError } from "../LiminalAssertionError.ts"
|
|
6
3
|
import type { Rune } from "../Rune.ts"
|
|
7
|
-
import {
|
|
4
|
+
import { Schema } from "../Schema.ts"
|
|
5
|
+
import { continuation } from "./continuation.ts"
|
|
8
6
|
import { emit } from "./emit.ts"
|
|
9
|
-
import {
|
|
7
|
+
import { reflect } from "./reflect.ts"
|
|
10
8
|
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
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 {
|
|
2
|
-
import type {
|
|
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
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
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
|
|
8
|
-
|
|
9
|
-
|
|
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 }
|
package/L/schema/enum.ts
ADDED
|
@@ -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 }
|
package/L/schema/null.ts
ADDED
|
@@ -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 }
|