liminal 0.5.1 → 0.5.2

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 (196) hide show
  1. package/Agent.ts +10 -0
  2. package/CHANGELOG.md +6 -0
  3. package/Exec.ts +19 -31
  4. package/L.ts +4 -7
  5. package/LiminalConfig.ts +1 -1
  6. package/Model.ts +8 -2
  7. package/PathLike.ts +8 -10
  8. package/Scope.ts +33 -14
  9. package/Tool.ts +3 -3
  10. package/actions/actions_derived/clear.ts +2 -2
  11. package/actions/actions_derived/declareModel.test.ts +10 -7
  12. package/actions/actions_derived/declareModel.ts +3 -3
  13. package/actions/actions_derived/messages.ts +0 -1
  14. package/actions/actions_derived/readers_derived.ts +15 -0
  15. package/actions/appendMessage.ts +27 -3
  16. package/actions/branch.ts +31 -33
  17. package/actions/catch.ts +3 -3
  18. package/actions/embed.ts +2 -1
  19. package/actions/enableTool.ts +2 -2
  20. package/actions/enableTool.type-test.ts +7 -7
  21. package/actions/infer.ts +15 -3
  22. package/actions/messages.ts +62 -0
  23. package/actions/pushModel.ts +22 -2
  24. package/actions/scope.ts +13 -0
  25. package/actions/section.ts +105 -0
  26. package/cli/cli_common.ts +1 -0
  27. package/cli/main.ts +6 -1
  28. package/cli/runExec.ts +20 -16
  29. package/dist/Agent.d.ts +7 -0
  30. package/dist/Agent.js +2 -0
  31. package/dist/Agent.js.map +1 -0
  32. package/dist/Exec.d.ts +5 -7
  33. package/dist/Exec.js +14 -17
  34. package/dist/Exec.js.map +1 -1
  35. package/dist/L.d.ts +4 -7
  36. package/dist/L.js +4 -7
  37. package/dist/L.js.map +1 -1
  38. package/dist/LiminalConfig.d.ts +1 -1
  39. package/dist/Model.d.ts +7 -2
  40. package/dist/PathLike.d.ts +4 -7
  41. package/dist/Scope.d.ts +11 -5
  42. package/dist/Scope.js +8 -5
  43. package/dist/Scope.js.map +1 -1
  44. package/dist/Tool.d.ts +2 -2
  45. package/dist/actions/actions_derived/clear.js +2 -2
  46. package/dist/actions/actions_derived/clear.js.map +1 -1
  47. package/dist/actions/actions_derived/declareModel.d.ts +2 -2
  48. package/dist/actions/actions_derived/declareModel.js +1 -1
  49. package/dist/actions/actions_derived/declareModel.js.map +1 -1
  50. package/dist/actions/actions_derived/declareModel.test.js +10 -7
  51. package/dist/actions/actions_derived/declareModel.test.js.map +1 -1
  52. package/dist/actions/actions_derived/messages.d.ts +3 -3
  53. package/dist/actions/actions_derived/messages.js.map +1 -1
  54. package/dist/actions/actions_derived/readers_derived.d.ts +6 -0
  55. package/dist/actions/actions_derived/readers_derived.js +14 -0
  56. package/dist/actions/actions_derived/readers_derived.js.map +1 -0
  57. package/dist/actions/appendMessage.d.ts +6 -2
  58. package/dist/actions/appendMessage.js +23 -2
  59. package/dist/actions/appendMessage.js.map +1 -1
  60. package/dist/actions/branch.d.ts +18 -14
  61. package/dist/actions/branch.js +10 -10
  62. package/dist/actions/branch.js.map +1 -1
  63. package/dist/actions/catch.d.ts +2 -2
  64. package/dist/actions/catch.js +2 -2
  65. package/dist/actions/embed.js +2 -1
  66. package/dist/actions/embed.js.map +1 -1
  67. package/dist/actions/enableTool.d.ts +2 -2
  68. package/dist/actions/enableTool.type-test.js +5 -5
  69. package/dist/actions/infer.d.ts +3 -1
  70. package/dist/actions/infer.js +11 -3
  71. package/dist/actions/infer.js.map +1 -1
  72. package/dist/actions/messages.d.ts +24 -0
  73. package/dist/actions/{setMessages.js → messages.js} +14 -10
  74. package/dist/actions/messages.js.map +1 -0
  75. package/dist/actions/pushModel.d.ts +5 -1
  76. package/dist/actions/pushModel.js +17 -2
  77. package/dist/actions/pushModel.js.map +1 -1
  78. package/dist/actions/{getScope.d.ts → scope.d.ts} +3 -1
  79. package/dist/actions/scope.js +10 -0
  80. package/dist/actions/scope.js.map +1 -0
  81. package/dist/actions/section.d.ts +23 -0
  82. package/dist/actions/section.js +64 -0
  83. package/dist/actions/section.js.map +1 -0
  84. package/dist/cli/cli_common.d.ts +1 -0
  85. package/dist/cli/main.js +6 -1
  86. package/dist/cli/main.js.map +1 -1
  87. package/dist/cli/runExec.js +21 -16
  88. package/dist/cli/runExec.js.map +1 -1
  89. package/dist/events/LEvent.d.ts +3 -1
  90. package/dist/events/SectionCleared.d.ts +5 -0
  91. package/dist/events/SectionCleared.js +2 -0
  92. package/dist/events/SectionCleared.js.map +1 -0
  93. package/dist/events/Sectioned.d.ts +5 -0
  94. package/dist/events/Sectioned.js +2 -0
  95. package/dist/events/Sectioned.js.map +1 -0
  96. package/dist/index.d.ts +1 -1
  97. package/dist/index.js +1 -1
  98. package/dist/testing/{ActorAssertions.d.ts → AgentAssertions.d.ts} +3 -3
  99. package/dist/testing/AgentAssertions.js +6 -0
  100. package/dist/testing/AgentAssertions.js.map +1 -0
  101. package/dist/testing/TestEmbeddingModel.js +1 -0
  102. package/dist/testing/TestEmbeddingModel.js.map +1 -1
  103. package/dist/testing/TestLanguageModel.js +1 -0
  104. package/dist/testing/TestLanguageModel.js.map +1 -1
  105. package/dist/testing/index.d.ts +1 -1
  106. package/dist/testing/index.js +1 -1
  107. package/dist/testing/takeFirstAction.d.ts +2 -2
  108. package/dist/tsconfig.tsbuildinfo +1 -1
  109. package/dist/types/Type.d.ts +1 -0
  110. package/dist/types/Type.js.map +1 -1
  111. package/dist/types/array.js +2 -2
  112. package/dist/types/array.js.map +1 -1
  113. package/dist/types/boolean.js +2 -2
  114. package/dist/types/boolean.js.map +1 -1
  115. package/dist/types/const.js +2 -2
  116. package/dist/types/const.js.map +1 -1
  117. package/dist/types/enum.js +2 -2
  118. package/dist/types/enum.js.map +1 -1
  119. package/dist/types/integer.js +2 -2
  120. package/dist/types/integer.js.map +1 -1
  121. package/dist/types/makeType.d.ts +2 -0
  122. package/dist/types/{declareType.js → makeType.js} +4 -3
  123. package/dist/types/makeType.js.map +1 -0
  124. package/dist/types/null.js +2 -2
  125. package/dist/types/null.js.map +1 -1
  126. package/dist/types/number.js +2 -2
  127. package/dist/types/number.js.map +1 -1
  128. package/dist/types/object.js +2 -2
  129. package/dist/types/object.js.map +1 -1
  130. package/dist/types/ref.js +2 -2
  131. package/dist/types/ref.js.map +1 -1
  132. package/dist/types/string.js +2 -2
  133. package/dist/types/string.js.map +1 -1
  134. package/dist/types/union.js +2 -2
  135. package/dist/types/union.js.map +1 -1
  136. package/dist/util/peekLast.d.ts +1 -0
  137. package/dist/util/peekLast.js +4 -0
  138. package/dist/util/peekLast.js.map +1 -0
  139. package/events/LEvent.ts +4 -0
  140. package/events/SectionCleared.ts +6 -0
  141. package/events/Sectioned.ts +6 -0
  142. package/index.ts +1 -1
  143. package/package.json +1 -1
  144. package/testing/{ActorAssertions.ts → AgentAssertions.ts} +3 -5
  145. package/testing/TestEmbeddingModel.ts +1 -0
  146. package/testing/TestLanguageModel.ts +1 -0
  147. package/testing/index.ts +1 -1
  148. package/testing/takeFirstAction.ts +2 -2
  149. package/types/Type.ts +1 -0
  150. package/types/array.ts +2 -2
  151. package/types/boolean.ts +2 -2
  152. package/types/const.ts +2 -2
  153. package/types/enum.ts +2 -2
  154. package/types/integer.ts +2 -2
  155. package/types/{declareType.ts → makeType.ts} +3 -2
  156. package/types/null.ts +2 -2
  157. package/types/number.ts +2 -2
  158. package/types/object.ts +2 -2
  159. package/types/ref.ts +2 -2
  160. package/types/string.ts +2 -2
  161. package/types/union.ts +2 -2
  162. package/util/peekLast.ts +3 -0
  163. package/Actor.ts +0 -20
  164. package/actions/actions_derived/getMessages.ts +0 -9
  165. package/actions/getScope.ts +0 -11
  166. package/actions/getSignal.ts +0 -10
  167. package/actions/getTools.ts +0 -11
  168. package/actions/removeMessage.ts +0 -22
  169. package/actions/removeModel.ts +0 -31
  170. package/actions/setMessages.ts +0 -66
  171. package/dist/Actor.d.ts +0 -14
  172. package/dist/Actor.js +0 -2
  173. package/dist/Actor.js.map +0 -1
  174. package/dist/actions/actions_derived/getMessages.d.ts +0 -3
  175. package/dist/actions/actions_derived/getMessages.js +0 -8
  176. package/dist/actions/actions_derived/getMessages.js.map +0 -1
  177. package/dist/actions/getScope.js +0 -8
  178. package/dist/actions/getScope.js.map +0 -1
  179. package/dist/actions/getSignal.d.ts +0 -4
  180. package/dist/actions/getSignal.js +0 -8
  181. package/dist/actions/getSignal.js.map +0 -1
  182. package/dist/actions/getTools.d.ts +0 -5
  183. package/dist/actions/getTools.js +0 -8
  184. package/dist/actions/getTools.js.map +0 -1
  185. package/dist/actions/removeMessage.d.ts +0 -9
  186. package/dist/actions/removeMessage.js +0 -17
  187. package/dist/actions/removeMessage.js.map +0 -1
  188. package/dist/actions/removeModel.d.ts +0 -10
  189. package/dist/actions/removeModel.js +0 -20
  190. package/dist/actions/removeModel.js.map +0 -1
  191. package/dist/actions/setMessages.d.ts +0 -16
  192. package/dist/actions/setMessages.js.map +0 -1
  193. package/dist/testing/ActorAssertions.js +0 -6
  194. package/dist/testing/ActorAssertions.js.map +0 -1
  195. package/dist/types/declareType.d.ts +0 -2
  196. package/dist/types/declareType.js.map +0 -1
package/Agent.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { Action } from "./Action.ts"
2
+ import type { DeferredOr } from "./util/DeferredOr.ts"
3
+ import type { IteratorLike } from "./util/IteratorLike.ts"
4
+
5
+ export type Agent<Y extends Action = Action, R = any> = IteratorLike<Y, R>
6
+
7
+ export type AgentLike<Y extends Action = Action, R = any> = DeferredOr<Agent<Y, R>>
8
+
9
+ export type AgentLikeY<A extends AgentLike> = A extends AgentLike<infer Y> ? Y : never
10
+ export type AgentLikeT<A extends AgentLike> = A extends AgentLike<Action, infer T> ? T : never
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # liminal
2
2
 
3
+ ## 0.5.2
4
+
5
+ ### Patch Changes
6
+
7
+ - de74823: Various API changes, such as infer as a callable object (the object of which implements Symbol.iterator and yields the actual action). Introduced initial "section" action. Also created an adapter for Ollama.
8
+
3
9
  ## 0.5.1
4
10
 
5
11
  ### Patch Changes
package/Exec.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Action } from "./Action.ts"
2
- import type { Actor } from "./Actor.ts"
2
+ import type { Agent } from "./Agent.ts"
3
3
  import type { EventHandler } from "./events/EventHandler.ts"
4
4
  import type { EventResolved, ExtractEventResolved } from "./events/EventResolved.ts"
5
5
  import type { LanguageModel } from "./Model.ts"
@@ -7,27 +7,18 @@ import { RootScope, type Scope } from "./Scope.ts"
7
7
  import type { FromEntries } from "./util/FromEntries.ts"
8
8
  import type { JSONKey } from "./util/JSONKey.ts"
9
9
 
10
- export interface Exec<Y extends Action = Action, T = any> {
11
- (
12
- handler?: EventHandler<
13
- Extract<ExtractEventResolved<Y[""]> & {}, EventResolved>
14
- >,
15
- options?: ExecOptions,
16
- ): Promise<T>
17
- }
18
-
19
10
  export interface ExecConfig {
20
11
  default: LanguageModel
21
12
  args?: Record<JSONKey, any>
22
- }
23
-
24
- export interface ExecOptions {
25
13
  signal?: AbortSignal
14
+ handler?: EventHandler
26
15
  }
27
16
 
28
17
  export type ExtractExecConfig<Y extends Action> =
29
18
  & {
30
19
  default: LanguageModel
20
+ signal?: AbortSignal
21
+ handler?: EventHandler<Extract<ExtractEventResolved<Y[""]> & {}, EventResolved>>
31
22
  }
32
23
  & (
33
24
  [Y[""]["Entry"]] extends [never] ? {
@@ -38,26 +29,23 @@ export type ExtractExecConfig<Y extends Action> =
38
29
  }
39
30
  )
40
31
 
41
- export function Exec<Y extends Action, T>(
42
- createActor: () => Actor<Y, T>,
32
+ export async function exec<Y extends Action, T>(
33
+ createAgent: () => Agent<Y, T>,
43
34
  config: ExtractExecConfig<Y>,
44
- ): Exec<Y, T> {
45
- // TODO: consider `Result` type.
46
- return async (handler, options) => {
47
- let scope: Scope = RootScope(config.default, config.args, handler as never, options?.signal)
48
- scope = await scope.reduce(createActor())
49
- const { signal: { aborted, reason } } = scope.controller
50
- if (aborted) {
51
- scope.event({
52
- type: "aborted",
53
- reason,
54
- })
55
- throw reason
56
- }
35
+ ): Promise<T> {
36
+ let scope: Scope = RootScope(config as never)
37
+ scope = await scope.reduce(createAgent())
38
+ const { signal: { aborted, reason } } = scope.controller
39
+ if (aborted) {
57
40
  scope.event({
58
- type: "returned",
59
- value: scope.value,
41
+ type: "aborted",
42
+ reason,
60
43
  })
61
- return scope.value
44
+ throw reason
62
45
  }
46
+ scope.event({
47
+ type: "returned",
48
+ value: scope.value,
49
+ })
50
+ return scope.value
63
51
  }
package/L.ts CHANGED
@@ -2,9 +2,8 @@ export * from "./_.ts"
2
2
  export * from "./actions/abort.ts"
3
3
  export * from "./actions/actions_derived/clear.ts"
4
4
  export * from "./actions/actions_derived/declareModel.ts"
5
- export * from "./actions/actions_derived/declareModel.ts"
6
- export * from "./actions/actions_derived/getMessages.ts"
7
5
  export * from "./actions/actions_derived/messages.ts"
6
+ export * from "./actions/actions_derived/readers_derived.ts"
8
7
  export * from "./actions/appendMessage.ts"
9
8
  export * from "./actions/await.ts"
10
9
  export * from "./actions/branch.ts"
@@ -14,13 +13,11 @@ export * from "./actions/disableTool.ts"
14
13
  export * from "./actions/embed.ts"
15
14
  export * from "./actions/emit.ts"
16
15
  export * from "./actions/enableTool.ts"
17
- export * from "./actions/getScope.ts"
18
- export * from "./actions/getSignal.ts"
19
- export * from "./actions/getTools.ts"
20
16
  export * from "./actions/infer.ts"
17
+ export * from "./actions/messages.ts"
21
18
  export * from "./actions/pushModel.ts"
22
- export * from "./actions/removeModel.ts"
23
- export * from "./actions/setMessages.ts"
19
+ export * from "./actions/scope.ts"
20
+ export * from "./actions/section.ts"
24
21
  export * from "./actions/throw.ts"
25
22
  export * from "./types/factories.ts"
26
23
 
package/LiminalConfig.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { ExecConfig } from "./Exec.ts"
2
2
 
3
3
  export interface LiminalConfig extends ExecConfig {
4
- actors?: string
4
+ agents?: string
5
5
  silent?: boolean
6
6
  write?: boolean | string
7
7
  }
package/Model.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import type { StandardSchemaV1 } from "@standard-schema/spec"
2
2
  import type { Action } from "./Action.ts"
3
- import type { Actor } from "./Actor.ts"
3
+ import type { Agent } from "./Agent.ts"
4
4
  import type { JSONObject } from "./util/JSONObject.ts"
5
5
 
6
6
  export type Model = LanguageModel | EmbeddingModel
7
7
 
8
8
  export interface LanguageModel extends ModelBase<"language"> {
9
- infer: (type: StandardSchemaV1<JSONObject, any> | undefined) => Actor<Action, any>
9
+ infer: (type: StandardSchemaV1<JSONObject, any> | undefined) => Agent<Action, any>
10
10
  }
11
11
 
12
12
  export interface EmbeddingModel extends ModelBase<"embedding"> {
@@ -15,6 +15,12 @@ export interface EmbeddingModel extends ModelBase<"embedding"> {
15
15
 
16
16
  interface ModelBase<K extends ModelType> {
17
17
  type: K
18
+ vendor: string
18
19
  }
19
20
 
20
21
  export type ModelType = "language" | "embedding"
22
+
23
+ export type Models = {
24
+ language: LanguageModel
25
+ embedding: EmbeddingModel
26
+ }
package/PathLike.ts CHANGED
@@ -3,17 +3,15 @@ import type { JSONKey } from "./util/JSONKey.ts"
3
3
 
4
4
  export type Path = Array<JSONKey>
5
5
  export declare namespace Path {
6
- export type FromPathLike<P extends PathLike> = Extract<
7
- {
8
- [K in keyof P]: P[K] extends _ ? JSONKey : P[K]
9
- },
10
- Path
11
- >
6
+ export type FromPathLike<P extends PathLike> = P extends [infer E0 extends KeyLike, ...infer ERest extends PathLike]
7
+ ? [E0 extends JSONKey ? E0 : JSONKey, ...FromPathLike<ERest>]
8
+ : []
12
9
  }
13
10
 
14
- export type PathLike = Array<JSONKey | _>
11
+ export type KeyLike = JSONKey | _
12
+ export type PathLike = Array<KeyLike>
15
13
  export declare namespace PathLike {
16
- export type FromPath<P extends Path> = {
17
- [K in keyof P]: P[K] | _
18
- }
14
+ export type FromPath<P extends Path> = P extends [infer E0, ...infer ERest extends Path]
15
+ ? [E0 | _, ...FromPath<ERest>]
16
+ : []
19
17
  }
package/Scope.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import type { Action } from "./Action.ts"
2
- import type { Actor } from "./Actor.ts"
2
+ import type { Agent } from "./Agent.ts"
3
3
  import type { EventHandler } from "./events/EventHandler.ts"
4
4
  import type { LEvent } from "./events/LEvent.ts"
5
+ import type { ExecConfig } from "./Exec.ts"
5
6
  import type { Message } from "./Message.ts"
6
7
  import type { EmbeddingModel, LanguageModel } from "./Model.ts"
7
8
  import type { Tool } from "./Tool.ts"
@@ -15,7 +16,7 @@ export interface ChildScope extends ScopeBase<ChildScopeType> {
15
16
  }
16
17
 
17
18
  export type RootScopeType = "root"
18
- export type ChildScopeType = "catch" | "tool" | "branch" | "branch_arm" | "set_messages"
19
+ export type ChildScopeType = "catch" | "tool" | "branch" | "branch_arm" | "set_messages" | "run_section"
19
20
  export type ScopeType = RootScopeType | ChildScopeType
20
21
 
21
22
  export interface ScopeBase<Type extends ScopeType> {
@@ -33,18 +34,28 @@ export interface ScopeBase<Type extends ScopeType> {
33
34
  readonly handler?: EventHandler
34
35
  readonly childForkCounts: Record<JSONKey, number>
35
36
  readonly index: number
37
+ readonly sections: Set<Section>
36
38
 
37
- reduce(actor: Actor): Promise<Scope>
38
- fork(source: ChildScopeType, subpath: Array<JSONKey>): Scope
39
+ reduce(agent: Agent): Promise<Scope>
40
+ fork(
41
+ source: ChildScopeType,
42
+ subpath: Array<JSONKey>,
43
+ overrides?: Partial<ChildScope>,
44
+ ): Scope
39
45
  event(event: LEvent): void
40
46
  }
41
47
 
42
- export function RootScope(
43
- defaultLanguageModel: LanguageModel,
44
- args?: Record<JSONKey, any>,
45
- handler: EventHandler = () => {},
46
- signal?: AbortSignal,
47
- ): RootScope {
48
+ export interface Section {
49
+ sectionKey: JSONKey
50
+ messages: Set<Message>
51
+ }
52
+
53
+ export function RootScope({
54
+ signal,
55
+ args,
56
+ handler,
57
+ default: defaultLanguageModel,
58
+ }: ExecConfig): RootScope {
48
59
  const controller = new AbortController()
49
60
  signal?.addEventListener("abort", () => {
50
61
  controller.abort()
@@ -60,6 +71,7 @@ export function RootScope(
60
71
  languageModels: new Set([defaultLanguageModel]),
61
72
  embeddingModels: new Set(),
62
73
  childForkCounts: {},
74
+ sections: new Set(),
63
75
  index: 0,
64
76
  reduce,
65
77
  fork,
@@ -68,19 +80,19 @@ export function RootScope(
68
80
  }
69
81
  }
70
82
 
71
- async function reduce(this: Scope, actor: Actor): Promise<Scope> {
83
+ async function reduce(this: Scope, agent: Agent): Promise<Scope> {
72
84
  const { signal } = this.controller
73
85
  let scope = { ...this }
74
86
  if (signal.aborted) return scope
75
87
  let value: unknown
76
88
  try {
77
- let current = await actor.next()
89
+ let current = await agent.next()
78
90
  while (!current.done) {
79
91
  const { value } = current
80
92
  if (signal.aborted) return scope
81
93
  scope = await (value as Action).reducer(scope)
82
94
  if (signal.aborted) return scope
83
- current = await actor.next(scope.nextArg)
95
+ current = await agent.next(scope.nextArg)
84
96
  }
85
97
  value = current.value
86
98
  } catch (thrown: unknown) {
@@ -93,7 +105,12 @@ async function reduce(this: Scope, actor: Actor): Promise<Scope> {
93
105
  }
94
106
  }
95
107
 
96
- function fork(this: Scope, type: ChildScopeType, subpath: Array<JSONKey>): ChildScope {
108
+ function fork(
109
+ this: Scope,
110
+ type: ChildScopeType,
111
+ subpath: Array<JSONKey>,
112
+ overrides?: Partial<ChildScope>,
113
+ ): ChildScope {
97
114
  const subpathStr = subpath.join(" ")
98
115
  let index = this.childForkCounts[subpathStr]
99
116
  if (typeof index === "number") {
@@ -116,9 +133,11 @@ function fork(this: Scope, type: ChildScopeType, subpath: Array<JSONKey>): Child
116
133
  parent: this,
117
134
  value: undefined,
118
135
  index,
136
+ sections: new Set(this.sections),
119
137
  reduce,
120
138
  fork,
121
139
  event,
140
+ ...overrides ?? {},
122
141
  }
123
142
  f.event({ type: "forked" })
124
143
  return f
package/Tool.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { StandardSchemaV1 } from "@standard-schema/spec"
2
2
  import type { Action } from "./Action.ts"
3
- import type { Actor } from "./Actor.ts"
3
+ import type { Agent } from "./Agent.ts"
4
4
  import type { Scope } from "./Scope.ts"
5
5
  import { assert } from "./util/assert.ts"
6
6
  import { isJSONValue } from "./util/isJSONValue.ts"
@@ -27,7 +27,7 @@ export function Tool<K extends JSONKey>(config: ToolConfig<K>): Tool<K> {
27
27
  }
28
28
  }
29
29
 
30
- export type ToolImplementation = (params: any) => Actor<Action, ToolResult> | PromiseOr<ToolResult>
30
+ export type ToolImplementation = (params: any) => Agent<Action, ToolResult> | PromiseOr<ToolResult>
31
31
 
32
32
  export type ToolExecutor = (args: any) => Promise<JSONValue>
33
33
 
@@ -48,7 +48,7 @@ function executor(this: Tool, scope: Scope): ToolExecutor {
48
48
  return { value: initial }
49
49
  }
50
50
  const fork = scope.fork("tool", [this.toolKey])
51
- const { value } = await fork.reduce(initial as Actor)
51
+ const { value } = await fork.reduce(initial as Agent)
52
52
  fork.event({
53
53
  type: "returned",
54
54
  value,
@@ -1,6 +1,6 @@
1
- import { setMessages } from "../../actions/setMessages.ts"
2
1
  import type { MessagesSet } from "../../events/MessagesSet.ts"
2
+ import { messages } from "../messages.ts"
3
3
 
4
4
  export function clear() {
5
- return setMessages(() => [])
5
+ return messages(() => [])
6
6
  }
@@ -1,6 +1,6 @@
1
1
  import { describe, expect, it } from "bun:test"
2
2
  import type { LEvent } from "../../events/LEvent.ts"
3
- import { Exec } from "../../Exec.ts"
3
+ import { exec } from "../../Exec.ts"
4
4
  import * as L from "../../L.ts"
5
5
  import { TestEmbeddingModel } from "../../testing/TestEmbeddingModel.ts"
6
6
  import { TestLanguageModel } from "../../testing/TestLanguageModel.ts"
@@ -8,15 +8,15 @@ import { TestLanguageModel } from "../../testing/TestLanguageModel.ts"
8
8
  describe("Model", () => {
9
9
  it("generates the expected event sequence", async () => {
10
10
  const events: Array<LEvent> = []
11
- await Exec(function*() {
12
- const a = yield* L.declareModel("secondary")
11
+ await exec(function*() {
12
+ const a = yield* L.declareModel("secondary", "language")
13
13
  yield* L.branch("fork-key", function*() {
14
- const b = yield* L.declareModel("child_a")
15
- const c = yield* L.declareModel("child_b")
14
+ const b = yield* L.declareModel("child_a", "language")
15
+ const c = yield* L.declareModel("child_b", "embedding")
16
16
  yield* b
17
17
  yield* c
18
18
  })
19
- const c = yield* L.declareModel("tertiary")
19
+ const c = yield* L.declareModel("tertiary", "embedding")
20
20
  yield* a
21
21
  yield* c
22
22
  }, {
@@ -27,7 +27,10 @@ describe("Model", () => {
27
27
  child_b: TestEmbeddingModel(),
28
28
  tertiary: TestEmbeddingModel(),
29
29
  },
30
- })((event) => events.push(event))
30
+ handler(event) {
31
+ events.push(event)
32
+ },
33
+ })
31
34
  expect(JSON.stringify(events, null, 2)).toMatchSnapshot()
32
35
  })
33
36
  })
@@ -1,9 +1,9 @@
1
- import type { Model } from "../../Model.ts"
1
+ import type { Models, ModelType } from "../../Model.ts"
2
2
  import type { JSONKey } from "../../util/JSONKey.ts"
3
3
  import { declareArg } from "../declareArg.ts"
4
4
  import { pushModel } from "../pushModel.ts"
5
5
 
6
- export function* declareModel<K extends JSONKey>(key: K) {
7
- const model = yield* declareArg(key)<Model>()
6
+ export function* declareModel<K extends JSONKey, M extends ModelType>(key: K, _modelType: M) {
7
+ const model = yield* declareArg(key)<Models[M]>()
8
8
  return yield* pushModel(key, model)
9
9
  }
@@ -1,4 +1,3 @@
1
- import type { MessageRemoved } from "../../events/MessageRemoved.ts"
2
1
  import type { AssistantContent, UserContent } from "../../Message.ts"
3
2
  import { normalizeTaggableArgs, type TaggableArgs } from "../../util/Taggable.ts"
4
3
  import { appendMessage } from "../appendMessage.ts"
@@ -0,0 +1,15 @@
1
+ import { scope } from "../scope.ts"
2
+
3
+ export const tools = {
4
+ *[Symbol.iterator]() {
5
+ const scope_ = yield* scope
6
+ return scope_.tools
7
+ },
8
+ }
9
+
10
+ export const signal = {
11
+ *[Symbol.iterator]() {
12
+ const scope_ = yield* scope
13
+ return scope_.controller.signal
14
+ },
15
+ }
@@ -1,13 +1,16 @@
1
1
  import { Action } from "../Action.ts"
2
2
  import type { MessageAppended } from "../events/MessageAppended.ts"
3
+ import type { MessageRemoved } from "../events/MessageRemoved.ts"
3
4
  import type { Message, MessageContents, MessageRole, Messages } from "../Message.ts"
4
5
  import type { Spec } from "../Spec.ts"
5
- import { removeMessage } from "./removeMessage.ts"
6
+ import { peekLast } from "../util/peekLast.ts"
6
7
 
7
- export interface appendMessage<M extends Message>
8
+ export interface appendMessage<M extends Message = Message>
8
9
  extends Action<"append_message", Spec.Make<{ Event: MessageAppended<M> }>>
9
10
  {}
10
11
 
12
+ export interface removeMessage extends Action<"remove_message", Spec.Make<{ Event: MessageRemoved }>> {}
13
+
11
14
  export function* appendMessage<R extends MessageRole>(
12
15
  role: R,
13
16
  content: MessageContents[R],
@@ -17,13 +20,34 @@ export function* appendMessage<R extends MessageRole>(
17
20
  role,
18
21
  content,
19
22
  } as Messages[R] // <-- why isn't this inferred?
23
+ const section = peekLast(scope.sections)
24
+ if (section) {
25
+ section.messages.add(message)
26
+ }
20
27
  scope.event({
21
28
  type: "message_appended",
22
29
  message,
23
30
  })
24
31
  return {
25
32
  ...scope,
26
- nextArg: removeMessage(message),
33
+ nextArg: (function*(): Generator<removeMessage> {
34
+ return yield Action("remove_message", (scope) => {
35
+ scope.event({
36
+ type: "message_removed",
37
+ message,
38
+ })
39
+ const messages = new Set(scope.messages)
40
+ messages.delete(message)
41
+ if (section) {
42
+ section.messages.delete(message)
43
+ }
44
+ return {
45
+ ...scope,
46
+ nextArg: message,
47
+ messages,
48
+ }
49
+ })
50
+ })(),
27
51
  messages: new Set([...scope.messages, message]),
28
52
  }
29
53
  })
package/actions/branch.ts CHANGED
@@ -1,14 +1,5 @@
1
1
  import { Action } from "../Action.ts"
2
- import type {
3
- Actor,
4
- ActorLike,
5
- ActorLikeArray,
6
- ActorLikeRecord,
7
- ActorLikesT,
8
- ActorLikeT,
9
- ActorLikeY,
10
- } from "../Actor.ts"
11
- import type { ActorLikes } from "../Actor.ts"
2
+ import type { Agent, AgentLike, AgentLikeT, AgentLikeY } from "../Agent.ts"
12
3
  import type { Spec } from "../Spec.ts"
13
4
  import type { JSONKey } from "../util/JSONKey.ts"
14
5
  import { unwrapDeferred } from "../util/unwrapDeferred.ts"
@@ -19,7 +10,7 @@ export function branch<
19
10
  T,
20
11
  >(
21
12
  key: K,
22
- actorLike: ActorLike<Y, T>,
13
+ agentLike: AgentLike<Y, T>,
23
14
  ): Generator<
24
15
  Action<
25
16
  "branch",
@@ -31,7 +22,7 @@ export function branch<
31
22
  >,
32
23
  T
33
24
  >
34
- export function branch<K extends JSONKey, const A extends ActorLikeArray>(name: K, actorLikeArray: A): Generator<
25
+ export function branch<K extends JSONKey, const A extends Array<AgentLike>>(name: K, agentLikes: A): Generator<
35
26
  Action<
36
27
  "branch",
37
28
  Spec.Make<{
@@ -39,18 +30,18 @@ export function branch<K extends JSONKey, const A extends ActorLikeArray>(name:
39
30
  K,
40
31
  {
41
32
  [L in keyof A]: Spec.Make<{
42
- Child: [L, ActorLikeY<A[L]>[""]]
43
- Entry: ActorLikeY<A[L]>[""]["Entry"]
44
- Value: ActorLikeT<A[L]>
33
+ Child: [L, AgentLikeY<A[L]>[""]]
34
+ Entry: AgentLikeY<A[L]>[""]["Entry"]
35
+ Value: AgentLikeT<A[L]>
45
36
  }>
46
37
  }[keyof A],
47
38
  ]
48
- Entry: ActorLikeY<A[number]>[""]["Entry"]
39
+ Entry: AgentLikeY<A[number]>[""]["Entry"]
49
40
  }>
50
41
  >,
51
- ActorLikesT<A>
42
+ AgentLikesT<A>
52
43
  >
53
- export function branch<K extends JSONKey, A extends ActorLikeRecord>(name: K, actorLikeRecord: A): Generator<
44
+ export function branch<K extends JSONKey, A extends Record<JSONKey, AgentLike>>(name: K, agentLikes: A): Generator<
54
45
  Action<
55
46
  "branch",
56
47
  Spec.Make<{
@@ -58,23 +49,23 @@ export function branch<K extends JSONKey, A extends ActorLikeRecord>(name: K, ac
58
49
  K,
59
50
  {
60
51
  [L in Exclude<keyof A, symbol>]: Spec.Make<{
61
- Child: [L, ActorLikeY<A[L]>[""]]
62
- Entry: ActorLikeY<A[L]>[""]["Entry"]
63
- Value: ActorLikeT<A[L]>
52
+ Child: [L, AgentLikeY<A[L]>[""]]
53
+ Entry: AgentLikeY<A[L]>[""]["Entry"]
54
+ Value: AgentLikeT<A[L]>
64
55
  }>
65
56
  }[Exclude<keyof A, symbol>],
66
57
  ]
67
- Entry: ActorLikeY<A[keyof A]>[""]["Entry"]
58
+ Entry: AgentLikeY<A[keyof A]>[""]["Entry"]
68
59
  }>
69
60
  >,
70
- ActorLikesT<A>
61
+ AgentLikesT<A>
71
62
  >
72
- export function* branch(key: JSONKey, implementation: ActorLike | ActorLikes): Generator<Action<"branch">, any> {
63
+ export function* branch(key: JSONKey, a1: AgentLike | AgentLikes): Generator<Action<"branch">, any> {
73
64
  return yield Action("branch", async (scope) => {
74
- if (typeof implementation === "function") {
65
+ if (typeof a1 === "function") {
75
66
  const branchScope = scope.fork("branch", [key])
76
- const actor = unwrapDeferred(implementation as ActorLike)
77
- const { value } = await branchScope.reduce(actor)
67
+ const agent = unwrapDeferred(a1 as AgentLike)
68
+ const { value } = await branchScope.reduce(agent)
78
69
  branchScope.event({
79
70
  type: "returned",
80
71
  value,
@@ -84,20 +75,20 @@ export function* branch(key: JSONKey, implementation: ActorLike | ActorLikes): G
84
75
  nextArg: value,
85
76
  }
86
77
  }
87
- const armKeys = Array.isArray(implementation)
88
- ? Array.from({ length: implementation.length }, (_0, i) => i)
89
- : Reflect.ownKeys(implementation) as Array<string>
78
+ const armKeys = Array.isArray(a1)
79
+ ? Array.from({ length: a1.length }, (_0, i) => i)
80
+ : Reflect.ownKeys(a1) as Array<string>
90
81
  const values = await Promise.all(armKeys.map(async (armKey) => {
91
82
  const branchArmScope = scope.fork("branch_arm", [key, armKey])
92
- const actor = unwrapDeferred(implementation[armKey as never]) as Actor
93
- const { value } = await branchArmScope.reduce(actor)
83
+ const agent = unwrapDeferred(a1[armKey as never]) as Agent
84
+ const { value } = await branchArmScope.reduce(agent)
94
85
  branchArmScope.event({
95
86
  type: "returned",
96
87
  value,
97
88
  })
98
89
  return value
99
90
  }))
100
- const value = Array.isArray(implementation)
91
+ const value = Array.isArray(a1)
101
92
  ? values
102
93
  : Object.fromEntries(armKeys.map((key, i) => [key, values[i]]))
103
94
  return {
@@ -106,3 +97,10 @@ export function* branch(key: JSONKey, implementation: ActorLike | ActorLikes): G
106
97
  }
107
98
  })
108
99
  }
100
+
101
+ export type AgentLikes = Array<AgentLike> | Record<JSONKey, AgentLike>
102
+ export type AgentLikesT<A extends AgentLikes> =
103
+ & {
104
+ -readonly [K in keyof A]: A[K] extends AgentLike<Action, infer T> ? T : never
105
+ }
106
+ & {}
package/actions/catch.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Action } from "../Action.ts"
2
- import type { ActorLike } from "../Actor.ts"
2
+ import type { AgentLike } from "../Agent.ts"
3
3
  import type { Spec } from "../Spec.ts"
4
4
  import type { JSONKey } from "../util/JSONKey.ts"
5
5
  import type { Result } from "../util/Result.ts"
@@ -8,7 +8,7 @@ import { unwrapDeferred } from "../util/unwrapDeferred.ts"
8
8
  export { catch_ as catch }
9
9
  function* catch_<K extends JSONKey, Y extends Action, T>(
10
10
  key: K,
11
- actorLike: ActorLike<Y, T>,
11
+ agentLike: AgentLike<Y, T>,
12
12
  ): Generator<
13
13
  Action<
14
14
  "catch",
@@ -22,7 +22,7 @@ function* catch_<K extends JSONKey, Y extends Action, T>(
22
22
  return yield Action("catch", async (scope) => {
23
23
  const catchScope = scope.fork("catch", [key])
24
24
  try {
25
- const { value } = await catchScope.reduce(unwrapDeferred(actorLike))
25
+ const { value } = await catchScope.reduce(unwrapDeferred(agentLike))
26
26
  catchScope.event({
27
27
  type: "returned",
28
28
  value,
package/actions/embed.ts CHANGED
@@ -3,12 +3,13 @@ import type { Embedded } from "../events/Embedded.ts"
3
3
  import type { EmbeddingRequested } from "../events/EmbeddingRequested.ts"
4
4
  import type { Spec } from "../Spec.ts"
5
5
  import { assert } from "../util/assert.ts"
6
+ import { peekLast } from "../util/peekLast.ts"
6
7
 
7
8
  export interface embed extends Action<"embed", Spec.Make<{ Event: EmbeddingRequested | Embedded }>> {}
8
9
 
9
10
  export function* embed(value: string): Generator<embed, Array<number>> {
10
11
  return yield Action("embed", async (scope) => {
11
- const model = scope.embeddingModels.values().next().value
12
+ const model = peekLast(scope.embeddingModels)
12
13
  assert(model)
13
14
  scope.event({
14
15
  type: "embedding_requested",
@@ -1,6 +1,6 @@
1
1
  import type { StandardSchemaV1 } from "@standard-schema/spec"
2
2
  import { Action } from "../Action.ts"
3
- import type { Actor } from "../Actor.ts"
3
+ import type { Agent } from "../Agent.ts"
4
4
  import type { ToolCalled } from "../events/ToolCalled.ts"
5
5
  import type { ToolEnabled } from "../events/ToolEnabled.ts"
6
6
  import type { Spec } from "../Spec.ts"
@@ -34,7 +34,7 @@ export function enableTool<
34
34
  key: K,
35
35
  description: string,
36
36
  params: StandardSchemaV1<JSONObject, A>,
37
- implementation: (params: A) => Actor<Y, T>,
37
+ implementation: (params: A) => Agent<Y, T>,
38
38
  ): Generator<
39
39
  Action<
40
40
  "enable_tool",