liminal 0.5.17 → 0.6.0

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 (222) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/Context.ts +11 -47
  3. package/L.ts +4 -0
  4. package/LEvent.ts +20 -53
  5. package/_emit.ts +16 -0
  6. package/assistant.ts +53 -0
  7. package/dist/Context.d.ts +15 -30
  8. package/dist/Context.js +9 -22
  9. package/dist/Context.js.map +1 -1
  10. package/dist/L.d.ts +4 -0
  11. package/dist/L.js +5 -0
  12. package/dist/L.js.map +1 -0
  13. package/dist/LEvent.d.ts +29 -62
  14. package/dist/LEvent.js +14 -46
  15. package/dist/LEvent.js.map +1 -1
  16. package/dist/_emit.d.ts +2 -0
  17. package/dist/_emit.js +16 -0
  18. package/dist/_emit.js.map +1 -0
  19. package/dist/assistant.d.ts +8 -0
  20. package/dist/assistant.js +46 -0
  21. package/dist/assistant.js.map +1 -0
  22. package/dist/index.d.ts +2 -13
  23. package/dist/index.js +2 -13
  24. package/dist/index.js.map +1 -1
  25. package/dist/messages.d.ts +4 -0
  26. package/dist/messages.js +4 -0
  27. package/dist/messages.js.map +1 -0
  28. package/dist/set.d.ts +4 -0
  29. package/dist/set.js +9 -0
  30. package/dist/set.js.map +1 -0
  31. package/dist/strand.d.ts +16 -0
  32. package/dist/strand.js +16 -0
  33. package/dist/strand.js.map +1 -0
  34. package/dist/todo.test.js +3 -0
  35. package/dist/todo.test.js.map +1 -0
  36. package/dist/tsconfig.tsbuildinfo +1 -1
  37. package/dist/user.d.ts +6 -0
  38. package/dist/user.js +18 -0
  39. package/dist/user.js.map +1 -0
  40. package/dist/util/JSONValue.d.ts +0 -3
  41. package/dist/util/JSONValue.js +1 -31
  42. package/dist/util/JSONValue.js.map +1 -1
  43. package/index.ts +2 -13
  44. package/messages.ts +5 -0
  45. package/package.json +4 -3
  46. package/set.ts +10 -0
  47. package/strand.ts +38 -0
  48. package/todo.test.ts +3 -0
  49. package/user.ts +24 -0
  50. package/util/JSONValue.ts +0 -30
  51. package/Adapter.ts +0 -27
  52. package/AdapterRegistry.ts +0 -78
  53. package/Config.ts +0 -13
  54. package/Definition.ts +0 -48
  55. package/EventBase.ts +0 -23
  56. package/Handler.ts +0 -4
  57. package/L/L.ts +0 -24
  58. package/L/all.ts +0 -39
  59. package/L/assistant.ts +0 -28
  60. package/L/catch.ts +0 -38
  61. package/L/continuation.ts +0 -14
  62. package/L/emit.ts +0 -16
  63. package/L/focus.ts +0 -17
  64. package/L/infer.ts +0 -34
  65. package/L/message.ts +0 -16
  66. package/L/reflect.ts +0 -13
  67. package/L/run.ts +0 -29
  68. package/L/schema/_schema_common.ts +0 -30
  69. package/L/schema/anyOf.ts +0 -10
  70. package/L/schema/array.ts +0 -13
  71. package/L/schema/boolean.ts +0 -8
  72. package/L/schema/const.ts +0 -18
  73. package/L/schema/enum.ts +0 -18
  74. package/L/schema/integer.ts +0 -6
  75. package/L/schema/null.ts +0 -8
  76. package/L/schema/number.ts +0 -8
  77. package/L/schema/object.ts +0 -21
  78. package/L/schema/string.ts +0 -11
  79. package/L/strand.ts +0 -22
  80. package/L/stream.ts +0 -16
  81. package/L/system.ts +0 -19
  82. package/L/user.ts +0 -19
  83. package/LiminalAssertionError.ts +0 -19
  84. package/Message.ts +0 -16
  85. package/Rune.test.ts +0 -5
  86. package/Rune.ts +0 -60
  87. package/Schema.ts +0 -185
  88. package/Strand.ts +0 -284
  89. package/Tool.ts +0 -26
  90. package/TypeAdapter.ts +0 -3
  91. package/dist/Adapter.d.ts +0 -18
  92. package/dist/Adapter.js +0 -13
  93. package/dist/Adapter.js.map +0 -1
  94. package/dist/AdapterRegistry.d.ts +0 -27
  95. package/dist/AdapterRegistry.js +0 -62
  96. package/dist/AdapterRegistry.js.map +0 -1
  97. package/dist/Config.d.ts +0 -12
  98. package/dist/Config.js +0 -2
  99. package/dist/Config.js.map +0 -1
  100. package/dist/Definition.d.ts +0 -24
  101. package/dist/Definition.js +0 -29
  102. package/dist/Definition.js.map +0 -1
  103. package/dist/EventBase.d.ts +0 -11
  104. package/dist/EventBase.js +0 -16
  105. package/dist/EventBase.js.map +0 -1
  106. package/dist/Handler.d.ts +0 -3
  107. package/dist/Handler.js +0 -2
  108. package/dist/Handler.js.map +0 -1
  109. package/dist/L/L.d.ts +0 -24
  110. package/dist/L/L.js +0 -25
  111. package/dist/L/L.js.map +0 -1
  112. package/dist/L/all.d.ts +0 -14
  113. package/dist/L/all.js +0 -20
  114. package/dist/L/all.js.map +0 -1
  115. package/dist/L/assistant.d.ts +0 -11
  116. package/dist/L/assistant.js +0 -15
  117. package/dist/L/assistant.js.map +0 -1
  118. package/dist/L/catch.d.ts +0 -16
  119. package/dist/L/catch.js +0 -25
  120. package/dist/L/catch.js.map +0 -1
  121. package/dist/L/continuation.d.ts +0 -4
  122. package/dist/L/continuation.js +0 -13
  123. package/dist/L/continuation.js.map +0 -1
  124. package/dist/L/emit.d.ts +0 -7
  125. package/dist/L/emit.js +0 -15
  126. package/dist/L/emit.js.map +0 -1
  127. package/dist/L/focus.d.ts +0 -8
  128. package/dist/L/focus.js +0 -14
  129. package/dist/L/focus.js.map +0 -1
  130. package/dist/L/infer.d.ts +0 -8
  131. package/dist/L/infer.js +0 -30
  132. package/dist/L/infer.js.map +0 -1
  133. package/dist/L/message.d.ts +0 -8
  134. package/dist/L/message.js +0 -14
  135. package/dist/L/message.js.map +0 -1
  136. package/dist/L/reflect.d.ts +0 -5
  137. package/dist/L/reflect.js +0 -11
  138. package/dist/L/reflect.js.map +0 -1
  139. package/dist/L/run.d.ts +0 -14
  140. package/dist/L/run.js +0 -16
  141. package/dist/L/run.js.map +0 -1
  142. package/dist/L/schema/_schema_common.d.ts +0 -6
  143. package/dist/L/schema/_schema_common.js +0 -20
  144. package/dist/L/schema/_schema_common.js.map +0 -1
  145. package/dist/L/schema/anyOf.d.ts +0 -5
  146. package/dist/L/schema/anyOf.js +0 -5
  147. package/dist/L/schema/anyOf.js.map +0 -1
  148. package/dist/L/schema/array.d.ts +0 -5
  149. package/dist/L/schema/array.js +0 -8
  150. package/dist/L/schema/array.js.map +0 -1
  151. package/dist/L/schema/boolean.d.ts +0 -6
  152. package/dist/L/schema/boolean.js +0 -4
  153. package/dist/L/schema/boolean.js.map +0 -1
  154. package/dist/L/schema/const.d.ts +0 -8
  155. package/dist/L/schema/const.js +0 -10
  156. package/dist/L/schema/const.js.map +0 -1
  157. package/dist/L/schema/enum.d.ts +0 -8
  158. package/dist/L/schema/enum.js +0 -10
  159. package/dist/L/schema/enum.js.map +0 -1
  160. package/dist/L/schema/integer.d.ts +0 -5
  161. package/dist/L/schema/integer.js +0 -3
  162. package/dist/L/schema/integer.js.map +0 -1
  163. package/dist/L/schema/null.d.ts +0 -6
  164. package/dist/L/schema/null.js +0 -4
  165. package/dist/L/schema/null.js.map +0 -1
  166. package/dist/L/schema/number.d.ts +0 -6
  167. package/dist/L/schema/number.js +0 -4
  168. package/dist/L/schema/number.js.map +0 -1
  169. package/dist/L/schema/object.d.ts +0 -9
  170. package/dist/L/schema/object.js +0 -12
  171. package/dist/L/schema/object.js.map +0 -1
  172. package/dist/L/schema/string.d.ts +0 -8
  173. package/dist/L/schema/string.js +0 -4
  174. package/dist/L/schema/string.js.map +0 -1
  175. package/dist/L/strand.d.ts +0 -9
  176. package/dist/L/strand.js +0 -16
  177. package/dist/L/strand.js.map +0 -1
  178. package/dist/L/stream.d.ts +0 -4
  179. package/dist/L/stream.js +0 -15
  180. package/dist/L/stream.js.map +0 -1
  181. package/dist/L/system.d.ts +0 -4
  182. package/dist/L/system.js +0 -9
  183. package/dist/L/system.js.map +0 -1
  184. package/dist/L/user.d.ts +0 -4
  185. package/dist/L/user.js +0 -9
  186. package/dist/L/user.js.map +0 -1
  187. package/dist/LiminalAssertionError.d.ts +0 -8
  188. package/dist/LiminalAssertionError.js +0 -20
  189. package/dist/LiminalAssertionError.js.map +0 -1
  190. package/dist/Message.d.ts +0 -14
  191. package/dist/Message.js +0 -2
  192. package/dist/Message.js.map +0 -1
  193. package/dist/Rune.d.ts +0 -46
  194. package/dist/Rune.js +0 -11
  195. package/dist/Rune.js.map +0 -1
  196. package/dist/Rune.test.js +0 -5
  197. package/dist/Rune.test.js.map +0 -1
  198. package/dist/Schema.d.ts +0 -46
  199. package/dist/Schema.js +0 -130
  200. package/dist/Schema.js.map +0 -1
  201. package/dist/Strand.d.ts +0 -81
  202. package/dist/Strand.js +0 -200
  203. package/dist/Strand.js.map +0 -1
  204. package/dist/Tool.d.ts +0 -10
  205. package/dist/Tool.js +0 -19
  206. package/dist/Tool.js.map +0 -1
  207. package/dist/TypeAdapter.d.ts +0 -1
  208. package/dist/TypeAdapter.js +0 -3
  209. package/dist/TypeAdapter.js.map +0 -1
  210. package/dist/errors.d.ts +0 -9
  211. package/dist/errors.js +0 -11
  212. package/dist/errors.js.map +0 -1
  213. package/dist/util/EnsureNarrow.d.ts +0 -1
  214. package/dist/util/EnsureNarrow.js +0 -2
  215. package/dist/util/EnsureNarrow.js.map +0 -1
  216. package/dist/util/attachCustomInspect.d.ts +0 -1
  217. package/dist/util/attachCustomInspect.js +0 -11
  218. package/dist/util/attachCustomInspect.js.map +0 -1
  219. package/errors.ts +0 -12
  220. package/util/EnsureNarrow.ts +0 -1
  221. package/util/attachCustomInspect.ts +0 -14
  222. /package/dist/{Rune.test.d.ts → todo.test.d.ts} +0 -0
package/L/message.ts DELETED
@@ -1,16 +0,0 @@
1
- import { LEvent, MessageAppended } from "../LEvent.ts"
2
- import type { Content, Message, MessageRole } from "../Message.ts"
3
- import type { Rune } from "../Rune.ts"
4
- import { emit } from "./emit.ts"
5
- import { reflect } from "./reflect.ts"
6
-
7
- /**
8
- * Adds a new message to the current context's message list.
9
- * Emits a message append event to observers.
10
- */
11
- export function* message(role: MessageRole, content: Array<Content>): Generator<Rune<LEvent>, void> {
12
- const { context: { messages } } = yield* reflect
13
- const message: Message = { role, parts: content }
14
- yield* emit(new MessageAppended(message))
15
- messages.push(message)
16
- }
package/L/reflect.ts DELETED
@@ -1,13 +0,0 @@
1
- import type { LEvent } from "../LEvent.ts"
2
- import { Rune, RuneKey } from "../Rune.ts"
3
- import type { Strand } from "../Strand.ts"
4
-
5
- /** Returns the current strand instance, providing access to context and parent information. */
6
- export const reflect: Iterable<Rune<LEvent>, Strand> = {
7
- *[Symbol.iterator]() {
8
- return yield {
9
- [RuneKey]: true,
10
- instruction: { kind: "reflect" },
11
- }
12
- },
13
- }
package/L/run.ts DELETED
@@ -1,29 +0,0 @@
1
- import { AdapterRegistry } from "../AdapterRegistry.ts"
2
- import { Context } from "../Context.ts"
3
- import type { Definition } from "../Definition.ts"
4
- import type { Handler } from "../Handler.ts"
5
- import type { Message } from "../Message.ts"
6
- import type { Rune } from "../Rune.ts"
7
- import { Strand } from "../Strand.ts"
8
- import type { Tool } from "../Tool.ts"
9
-
10
- /** Configuration options for running a definition. */
11
- export interface RunConfig<Y extends Rune<any>> {
12
- handler?: Handler<Y> | 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
- adapters: new AdapterRegistry(),
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
- }
@@ -1,30 +0,0 @@
1
- import type { Schema } from "../../Schema.ts"
2
- import { fixTemplateStrings } from "../../util/fixTemplateStrings.ts"
3
- import { isTemplateStringsArray } from "../../util/isTemplateStringsArray.ts"
4
-
5
- export function make<S extends Schema>(schema: Omit<S, "T">, description?: string): S & TypeBase {
6
- const schema_ = {
7
- ...schema,
8
- ...description && { description },
9
- }
10
-
11
- const Type = Object.assign(function describe(e0?: TemplateStringsArray | string, ...rest: Array<string>) {
12
- const junction = isTemplateStringsArray(e0) ? String.raw(fixTemplateStrings(e0), ...rest) : e0
13
- return make(
14
- schema_,
15
- description ? `${description}${junction ? `\n\n${junction}` : ""}` : junction,
16
- )
17
- }, schema_)
18
- Object.defineProperty(Type, "toJSON", {
19
- value() {
20
- return schema_
21
- },
22
- enumerable: false,
23
- })
24
- return Type as never
25
- }
26
-
27
- export interface TypeBase {
28
- (template: TemplateStringsArray, ...substitutions: Array<string>): this
29
- (...values: Array<string>): this
30
- }
package/L/schema/anyOf.ts DELETED
@@ -1,10 +0,0 @@
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
- }
package/L/schema/array.ts DELETED
@@ -1,13 +0,0 @@
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
- }
@@ -1,8 +0,0 @@
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 }
package/L/schema/const.ts DELETED
@@ -1,18 +0,0 @@
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 DELETED
@@ -1,18 +0,0 @@
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 }
@@ -1,6 +0,0 @@
1
- import type { SchemaInteger } from "../../Schema.ts"
2
- import { make, type TypeBase } from "./_schema_common.ts"
3
-
4
- export interface integer extends SchemaInteger<number>, TypeBase {}
5
-
6
- export const integer: integer = make({ type: "integer" })
package/L/schema/null.ts DELETED
@@ -1,8 +0,0 @@
1
- import type { SchemaNull } from "../../Schema.ts"
2
- import { make, type TypeBase } from "./_schema_common.ts"
3
-
4
- export interface null_ extends SchemaNull<null>, TypeBase {}
5
-
6
- const null_: null_ = make({ type: "null" })
7
-
8
- export { null_ as null }
@@ -1,8 +0,0 @@
1
- import type { SchemaNumber } from "../../Schema.ts"
2
- import { make, type TypeBase } from "./_schema_common.ts"
3
-
4
- interface number_ extends SchemaNumber<number>, TypeBase {}
5
-
6
- const number_: number_ = make({ type: "number" })
7
-
8
- export { number_ as number }
@@ -1,21 +0,0 @@
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 }
@@ -1,11 +0,0 @@
1
- import type { SchemaString } from "../../Schema.ts"
2
- import { make, type TypeBase } from "./_schema_common.ts"
3
-
4
- interface string_ extends SchemaString<string>, TypeBase {
5
- enum?: never
6
- const?: never
7
- }
8
-
9
- const string_: string_ = make({ type: "string" })
10
-
11
- export { string_ as string }
package/L/strand.ts DELETED
@@ -1,22 +0,0 @@
1
- import type { Context } from "../Context.ts"
2
- import type { Definition } from "../Definition.ts"
3
- import type { LEvent } from "../LEvent.ts"
4
- import { type Rune, RuneKey } from "../Rune.ts"
5
-
6
- /**
7
- * Creates a child strand that executes a definition.
8
- * Optionally specify the context. Otherwise the current context is inherited.
9
- */
10
- export function* strand<Y extends Rune<any>, T>(
11
- definition: Definition<Y, T>,
12
- context?: Context,
13
- ): Generator<Rune<LEvent>, T> {
14
- return yield {
15
- [RuneKey]: true,
16
- instruction: {
17
- kind: "create_child",
18
- definition,
19
- context,
20
- },
21
- }
22
- }
package/L/stream.ts DELETED
@@ -1,16 +0,0 @@
1
- import { InferenceRequested, type LEvent } from "../LEvent.ts"
2
- import type { Rune } from "../Rune.ts"
3
- import { continuation } from "./continuation.ts"
4
- import { emit } from "./emit.ts"
5
- import { reflect } from "./reflect.ts"
6
-
7
- /** Creates a readable stream of content from the current model. */
8
- export const stream: Iterable<Rune<LEvent>, ReadableStream<string>> = {
9
- *[Symbol.iterator]() {
10
- const { context: { adapters, messages }, signal } = yield* reflect
11
- const adapter = adapters.ensure()
12
- const requestId = crypto.randomUUID()
13
- yield* emit(new InferenceRequested(requestId))
14
- return yield* continuation("stream", adapter.seal({ messages, signal }).stream)
15
- },
16
- }
package/L/system.ts DELETED
@@ -1,19 +0,0 @@
1
- import type { LEvent } from "../LEvent.ts"
2
- import type { Rune } from "../Rune.ts"
3
- import { fixTemplateStrings } from "../util/fixTemplateStrings.ts"
4
- import { isTemplateStringsArray } from "../util/isTemplateStringsArray.ts"
5
- import { message } from "./message.ts"
6
-
7
- export function system(
8
- template: TemplateStringsArray,
9
- ...substitutions: Array<number | string>
10
- ): Generator<Rune<LEvent>, void>
11
- export function system(value: string): Generator<Rune<LEvent>, void>
12
- export function system(
13
- e0: TemplateStringsArray | string,
14
- ...rest: Array<number | string>
15
- ): Generator<Rune<LEvent>, void> {
16
- return message("system", [{
17
- part: isTemplateStringsArray(e0) ? String.raw(fixTemplateStrings(e0), ...rest) : e0,
18
- }])
19
- }
package/L/user.ts DELETED
@@ -1,19 +0,0 @@
1
- import type { LEvent } from "../LEvent.ts"
2
- import type { Rune } from "../Rune.ts"
3
- import { fixTemplateStrings } from "../util/fixTemplateStrings.ts"
4
- import { isTemplateStringsArray } from "../util/isTemplateStringsArray.ts"
5
- import { message } from "./message.ts"
6
-
7
- export function user(
8
- template: TemplateStringsArray,
9
- ...substitutions: Array<number | string>
10
- ): Generator<Rune<LEvent>, void>
11
- export function user(value: string): Generator<Rune<LEvent>, void>
12
- export function user(
13
- e0: TemplateStringsArray | string,
14
- ...rest: Array<number | string>
15
- ): Generator<Rune<LEvent>, void> {
16
- return message("user", [{
17
- part: isTemplateStringsArray(e0) ? String.raw(fixTemplateStrings(e0), ...rest) : e0,
18
- }])
19
- }
@@ -1,19 +0,0 @@
1
- export class LiminalAssertionError extends Error {
2
- override readonly name = "LiminalAssertionError"
3
- }
4
-
5
- export namespace LiminalAssertionError {
6
- export function assert(expr: unknown, msg = ""): asserts expr {
7
- if (!expr) {
8
- throw new LiminalAssertionError(msg)
9
- }
10
- }
11
-
12
- export function unimplemented(): never {
13
- throw new LiminalAssertionError()
14
- }
15
-
16
- export function unreachable(): never {
17
- throw new LiminalAssertionError()
18
- }
19
- }
package/Message.ts DELETED
@@ -1,16 +0,0 @@
1
- export interface Message {
2
- readonly role: MessageRole
3
- readonly parts: Array<Content>
4
- }
5
-
6
- export type MessageRole = "system" | "user" | "assistant"
7
-
8
- export type Content = {
9
- readonly part: string
10
- readonly alt?: never
11
- readonly mime?: never
12
- } | {
13
- readonly part: URL
14
- readonly alt: string
15
- readonly mime?: string
16
- }
package/Rune.test.ts DELETED
@@ -1,5 +0,0 @@
1
- import { expect, test } from "vitest"
2
-
3
- test("adds 1 + 2 to equal 3", () => {
4
- expect(1 + 2).toBe(3)
5
- })
package/Rune.ts DELETED
@@ -1,60 +0,0 @@
1
- import type { Context } from "./Context.ts"
2
- import type { Definition } from "./Definition.ts"
3
-
4
- /**
5
- * `Rune` represents a computational unit within a Strand execution.
6
- * It is a specialized protocol for expressing different kinds of operations
7
- * that can be yielded from generator functions to control message state
8
- * and concurrency within the Liminal runtime.
9
- * @template E The event type this Rune can handle
10
- */
11
- export interface Rune<E> {
12
- [RuneKey]: true
13
- instruction: RuneInstruction<E>
14
- }
15
-
16
- export type RuneInstruction<E> =
17
- | RuneInstruction.Continue
18
- | RuneInstruction.Emit<E>
19
- | RuneInstruction.Reflect
20
- | RuneInstruction.CreateChild
21
- export namespace RuneInstruction {
22
- /** Tells the runtime to call this function and and pass the result to the next iteration. */
23
- export interface Continue {
24
- kind: "continue"
25
- debug: string
26
- f: () => any
27
- }
28
-
29
- /** Dispatch an event to the registered handler. */
30
- export interface Emit<E> {
31
- kind: "emit"
32
- event: E
33
- }
34
-
35
- /** Tells the runtime to provide a reference to the current strand */
36
- export interface Reflect {
37
- kind: "reflect"
38
- }
39
-
40
- /** Creates a nested execution context with its own definition. */
41
- export interface CreateChild {
42
- kind: "create_child"
43
- definition: Definition
44
- context?: Context | undefined
45
- }
46
- }
47
-
48
- export namespace Rune {
49
- /** Extract the event type from a Rune type. */
50
- export type E<X extends Rune<any>> = X extends Rune<infer E> ? E : never
51
-
52
- /** Type guard to check if a value is a Rune. */
53
- export function is(value: unknown): value is Rune<any> {
54
- return typeof value === "object" && value !== null && RuneKey in value
55
- }
56
- }
57
-
58
- /** Symbol used to identify Rune objects. */
59
- export const RuneKey: unique symbol = Symbol.for("liminal/RuneKey")
60
- export type RuneKey = typeof RuneKey
package/Schema.ts DELETED
@@ -1,185 +0,0 @@
1
- import { subtle } from "node:crypto"
2
- import { LiminalAssertionError as LE } from "./LiminalAssertionError.ts"
3
-
4
- export type Schema<T = any> = SchemaType<T> | SchemaAnyOf<T>
5
-
6
- export type SchemaType<T = any> =
7
- | SchemaNull<T>
8
- | SchemaBoolean<T>
9
- | SchemaInteger<T>
10
- | SchemaNumber<T>
11
- | SchemaString<T>
12
- | SchemaArray<T>
13
- | SchemaObject<T>
14
-
15
- export interface SchemaNull<T = any> extends SchemaTypeBase<"null", T> {}
16
-
17
- export interface SchemaBoolean<T = any> extends SchemaTypeBase<"boolean", T> {}
18
-
19
- export interface SchemaInteger<T = any> extends SchemaTypeBase<"integer", T> {}
20
-
21
- export interface SchemaNumber<T = any> extends SchemaTypeBase<"number", T> {}
22
-
23
- export interface SchemaString<T = any> extends SchemaTypeBase<"string", T> {
24
- enum?: Array<string>
25
- const?: string
26
- }
27
-
28
- export interface SchemaArray<T = any> extends SchemaTypeBase<"array", T> {
29
- items: Schema
30
- }
31
-
32
- export interface SchemaObject<T = any> extends SchemaTypeBase<"object", T> {
33
- properties: Record<string, Schema>
34
- required: Array<string>
35
- additionalProperties: false
36
- }
37
-
38
- interface SchemaTypeBase<K extends SchemaTypeName, T> extends SchemaBase<T> {
39
- type: K
40
- anyOf?: never
41
- }
42
-
43
- export type SchemaTypeName = "null" | "boolean" | "integer" | "number" | "string" | "array" | "object"
44
-
45
- export interface SchemaAnyOf<T = any> extends SchemaBase<T> {
46
- type?: never
47
- anyOf: Array<Schema>
48
- }
49
-
50
- export interface SchemaBase<T> {
51
- T: T
52
- description?: string
53
- }
54
-
55
- export namespace Schema {
56
- const ids = new WeakMap<Schema, Record<string, string>>()
57
- const schemas = new WeakMap<WeakKey, Schema>()
58
- const validators = new WeakMap<Schema, (value: unknown) => unknown>()
59
-
60
- export function compile<T>(key: WeakKey, {
61
- schema: schema_,
62
- validate,
63
- }: {
64
- schema(): unknown
65
- validate(value: unknown): T
66
- }) {
67
- let schema = schemas.get(key)
68
- if (!schema) {
69
- schema = Schema.validate(schema_())
70
- schemas.set(key, schema)
71
- }
72
- validators.set(schema, validate)
73
- return schema
74
- }
75
-
76
- export function validateValue<T>(schema: Schema<T>, value: unknown): T {
77
- const validator = validators.get(schema)
78
- LE.assert(validator)
79
- return validator(value) as never
80
- }
81
-
82
- export async function id(schema: Schema, description?: string): Promise<string> {
83
- description = description ?? ""
84
- if (!ids.has(schema)) {
85
- ids.set(schema, {})
86
- }
87
- const schemaIds = ids.get(schema)!
88
- let id = schemaIds[description]
89
- if (!id) {
90
- const content = `${description}_${JSON.stringify(schema)}`
91
- const bytes = new Uint8Array(await subtle.digest("SHA-256", new TextEncoder().encode(content)))
92
- let binary = ""
93
- for (const b of bytes) binary += String.fromCharCode(b)
94
- id = btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "")
95
- schemaIds[description] = id
96
- }
97
- return id
98
- }
99
-
100
- export function wrap(schema: Schema): SchemaObject {
101
- return {
102
- type: "object",
103
- properties: { value: schema },
104
- additionalProperties: false,
105
- required: ["value"],
106
- } satisfies Omit<SchemaObject, "T"> as never
107
- }
108
-
109
- export function validate(value: unknown): Schema {
110
- LE.assert(typeof value === "object")
111
- LE.assert(value !== null)
112
- if ("anyOf" in value) {
113
- LE.assert(Array.isArray(value.anyOf))
114
- return {
115
- anyOf: value.anyOf.map(validate),
116
- } satisfies Omit<SchemaAnyOf, "T"> as never
117
- } else {
118
- if ("enum" in value) {
119
- LE.assert(Array.isArray(value.enum))
120
- LE.assert(!("const" in value))
121
- value.enum.forEach((v) => LE.assert(typeof v === "string"))
122
- return {
123
- type: "string",
124
- enum: value.enum,
125
- } satisfies Omit<SchemaString, "T"> as never
126
- } else if ("const" in value) {
127
- LE.assert(typeof value.const === "string")
128
- return {
129
- type: "string",
130
- const: value.const,
131
- } satisfies Omit<SchemaString, "T"> as never
132
- } else if ("type" in value) {
133
- LE.assert("type" in value)
134
- LE.assert(typeof value.type === "string")
135
- LE.assert(SCHEMA_TYPE_NAMES[value.type])
136
- switch (value.type) {
137
- case "null":
138
- case "boolean":
139
- case "integer":
140
- case "number": {
141
- break
142
- }
143
- case "string": {
144
- if ("const" in value) {
145
- LE.assert(typeof value.const === "string")
146
- }
147
- break
148
- }
149
- case "array": {
150
- LE.assert("items" in value)
151
- return {
152
- type: "array",
153
- items: validate(value.items),
154
- } satisfies Omit<SchemaArray, "T"> as never
155
- }
156
- case "object": {
157
- LE.assert("properties" in value)
158
- LE.assert(typeof value.properties === "object")
159
- const { properties } = value
160
- LE.assert(properties !== null)
161
- return {
162
- type: "object",
163
- properties: Object.fromEntries(
164
- Object.entries(properties).map(([key, value]) => [key, validate(value)]),
165
- ),
166
- required: Object.keys(properties),
167
- additionalProperties: false,
168
- } satisfies Omit<SchemaObject, "T"> as never
169
- }
170
- }
171
- }
172
- }
173
- return value as Schema
174
- }
175
- }
176
-
177
- const SCHEMA_TYPE_NAMES: Record<string, boolean> = {
178
- null: true,
179
- boolean: true,
180
- integer: true,
181
- number: true,
182
- string: true,
183
- array: true,
184
- object: true,
185
- } satisfies Record<SchemaTypeName, true>