@dxos/assistant 0.8.4-main.84f28bd → 0.8.4-main.9735255

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 (198) hide show
  1. package/dist/lib/browser/chunk-7CDDG6LL.mjs +578 -0
  2. package/dist/lib/browser/chunk-7CDDG6LL.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  4. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  5. package/dist/lib/browser/extraction/index.mjs +225 -0
  6. package/dist/lib/browser/extraction/index.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +723 -1892
  8. package/dist/lib/browser/index.mjs.map +4 -4
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/testing/index.mjs +32 -0
  11. package/dist/lib/browser/testing/index.mjs.map +7 -0
  12. package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs +579 -0
  13. package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs.map +7 -0
  14. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  15. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
  16. package/dist/lib/node-esm/extraction/index.mjs +226 -0
  17. package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
  18. package/dist/lib/node-esm/index.mjs +723 -1893
  19. package/dist/lib/node-esm/index.mjs.map +4 -4
  20. package/dist/lib/node-esm/meta.json +1 -1
  21. package/dist/lib/node-esm/testing/index.mjs +33 -0
  22. package/dist/lib/node-esm/testing/index.mjs.map +7 -0
  23. package/dist/types/src/conversation/context.d.ts +129 -0
  24. package/dist/types/src/conversation/context.d.ts.map +1 -0
  25. package/dist/types/src/conversation/context.test.d.ts +2 -0
  26. package/dist/types/src/conversation/context.test.d.ts.map +1 -0
  27. package/dist/types/src/conversation/conversation.d.ts +29 -32
  28. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  29. package/dist/types/src/conversation/conversation.test.d.ts +2 -0
  30. package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
  31. package/dist/types/src/conversation/index.d.ts +1 -0
  32. package/dist/types/src/conversation/index.d.ts.map +1 -1
  33. package/dist/types/src/diff.d.ts +15 -0
  34. package/dist/types/src/diff.d.ts.map +1 -0
  35. package/dist/types/src/diff.test.d.ts +2 -0
  36. package/dist/types/src/diff.test.d.ts.map +1 -0
  37. package/dist/types/src/errors.d.ts +67 -0
  38. package/dist/types/src/errors.d.ts.map +1 -0
  39. package/dist/types/src/execution-graph.d.ts +85 -0
  40. package/dist/types/src/execution-graph.d.ts.map +1 -0
  41. package/dist/types/src/execution-graph.test.d.ts +2 -0
  42. package/dist/types/src/execution-graph.test.d.ts.map +1 -0
  43. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +1 -1
  44. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  45. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +279 -140
  46. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  47. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +1 -1
  48. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  49. package/dist/types/src/extraction/extraction.d.ts +345 -212
  50. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  51. package/dist/types/src/extraction/quotes.d.ts +3 -3
  52. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  53. package/dist/types/src/functions/index.d.ts +2 -0
  54. package/dist/types/src/functions/index.d.ts.map +1 -0
  55. package/dist/types/src/functions/services.d.ts +20 -0
  56. package/dist/types/src/functions/services.d.ts.map +1 -0
  57. package/dist/types/src/functions.test.d.ts +2 -0
  58. package/dist/types/src/functions.test.d.ts.map +1 -0
  59. package/dist/types/src/index.d.ts +5 -5
  60. package/dist/types/src/index.d.ts.map +1 -1
  61. package/dist/types/src/session/GenericToolkit.d.ts +85 -0
  62. package/dist/types/src/session/GenericToolkit.d.ts.map +1 -0
  63. package/dist/types/src/session/artifact-diff.d.ts +27 -0
  64. package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
  65. package/dist/types/src/session/error-handling.d.ts +4 -0
  66. package/dist/types/src/session/error-handling.d.ts.map +1 -0
  67. package/dist/types/src/session/format.d.ts +12 -0
  68. package/dist/types/src/session/format.d.ts.map +1 -0
  69. package/dist/types/src/session/format.test.d.ts +2 -0
  70. package/dist/types/src/session/format.test.d.ts.map +1 -0
  71. package/dist/types/src/session/index.d.ts +4 -0
  72. package/dist/types/src/session/index.d.ts.map +1 -1
  73. package/dist/types/src/session/observer.d.ts +23 -0
  74. package/dist/types/src/session/observer.d.ts.map +1 -0
  75. package/dist/types/src/session/session.d.ts +41 -268
  76. package/dist/types/src/session/session.d.ts.map +1 -1
  77. package/dist/types/src/session/toolkit.d.ts +14 -0
  78. package/dist/types/src/session/toolkit.d.ts.map +1 -0
  79. package/dist/types/src/session/version-pin.d.ts +5 -5
  80. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  81. package/dist/types/src/templates/index.d.ts +2 -0
  82. package/dist/types/src/templates/index.d.ts.map +1 -0
  83. package/dist/types/src/templates/system.d.ts +18 -0
  84. package/dist/types/src/templates/system.d.ts.map +1 -0
  85. package/dist/types/src/testing/index.d.ts +1 -2
  86. package/dist/types/src/testing/index.d.ts.map +1 -1
  87. package/dist/types/src/testing/layer.d.ts +21 -0
  88. package/dist/types/src/testing/layer.d.ts.map +1 -0
  89. package/dist/types/src/types/artifact.d.ts +28 -0
  90. package/dist/types/src/types/artifact.d.ts.map +1 -0
  91. package/dist/types/src/types/index.d.ts +2 -0
  92. package/dist/types/src/types/index.d.ts.map +1 -0
  93. package/dist/types/tsconfig.tsbuildinfo +1 -1
  94. package/package.json +58 -27
  95. package/src/conversation/context.test.ts +46 -0
  96. package/src/conversation/context.ts +303 -0
  97. package/src/conversation/conversation.test.ts +65 -0
  98. package/src/conversation/conversation.ts +98 -76
  99. package/src/conversation/index.ts +1 -0
  100. package/src/diff.test.ts +43 -0
  101. package/src/diff.ts +84 -0
  102. package/src/errors.ts +15 -0
  103. package/src/execution-graph.test.ts +740 -0
  104. package/src/execution-graph.ts +509 -0
  105. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +14 -25
  106. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +48 -52
  107. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +22 -21
  108. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +43 -41
  109. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
  110. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +3 -1
  111. package/src/extraction/extraction.ts +10 -9
  112. package/src/extraction/quotes.test.ts +3 -3
  113. package/src/extraction/quotes.ts +6 -7
  114. package/src/functions/index.ts +5 -0
  115. package/src/functions/services.ts +180 -0
  116. package/src/functions.conversations.json +1 -0
  117. package/src/functions.test.ts +74 -0
  118. package/src/index.ts +5 -5
  119. package/src/session/GenericToolkit.ts +131 -0
  120. package/src/session/artifact-diff.ts +41 -0
  121. package/src/session/error-handling.ts +41 -0
  122. package/src/session/format.test.ts +124 -0
  123. package/src/session/format.ts +143 -0
  124. package/src/session/index.ts +5 -0
  125. package/src/session/observer.ts +61 -0
  126. package/src/session/session.conversations.json +1 -0
  127. package/src/session/session.test.ts +83 -215
  128. package/src/session/session.ts +181 -529
  129. package/src/session/toolkit.ts +39 -0
  130. package/src/session/version-pin.ts +12 -12
  131. package/src/templates/index.ts +5 -0
  132. package/src/templates/instructions/format.tpl +61 -0
  133. package/src/templates/instructions/system.tpl +48 -0
  134. package/src/templates/system.ts +41 -0
  135. package/src/testing/index.ts +2 -8
  136. package/src/testing/layer.ts +78 -0
  137. package/src/types/artifact.ts +82 -0
  138. package/src/types/index.ts +5 -0
  139. package/src/typings.d.ts +5 -0
  140. package/dist/types/bin/chat.d.ts +0 -2
  141. package/dist/types/bin/chat.d.ts.map +0 -1
  142. package/dist/types/src/blueprint/blueprint.d.ts +0 -41
  143. package/dist/types/src/blueprint/blueprint.d.ts.map +0 -1
  144. package/dist/types/src/blueprint/index.d.ts +0 -3
  145. package/dist/types/src/blueprint/index.d.ts.map +0 -1
  146. package/dist/types/src/blueprint/registry.d.ts +0 -7
  147. package/dist/types/src/blueprint/registry.d.ts.map +0 -1
  148. package/dist/types/src/context/binder.d.ts +0 -34
  149. package/dist/types/src/context/binder.d.ts.map +0 -1
  150. package/dist/types/src/context/binding.d.ts +0 -38
  151. package/dist/types/src/context/binding.d.ts.map +0 -1
  152. package/dist/types/src/context/index.d.ts +0 -3
  153. package/dist/types/src/context/index.d.ts.map +0 -1
  154. package/dist/types/src/research/exa.d.ts +0 -7
  155. package/dist/types/src/research/exa.d.ts.map +0 -1
  156. package/dist/types/src/research/graph.d.ts +0 -41
  157. package/dist/types/src/research/graph.d.ts.map +0 -1
  158. package/dist/types/src/research/graph.test.d.ts +0 -2
  159. package/dist/types/src/research/graph.test.d.ts.map +0 -1
  160. package/dist/types/src/research/index.d.ts +0 -4
  161. package/dist/types/src/research/index.d.ts.map +0 -1
  162. package/dist/types/src/research/research.d.ts +0 -10
  163. package/dist/types/src/research/research.d.ts.map +0 -1
  164. package/dist/types/src/research/research.test.d.ts +0 -2
  165. package/dist/types/src/research/research.test.d.ts.map +0 -1
  166. package/dist/types/src/session/version-pin.test.d.ts +0 -2
  167. package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
  168. package/dist/types/src/summarization/summarization.test.d.ts +0 -2
  169. package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
  170. package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
  171. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
  172. package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
  173. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
  174. package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
  175. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
  176. package/dist/types/src/types.d.ts +0 -3
  177. package/dist/types/src/types.d.ts.map +0 -1
  178. package/src/blueprint/blueprint.ts +0 -75
  179. package/src/blueprint/index.ts +0 -6
  180. package/src/blueprint/registry.ts +0 -15
  181. package/src/context/binder.ts +0 -101
  182. package/src/context/binding.ts +0 -31
  183. package/src/context/index.ts +0 -6
  184. package/src/research/exa.ts +0 -100
  185. package/src/research/graph.test.ts +0 -15
  186. package/src/research/graph.ts +0 -337
  187. package/src/research/index.ts +0 -7
  188. package/src/research/instructions-research.tpl +0 -97
  189. package/src/research/research.test.ts +0 -190
  190. package/src/research/research.ts +0 -90
  191. package/src/session/version-pin.test.ts +0 -26
  192. package/src/summarization/summarization.test.ts +0 -221
  193. package/src/testing/exa-search-1748337321991.ts +0 -131
  194. package/src/testing/exa-search-1748337331526.ts +0 -144
  195. package/src/testing/exa-search-1748337344119.ts +0 -133
  196. package/src/typedefs.d.ts +0 -8
  197. package/src/types.ts +0 -8
  198. /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
@@ -2,35 +2,58 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Array, Option, pipe, Schema } from 'effect';
5
+ import type * as AiError from '@effect/ai/AiError';
6
+ import * as LanguageModel from '@effect/ai/LanguageModel';
7
+ import type * as Tool from '@effect/ai/Tool';
8
+ import type * as Toolkit from '@effect/ai/Toolkit';
9
+ import * as Chunk from 'effect/Chunk';
10
+ import * as Effect from 'effect/Effect';
11
+ import type * as Schema from 'effect/Schema';
12
+ import * as Stream from 'effect/Stream';
6
13
 
7
14
  import {
8
- AgentStatusReport,
9
- isToolUse,
10
- runTools,
11
- type AgentStatus,
12
- structuredOutputParser,
13
- type AiServiceClient,
14
- type GenerationStream,
15
- type GenerateRequest,
16
- Message,
17
- type MessageContentBlock,
18
- MixedStreamParser,
19
- ToolResult,
20
- createTool,
21
- type ExecutableTool,
22
- type ToolId,
23
- type ToolResolver,
15
+ AiParser,
16
+ AiPreprocessor,
17
+ type AiToolNotFoundError,
18
+ type PromptPreprocessingError,
19
+ type ToolExecutionService,
20
+ type ToolResolverService,
21
+ callTool,
22
+ getToolCalls,
24
23
  } from '@dxos/ai';
25
- import { type ArtifactDefinition } from '@dxos/artifact';
26
- import { Event, synchronized } from '@dxos/async';
24
+ import { type Blueprint } from '@dxos/blueprints';
25
+ import { todo } from '@dxos/debug';
27
26
  import { Obj } from '@dxos/echo';
28
- import { ObjectVersion } from '@dxos/echo-db';
29
- import { create, type ObjectId } from '@dxos/echo-schema';
30
- import { invariant } from '@dxos/invariant';
27
+ import { type FunctionInvocationService, TracingService } from '@dxos/functions';
31
28
  import { log } from '@dxos/log';
29
+ import { Message } from '@dxos/types';
32
30
 
33
- import { VersionPin } from './version-pin';
31
+ import { type AiAssistantError } from '../errors';
32
+
33
+ import { formatSystemPrompt, formatUserPrompt } from './format';
34
+ import { GenerationObserver } from './observer';
35
+
36
+ export type AiSessionRunError = AiError.AiError | PromptPreprocessingError | AiToolNotFoundError | AiAssistantError;
37
+
38
+ export type AiSessionRunRequirements =
39
+ | LanguageModel.LanguageModel
40
+ | ToolExecutionService
41
+ | ToolResolverService
42
+ | TracingService
43
+ | FunctionInvocationService;
44
+
45
+ export type AiSessionOptions = {};
46
+
47
+ export type AiSessionRunProps<Tools extends Record<string, Tool.Any>> = {
48
+ prompt: string;
49
+ // TODO(wittjosiah): Rename to systemPrompt.
50
+ system?: string;
51
+ history?: Message.Message[];
52
+ objects?: Obj.Unknown[];
53
+ blueprints?: readonly Blueprint.Blueprint[];
54
+ toolkit?: Toolkit.WithHandler<Tools>;
55
+ observer?: GenerationObserver<Tools>;
56
+ };
34
57
 
35
58
  /**
36
59
  * Contains message history, tools, current context.
@@ -38,545 +61,174 @@ import { VersionPin } from './version-pin';
38
61
  * It makes requests to the model, its a state machine.
39
62
  * It keeps track of the current goal.
40
63
  * It manages the context window.
41
- * Tracks the success criteria of reaching the goal, exposing metrics (stretch)
64
+ * Tracks the success criteria of reaching the goal, exposing metrics (stretch).
42
65
  * Could be run locally in the app or remotely.
43
66
  * Could be personal or shared.
44
67
  */
68
+ export class AiSession {
69
+ /** Prevents concurrent execution of session. */
70
+ private readonly _semaphore = Effect.runSync(Effect.makeSemaphore(1));
45
71
 
46
- /**
47
- * Resolves artifact ids to their versions.
48
- * Used to give the model a sense of the changes to the artifacts made by users during the conversation.
49
- * The artifacts versions are pinned in the history, and whenever the artifact changes in-between assistant's steps,
50
- * a diff is inserted into the conversation.
51
- */
52
- export type ArtifactDiffResolver = (artifacts: { id: ObjectId; lastVersion: ObjectVersion }[]) => Promise<
53
- Map<
54
- ObjectId,
55
- {
56
- version: ObjectVersion;
57
- diff?: string;
58
- }
59
- >
60
- >;
61
-
62
- export type SessionRunOptions = {
63
- artifacts: ArtifactDefinition[];
64
-
65
- /**
66
- * Non-artifact specific tools.
67
- */
68
- tools: ToolId[];
69
-
70
- history: Message[];
71
-
72
- prompt: string;
73
-
74
- generationOptions?: Pick<GenerateRequest, 'model'>;
75
-
76
- extensions?: ToolContextExtensions;
77
-
78
- systemPrompt?: string;
79
-
80
- /**
81
- * Pre-require artifacts.
82
- */
83
- requiredArtifactIds?: string[];
84
-
85
- client: AiServiceClient;
86
- toolResolver: ToolResolver;
87
-
88
- /**
89
- * Executable tools that do not go through the tool resolver.
90
- */
91
- executableTools?: ExecutableTool[];
92
-
93
- /**
94
- * @see ArtifactDiffResolver
95
- */
96
- artifactDiffResolver?: ArtifactDiffResolver;
97
- };
98
-
99
- type OperationModel = 'planning' | 'importing' | 'configured';
100
-
101
- export type AiSessionOptions = {
102
- /**
103
- * Determines the agent's handling of the artifacts definitions:
104
- *
105
- * - `planning`: The model will create a plan with required artifacts for each step.
106
- * - `importing`: The model can query registry and only pull in the artifacts that are required to complete the task.
107
- * - `configured`: The available artifacts are pre-selected and the model cannot select additional artifacts.
108
- */
109
- operationModel: OperationModel;
110
- };
111
-
112
- export class AISession {
113
- /** SSE stream parser. */
114
- private readonly _parser = new MixedStreamParser();
115
-
116
- /** Pending messages (incl. the current user request). */
117
- private _pending: Message[] = [];
72
+ /** Message history from queue. */
73
+ // TODO(burdon): Evolve into supporting a git-like graph of messages.
74
+ private _history: Message.Message[] = [];
118
75
 
119
- /** Current streaming response. */
120
- private _stream: GenerationStream | undefined;
76
+ /** Pending messages for this session (incl. the current prompt). */
77
+ private _pending: Message.Message[] = [];
121
78
 
122
- /** Prior history from queue. */
123
- private _history: Message[] = [];
79
+ private _started = 0;
80
+ private _ended = 0;
81
+ private _toolCalls = 0;
124
82
 
125
- /**
126
- * New message.
127
- */
128
- public readonly message = this._parser.message;
83
+ constructor(private readonly _options: AiSessionOptions = {}) {}
129
84
 
130
- /**
131
- * Complete block added to Message.
132
- */
133
- public readonly block = this._parser.block;
85
+ get duration(): number {
86
+ return this._ended - this._started;
87
+ }
134
88
 
135
- /**
136
- * Update partial block (while streaming).
137
- */
138
- public readonly update = this._parser.update;
89
+ get toolCalls(): number {
90
+ return this._toolCalls;
91
+ }
139
92
 
140
- /**
141
- * Unparsed events from the underlying generation stream.
142
- */
143
- public readonly streamEvent = this._parser.streamEvent;
93
+ run = <Tools extends Record<string, Tool.Any>>({
94
+ prompt,
95
+ system: systemTemplate,
96
+ history = [],
97
+ objects = [],
98
+ blueprints = [],
99
+ toolkit,
100
+ observer = GenerationObserver.noop(),
101
+ }: AiSessionRunProps<Tools>): Effect.Effect<Message.Message[], AiSessionRunError, AiSessionRunRequirements> =>
102
+ Effect.gen(this, function* () {
103
+ this._started = Date.now();
104
+ this._history = [...history];
105
+ this._pending = [];
106
+ const pending = this._pending;
107
+
108
+ const submitMessage = Effect.fnUntraced(function* (message: Message.Message) {
109
+ pending.push(message);
110
+ yield* observer.onMessage(message);
111
+ yield* TracingService.emitConverationMessage(message);
112
+ return message;
113
+ });
144
114
 
145
- /**
146
- * User prompt or tool result.
147
- */
148
- public readonly userMessage = new Event<Message>();
115
+ // Submit the prompt.
116
+ // TODO(burdon): Remove if cancelled?
117
+ const promptMessage = yield* submitMessage(yield* formatUserPrompt({ prompt, history }));
149
118
 
150
- /**
151
- * Agent self-reporting its status.
152
- * Triggered by the model.
153
- */
154
- public readonly statusReport = new Event<AgentStatus>();
119
+ // Generate system and prompt messages.
120
+ const system = yield* formatSystemPrompt({ system: systemTemplate, blueprints, objects }).pipe(Effect.orDie);
155
121
 
156
- /**
157
- * Emits when the session is done.
158
- */
159
- public readonly done = new Event<void>();
122
+ // log('system', { prompt: system });
160
123
 
161
- /**
162
- * Emits when the tool reports its status.
163
- * Triggered by the tool execution function.
164
- */
165
- public readonly toolStatusReport = new Event<{ message: Message; status: AgentStatus }>();
166
-
167
- constructor(private readonly _options: AiSessionOptions) {
168
- // Message complete.
169
- this._parser.message.on((message) => {
170
- this._pending.push(message);
171
- });
172
- this._parser.block.on((block) => {
173
- if (block.type === 'text' && block.disposition === 'status') {
174
- this.statusReport.emit(create(AgentStatusReport, { message: block.text }));
175
- }
176
- });
177
- }
178
-
179
- /**
180
- * Runs the AI model loop interacting with tools and artifacts.
181
- * @param options - The session options.
182
- * @returns The messages generated by the session, including the user's prompt.
183
- */
184
- @synchronized
185
- async run(options: SessionRunOptions): Promise<Message[]> {
186
- const systemTools: ExecutableTool[] = [];
187
- switch (this._options.operationModel) {
188
- case 'planning':
189
- systemTools.push(this._createQueryArtifactsTool(options.artifacts), this._createPlanningTool(options));
190
- break;
191
- case 'importing':
192
- systemTools.push(
193
- this._createQueryArtifactsTool(options.artifacts),
194
- this._createRequireTool(options.artifacts, (artifactDefinitionIds) => {
195
- for (const artifactDefinitionId of artifactDefinitionIds) {
196
- requiredArtifactIds.add(artifactDefinitionId);
197
- }
198
- }),
199
- );
200
- break;
201
- case 'configured':
202
- // no system tools
203
- break;
204
- default:
205
- throw new TypeError(`Invalid operation model: ${this._options.operationModel}`);
206
- }
207
-
208
- this._history = [...options.history];
209
- this._pending = [await this._formatUserPrompt(options.artifactDiffResolver, options.prompt, options.history)];
210
- this.userMessage.emit(this._pending.at(-1)!);
211
- this._stream = undefined;
212
-
213
- let error: Error | undefined;
214
-
215
- const requiredArtifactIds = new Set<string>(options.requiredArtifactIds ?? []);
216
- try {
217
- let more = false;
124
+ // Tool call loop.
218
125
  do {
219
- const tools: ExecutableTool[] = pipe(
220
- [
221
- ...systemTools,
222
- ...(await Promise.all(options.tools.map((toolId) => options.toolResolver.resolve(toolId)))),
223
- ...(options.executableTools ?? []),
224
- ...options.artifacts
225
- .filter((artifact) => requiredArtifactIds.has(artifact.id))
226
- .flatMap((artifact) => artifact.tools),
227
- ],
228
- Array.dedupeWith((a, b) => a.id === b.id),
229
- );
230
-
231
126
  log('request', {
127
+ prompt: promptMessage,
128
+ system: { snippet: createSnippet(system), length: system.length },
232
129
  pending: this._pending.length,
233
130
  history: this._history.length,
234
- tools: tools.map((tool) => tool.name),
235
- });
236
-
237
- // Open request stream.
238
- this._stream = await options.client.execStream({
239
- ...(options.generationOptions ?? {}),
240
- // TODO(burdon): Rename messages or separate history/message.
241
- history: [...this._history, ...this._pending],
242
- tools,
243
- systemPrompt:
244
- options.systemPrompt ??
245
- createBaseInstructions({
246
- availableArtifacts: [...requiredArtifactIds],
247
- operationModel: this._options.operationModel,
248
- }),
131
+ objects: objects?.length ?? 0,
249
132
  });
250
133
 
251
- // Wait until complete.
252
- await this._parser.parse(this._stream);
253
- await this._stream.complete();
254
-
255
- // Add messages.
256
- log('response', { pending: this._pending });
257
-
258
- // Resolve tool use locally.
259
- more = false;
260
- const message = this._pending.at(-1);
261
- invariant(message);
262
- if (isToolUse(message)) {
263
- log('tool request...');
264
- const response = await runTools({
265
- message: this._pending.at(-1)!,
266
- tools,
267
- extensions: options.extensions ?? {},
268
- reportStatus: (status) => this._onToolStatusReport(message, status),
269
- });
270
-
271
- log('tool response', { response });
272
- switch (response.type) {
273
- case 'continue': {
274
- this._pending = [...this._pending, response.message];
275
- this.userMessage.emit(response.message);
276
- more = true;
277
- break;
278
- }
279
- }
280
- }
281
- } while (more);
282
- } catch (err) {
283
- log.catch(err);
284
- if (err instanceof Error && err.message.includes('Overloaded')) {
285
- error = new AIServiecOverloadedError('AI service overloaded', { cause: err } as any); // TODO(dmaretskyi): Schema errors cant have cause
286
- } else {
287
- error = new Error('AI service error', { cause: err });
288
- }
289
- } finally {
290
- this._stream = undefined;
291
- this.done.emit();
292
- }
293
-
294
- if (error) {
295
- throw error;
296
- }
297
-
298
- return this._pending;
299
- }
300
-
301
- async runStructured<S extends Schema.Schema.AnyNoContext>(
302
- schema: S,
303
- options: SessionRunOptions,
304
- ): Promise<Schema.Schema.Type<S>> {
305
- const parser = structuredOutputParser(schema);
306
- const result = await this.run({
307
- ...options,
308
- executableTools: [...(options.executableTools ?? []), parser.tool],
309
- });
310
- return parser.getResult(result);
311
- }
312
-
313
- private async _formatUserPrompt(
314
- artifactDiffResolver: ArtifactDiffResolver | undefined,
315
- prompt: string,
316
- history: Message[],
317
- ) {
318
- const prelude: MessageContentBlock[] = [];
319
-
320
- if (artifactDiffResolver) {
321
- const versions = gatherObjectVersions(history);
134
+ const prompt = yield* AiPreprocessor.preprocessPrompt([...this._history, ...this._pending], { system });
135
+
136
+ // Execute the stream request.
137
+ // logDump('prompt', Prompt.Prompt.pipe(Schema.encodeSync)(prompt));
138
+ const blocks = yield* LanguageModel.streamText({
139
+ prompt,
140
+ toolkit,
141
+ disableToolCallResolution: true,
142
+ }).pipe(
143
+ // TOOD(dmaretskyi): Error mapping.
144
+ AiParser.parseResponse({
145
+ onBegin: () => observer.onBegin(),
146
+ onBlock: (block) => observer.onBlock(block),
147
+ onPart: (part) => observer.onPart(part),
148
+ onEnd: (summary) => observer.onEnd(summary),
149
+ }),
150
+ Stream.runCollect,
151
+ Effect.map(Chunk.toArray),
152
+ );
322
153
 
323
- const artifactDiff = await artifactDiffResolver(
324
- [...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),
325
- );
154
+ // log('blocks', { blocks });
326
155
 
327
- log.info('vision', {
328
- artifactDiff,
329
- versions,
330
- });
156
+ // Create the response message.
157
+ const response = yield* submitMessage(
158
+ Obj.make(Message.Message, {
159
+ created: new Date().toISOString(),
160
+ sender: { role: 'assistant' },
161
+ blocks,
162
+ }),
163
+ );
331
164
 
332
- for (const [id, { version }] of [...artifactDiff.entries()]) {
333
- if (ObjectVersion.equals(version, versions.get(id)!)) {
334
- artifactDiff.delete(id);
335
- continue;
165
+ // Parse the response for tool calls.
166
+ const toolCalls = getToolCalls(response);
167
+ if (toolCalls.length === 0) {
168
+ break;
169
+ } else if (!toolkit) {
170
+ throw new Error('No toolkit provided'); // TODO(burdon): Throw user error?
336
171
  }
337
172
 
338
- prelude.push(VersionPin.createBlock(VersionPin.make({ objectId: id, version })));
339
- }
340
- if (artifactDiff.size > 0) {
341
- prelude.push(createArtifactUpdateBlock(artifactDiff));
342
- }
343
- }
344
-
345
- return Obj.make(Message, {
346
- role: 'user',
347
- content: [...prelude, { type: 'text', text: prompt }],
348
- });
349
- }
350
-
351
- private _createQueryArtifactsTool(artifacts: ArtifactDefinition[]): ExecutableTool {
352
- return createTool('system', {
353
- name: 'query_artifact_definitions',
354
- description: 'Query the available artifact definitions',
355
- schema: Schema.Struct({}),
356
- execute: async () => {
357
- return ToolResult.Success({
358
- artifactDefinitions: artifacts.map((artifact) => ({
359
- id: artifact.id,
360
- name: artifact.name,
361
- description: artifact.description,
362
- })),
363
- });
364
- },
365
- });
366
- }
367
-
368
- private _createRequireTool(
369
- artifacts: ArtifactDefinition[],
370
- onRequire: (artifactDefinitionIds: readonly string[]) => void,
371
- ): ExecutableTool {
372
- return createTool('system', {
373
- name: 'require_artifact_definitions',
374
- description:
375
- 'Require the use of specific artifact definitions. This will allow the model to interact with artifact definitions and use their tools.',
376
- schema: Schema.Struct({
377
- artifactDefinitionIds: Schema.Array(Schema.String).annotations({
378
- description: 'The ids of the artifact definitions to require',
379
- examples: [['artifact:dxos.org/example/Test']],
380
- }),
381
- }),
382
- execute: async ({ artifactDefinitionIds }) => {
383
- const missingArtifactDefinitionIds = artifactDefinitionIds.filter(
384
- (artifactId) => !artifacts.some((artifact) => artifact.id === artifactId),
385
- );
386
- if (missingArtifactDefinitionIds.length > 0) {
387
- return ToolResult.Error(
388
- `One or more artifact definition ids are invalid: ${missingArtifactDefinitionIds.join(', ')}`,
173
+ // Execute the tool calls.
174
+ // TODO(burdon): Retry errors? Write result when each completes individually?
175
+ const toolResults = yield* Effect.forEach(toolCalls, (toolCall) => {
176
+ return callTool(toolkit, toolCall).pipe(
177
+ Effect.provide(
178
+ TracingService.layerSubframe((context) => ({
179
+ ...context,
180
+ parentMessage: response.id,
181
+ toolCallId: toolCall.toolCallId,
182
+ })),
183
+ ),
389
184
  );
390
- }
391
-
392
- onRequire(artifactDefinitionIds);
393
-
394
- return ToolResult.Success({});
395
- },
396
- });
397
- }
185
+ });
398
186
 
399
- private _createPlanningTool(options: SessionRunOptions): ExecutableTool {
400
- return createTool('system', {
401
- name: 'create_plan',
402
- description:
403
- 'Create a plan. Make sure that each step is independent and can be executed solely on the data returned by the previous step. The steps only share the data that is specified in the plan.',
404
- schema: Schema.Struct({
405
- goal: Schema.String.annotations({ description: 'The goal that the plan will achieve.' }),
406
- steps: Schema.Array(
407
- Schema.Struct({
408
- action: Schema.String.annotations({
409
- description: 'Complete, detailed action to perform.',
410
- }),
411
- input: Schema.String.annotations({
412
- description: 'The required input data for this step. First step must not require any data.',
413
- }),
414
- output: Schema.String.annotations({
415
- description: 'The output data from this step. This is passed to the next step as input.',
416
- }),
417
- requiredArtifactIds: Schema.Array(Schema.String).annotations({
418
- description: 'The ids of the artifacts required to perform this step.',
419
- examples: [['artifact:dxos.org/example/Test']],
420
- }),
187
+ // Add to queue and continue loop.
188
+ // TODO(wittjosiah): Sometimes tool error results are added to the queue before the tool agent statuses.
189
+ // This results in a broken execution graph.
190
+ yield* submitMessage(
191
+ Obj.make(Message.Message, {
192
+ created: new Date().toISOString(),
193
+ sender: { role: 'tool' },
194
+ blocks: toolResults,
421
195
  }),
422
- ).annotations({ description: 'Steps' }),
423
- }),
424
- execute: async ({ goal, steps }) => {
425
- const missingArtifactIds = steps
426
- .flatMap((step) => step.requiredArtifactIds)
427
- .filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
428
- if (missingArtifactIds.length > 0) {
429
- return ToolResult.Error(`One or more artifact ids are invalid: ${missingArtifactIds.join(', ')}`);
430
- }
431
-
432
- const stepResults: any[] = [];
433
- for (const step of steps) {
434
- log('executing step', { action: step.action });
435
- const session = new AISession({ operationModel: 'importing' });
436
- session.message.on((ev) => this.message.emit(ev));
437
- session.block.on((ev) => this.block.emit(ev));
438
- session.update.on((ev) => this.update.emit(ev));
439
- session.streamEvent.on((ev) => this.streamEvent.emit(ev));
440
- session.userMessage.on((ev) => this.userMessage.emit(ev));
441
-
442
- const messages = await session.run({
443
- artifacts: options.artifacts,
444
- tools: options.tools,
445
- history: options.history,
446
- extensions: options.extensions,
447
- requiredArtifactIds: step.requiredArtifactIds.slice(),
448
- prompt: `
449
- You are an agent that executes the subtask in a plan.
450
-
451
- While you know the plan's goal only focus on your subtask
452
- Use the data from the previous step to complete your task.
453
- Return all of the data from "output" in your last message.
454
-
455
- Current step (that you need to complete):
456
- ${JSON.stringify(
457
- {
458
- goal,
459
- input: step.input,
460
- output: step.output,
461
- yourTask: step.action,
462
- },
463
- null,
464
- 2,
465
- )}
466
-
467
- Previous step results:
468
- ${JSON.stringify(stepResults.at(-1))}.
469
- `,
470
-
471
- generationOptions: options.generationOptions,
472
- client: options.client,
473
- toolResolver: options.toolResolver,
474
- artifactDiffResolver: options.artifactDiffResolver,
475
- });
476
- const result = messages.at(-1);
477
- stepResults.push(result);
478
- }
196
+ );
479
197
 
480
- return ToolResult.Success({ stepResults });
481
- },
482
- });
483
- }
198
+ this._toolCalls++;
199
+ } while (true);
484
200
 
485
- private _onToolStatusReport(message: Message, status: AgentStatus): void {
486
- this.toolStatusReport.emit({ message, status });
487
- }
201
+ this._ended = Date.now();
202
+ log('done', { pending: this._pending.length, duration: this.duration, tools: this._toolCalls });
203
+ return this._pending;
204
+ }).pipe(this._semaphore.withPermits(1), Effect.withSpan('AiSession.run'));
488
205
 
489
- abort(): void {
490
- this._stream?.abort();
206
+ /**
207
+ * @deprecated
208
+ */
209
+ // TODO(burdon): Implement or remove.
210
+ async runStructured<S extends Schema.Schema.AnyNoContext>(
211
+ _schema: S,
212
+ _options: AiSessionRunProps<any>,
213
+ ): Promise<Schema.Schema.Type<S>> {
214
+ return todo();
215
+ // const parser = structuredOutputParser(schema);
216
+ // const result = await this.run({
217
+ // ...options,
218
+ // executableTools: [...(options.executableTools ?? []), parser.tool],
219
+ // });
220
+ // return parser.getResult(result);
491
221
  }
492
222
  }
493
223
 
494
- // TODO(burdon): Use handlebars template with effect-schema input.
495
- const createBaseInstructions = ({
496
- availableArtifacts,
497
- operationModel,
498
- }: {
499
- availableArtifacts: string[];
500
- operationModel: OperationModel;
501
- }) => `
502
- You are a friendly, advanced AI assistant capable of creating and managing artifacts from available data and tools.
503
- Your task is to process user commands and questions and decide how best to respond.
504
- In some cases, you will need to create or reference data objects called artifacts.
505
-
506
- Follow these guidelines carefully:
507
-
508
- Decision-making:
509
-
510
- - Analyze the structure and type of the content in the user's message.
511
-
512
-
513
- ${
514
- operationModel === 'planning'
515
- ? `
516
- - Can you complete the task using the available artifacts?
517
- - If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
518
- - Identify which artifacts are relevant to the user's request.
519
- - Break down the user's request into a step-by-step plan that you will use to execute the user's request, the plan items should include artifacts which will be used to perform the actions.
520
- `
521
- : ''
522
- }
523
- ${
524
- operationModel === 'importing'
525
- ? `
526
- - Can you complete the task using the available artifacts?
527
- - If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
528
- - Identify which artifacts are relevant to the user's request.
529
- - Are the required artifacts already available?
530
- - If not, select which artifact(s) will be the most relevant and require them using the require_artifacts tool.
531
- - The require'd artifact tools will be available for use after require.
532
- `
533
- : ''
534
- }
535
- ${
536
- operationModel === 'configured'
537
- ? `
538
- - Select the most relevant artifact(s) to complete the task.
539
- - Call the appropriate tool to use the artifact(s).
540
- `
541
- : ''
542
- }
543
-
544
- ${availableArtifacts.length > 0 ? `Artifacts already in context: ${availableArtifacts.join('\n')}` : ''}
545
- `;
546
-
547
- export class AIServiecOverloadedError extends Schema.TaggedError<AIServiecOverloadedError>()(
548
- 'AIServiecOverloadedError',
549
- {},
550
- ) {}
551
-
552
- const gatherObjectVersions = (messages: Message[]): Map<ObjectId, ObjectVersion> => {
553
- const artifactIds = new Map<ObjectId, ObjectVersion>();
554
- for (const message of messages) {
555
- for (const block of message.content) {
556
- if (block.type === 'json' && block.disposition === VersionPin.DISPOSITION) {
557
- const pin = VersionPin.pipe(Schema.decodeOption)(JSON.parse(block.json)).pipe(Option.getOrUndefined);
558
- if (!pin) {
559
- continue;
560
- }
561
- artifactIds.set(pin.objectId, pin.version);
562
- }
563
- }
564
- }
565
-
566
- return artifactIds;
567
- };
224
+ const createSnippet = (text: string, len = 32) =>
225
+ text.length <= len * 2 ? text : [text.slice(0, len), '...', text.slice(-len)].join('');
568
226
 
569
- const createArtifactUpdateBlock = (
570
- artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
571
- ): MessageContentBlock => {
572
- return {
573
- type: 'text',
574
- disposition: 'artifact-update',
575
- text: `
576
- The following artifacts have been updated since the last message:
577
- ${[...artifactDiff.entries()]
578
- .map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
579
- .join('\n')}
580
- `,
581
- };
227
+ // TODO(dmaretskyi): Extract as a general util.
228
+ const logDump = (message: string, data: unknown) => {
229
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
230
+ const { writeFileSync } = require('node:fs');
231
+ const path = `/tmp/log-data-${Date.now()}.json`;
232
+ writeFileSync(path, JSON.stringify(data, null, 2));
233
+ log.info(message, { path });
582
234
  };