@dxos/assistant 0.8.4-main.ead640a → 0.8.4-main.ef1bc66f44

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 (120) hide show
  1. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  2. package/dist/lib/neutral/chunk-ZPC54VRM.mjs +596 -0
  3. package/dist/lib/neutral/chunk-ZPC54VRM.mjs.map +7 -0
  4. package/dist/lib/neutral/extraction/index.mjs +225 -0
  5. package/dist/lib/neutral/extraction/index.mjs.map +7 -0
  6. package/dist/lib/{browser → neutral}/index.mjs +346 -350
  7. package/dist/lib/neutral/index.mjs.map +7 -0
  8. package/dist/lib/neutral/meta.json +1 -0
  9. package/dist/lib/neutral/testing/index.mjs +38 -0
  10. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  11. package/dist/types/src/conversation/context.d.ts +97 -62
  12. package/dist/types/src/conversation/context.d.ts.map +1 -1
  13. package/dist/types/src/conversation/context.test.d.ts +2 -0
  14. package/dist/types/src/conversation/context.test.d.ts.map +1 -0
  15. package/dist/types/src/conversation/conversation.d.ts +41 -16
  16. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  17. package/dist/types/src/conversation/conversation.test.d.ts +2 -0
  18. package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
  19. package/dist/types/src/diff.d.ts +3 -1
  20. package/dist/types/src/diff.d.ts.map +1 -1
  21. package/dist/types/src/errors.d.ts +22 -30
  22. package/dist/types/src/errors.d.ts.map +1 -1
  23. package/dist/types/src/execution-graph.d.ts +5 -2
  24. package/dist/types/src/execution-graph.d.ts.map +1 -1
  25. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  26. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +254 -248
  27. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  28. package/dist/types/src/extraction/extraction.d.ts +344 -303
  29. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  30. package/dist/types/src/extraction/quotes.d.ts +2 -2
  31. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  32. package/dist/types/src/functions/services.d.ts +6 -6
  33. package/dist/types/src/functions/services.d.ts.map +1 -1
  34. package/dist/types/src/functions.test.d.ts +2 -0
  35. package/dist/types/src/functions.test.d.ts.map +1 -0
  36. package/dist/types/src/session/GenericToolkit.d.ts +107 -0
  37. package/dist/types/src/session/GenericToolkit.d.ts.map +1 -0
  38. package/dist/types/src/session/artifact-diff.d.ts +1 -1
  39. package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
  40. package/dist/types/src/session/format.d.ts +4 -176
  41. package/dist/types/src/session/format.d.ts.map +1 -1
  42. package/dist/types/src/session/index.d.ts +2 -0
  43. package/dist/types/src/session/index.d.ts.map +1 -1
  44. package/dist/types/src/session/observer.d.ts +6 -5
  45. package/dist/types/src/session/observer.d.ts.map +1 -1
  46. package/dist/types/src/session/session.d.ts +16 -11
  47. package/dist/types/src/session/session.d.ts.map +1 -1
  48. package/dist/types/src/session/toolkit.d.ts +5 -6
  49. package/dist/types/src/session/toolkit.d.ts.map +1 -1
  50. package/dist/types/src/session/version-pin.d.ts +3 -3
  51. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  52. package/dist/types/src/testing/index.d.ts +2 -0
  53. package/dist/types/src/testing/index.d.ts.map +1 -0
  54. package/dist/types/src/testing/layer.d.ts +22 -0
  55. package/dist/types/src/testing/layer.d.ts.map +1 -0
  56. package/dist/types/src/types/artifact.d.ts +14 -2
  57. package/dist/types/src/types/artifact.d.ts.map +1 -1
  58. package/dist/types/tsconfig.tsbuildinfo +1 -1
  59. package/package.json +40 -31
  60. package/src/conversation/context.test.ts +46 -0
  61. package/src/conversation/context.ts +211 -67
  62. package/src/conversation/conversation.test.ts +65 -0
  63. package/src/conversation/conversation.ts +128 -53
  64. package/src/diff.test.ts +37 -6
  65. package/src/diff.ts +18 -4
  66. package/src/errors.ts +2 -2
  67. package/src/execution-graph.test.ts +45 -43
  68. package/src/execution-graph.ts +23 -14
  69. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +2 -2
  70. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +44 -54
  71. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +15 -14
  72. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +36 -44
  73. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +10 -5
  74. package/src/extraction/extraction.ts +9 -8
  75. package/src/extraction/quotes.ts +4 -5
  76. package/src/functions/services.ts +66 -37
  77. package/src/functions.conversations.json +1 -0
  78. package/src/functions.test.ts +74 -0
  79. package/src/session/GenericToolkit.ts +160 -0
  80. package/src/session/artifact-diff.ts +1 -1
  81. package/src/session/format.test.ts +103 -39
  82. package/src/session/format.ts +17 -16
  83. package/src/session/index.ts +3 -0
  84. package/src/session/observer.ts +5 -3
  85. package/src/session/session.conversations.json +1 -1
  86. package/src/session/session.test.ts +28 -15
  87. package/src/session/session.ts +44 -21
  88. package/src/session/toolkit.ts +13 -22
  89. package/src/session/version-pin.ts +6 -6
  90. package/src/templates/{format.tpl → instructions/format.tpl} +1 -4
  91. package/src/templates/system.ts +2 -2
  92. package/src/testing/index.ts +5 -0
  93. package/src/testing/layer.ts +84 -0
  94. package/src/types/artifact.ts +43 -6
  95. package/dist/lib/browser/chunk-CWGVG235.mjs +0 -380
  96. package/dist/lib/browser/chunk-CWGVG235.mjs.map +0 -7
  97. package/dist/lib/browser/chunk-UB26N5UF.mjs +0 -89
  98. package/dist/lib/browser/chunk-UB26N5UF.mjs.map +0 -7
  99. package/dist/lib/browser/extraction/index.mjs +0 -272
  100. package/dist/lib/browser/extraction/index.mjs.map +0 -7
  101. package/dist/lib/browser/index.mjs.map +0 -7
  102. package/dist/lib/browser/meta.json +0 -1
  103. package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +0 -13
  104. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +0 -91
  105. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +0 -7
  106. package/dist/lib/node-esm/chunk-OV5PMQPB.mjs +0 -382
  107. package/dist/lib/node-esm/chunk-OV5PMQPB.mjs.map +0 -7
  108. package/dist/lib/node-esm/extraction/index.mjs +0 -273
  109. package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
  110. package/dist/lib/node-esm/index.mjs +0 -934
  111. package/dist/lib/node-esm/index.mjs.map +0 -7
  112. package/dist/lib/node-esm/meta.json +0 -1
  113. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
  114. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +0 -7
  115. package/dist/types/src/summarization/summarization.test.d.ts +0 -2
  116. package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
  117. package/src/summarization/summarization.test.ts +0 -202
  118. /package/dist/lib/{browser/named-entity-recognition-QPYHML66.mjs.map → neutral/chunk-J5LGTIGS.mjs.map} +0 -0
  119. /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
  120. /package/src/templates/{system.tpl → instructions/system.tpl} +0 -0
@@ -9,13 +9,12 @@ import * as Effect from 'effect/Effect';
9
9
  import * as Layer from 'effect/Layer';
10
10
  import * as Schema from 'effect/Schema';
11
11
 
12
- import { AiService, MemoizedAiService, ToolExecutionService, ToolResolverService } from '@dxos/ai';
13
- import { AiServiceTestingPreset } from '@dxos/ai/testing';
14
12
  import { Obj, Type } from '@dxos/echo';
15
- import { TestHelpers } from '@dxos/effect';
16
- import { TracingService } from '@dxos/functions';
13
+ import { TestHelpers } from '@dxos/effect/testing';
17
14
  import { log } from '@dxos/log';
18
15
 
16
+ import { AssistantTestLayer } from '../testing';
17
+
19
18
  import { AiSession } from './session';
20
19
 
21
20
  // Define a calendar event artifact schema.
@@ -25,7 +24,7 @@ const CalendarEventSchema = Schema.Struct({
25
24
  endTime: Schema.String,
26
25
  description: Schema.String,
27
26
  }).pipe(
28
- Type.Obj({
27
+ Type.object({
29
28
  typename: 'example.com/type/CalendarEvent',
30
29
  version: '0.1.0',
31
30
  }),
@@ -33,7 +32,7 @@ const CalendarEventSchema = Schema.Struct({
33
32
 
34
33
  type CalendarEvent = Schema.Schema.Type<typeof CalendarEventSchema>;
35
34
 
36
- class TestToolkit extends Toolkit.make(
35
+ const TestToolkit = Toolkit.make(
37
36
  Tool.make('Calculator', {
38
37
  description: 'Basic calculator tool',
39
38
  parameters: {
@@ -46,7 +45,7 @@ class TestToolkit extends Toolkit.make(
46
45
  }),
47
46
  failure: Schema.Never,
48
47
  }),
49
- ) {}
48
+ );
50
49
 
51
50
  // Tool handlers.
52
51
  const toolkitLayer = TestToolkit.toLayer({
@@ -66,15 +65,10 @@ const toolkitLayer = TestToolkit.toLayer({
66
65
  });
67
66
 
68
67
  const TestLayer = Layer.mergeAll(
68
+ AssistantTestLayer({
69
+ types: [CalendarEventSchema],
70
+ }),
69
71
  toolkitLayer,
70
- AiService.model('@anthropic/claude-3-5-sonnet-20241022'),
71
- TracingService.layerNoop,
72
- ToolResolverService.layerEmpty,
73
- ToolExecutionService.layerEmpty,
74
- ).pipe(
75
- //
76
- Layer.provideMerge(MemoizedAiService.layerTest()),
77
- Layer.provide(AiServiceTestingPreset('direct')),
78
72
  );
79
73
 
80
74
  describe('AiSession', () => {
@@ -111,6 +105,25 @@ describe('AiSession', () => {
111
105
  TestHelpers.provideTestContext,
112
106
  ),
113
107
  );
108
+
109
+ it.effect(
110
+ 'tool schema error',
111
+ Effect.fnUntraced(
112
+ function* (_) {
113
+ const session = new AiSession({ operationModel: 'configured' });
114
+ const toolkit = yield* TestToolkit;
115
+ const response = yield* session.run({
116
+ toolkit,
117
+ prompt:
118
+ 'I am testing error handling in tool paramter parsing. I want you to call the calculator tool but omit the input parameter to intentionally differ from the tool schema.',
119
+ history: [],
120
+ });
121
+ log.info('response', { response });
122
+ },
123
+ Effect.provide(TestLayer),
124
+ TestHelpers.provideTestContext,
125
+ ),
126
+ );
114
127
  });
115
128
 
116
129
  // Travel to rome, florence, livorno, siena, madrid for conferences
@@ -20,13 +20,14 @@ import {
20
20
  type ToolResolverService,
21
21
  callTool,
22
22
  getToolCalls,
23
+ withoutToolCallParising,
23
24
  } from '@dxos/ai';
24
25
  import { type Blueprint } from '@dxos/blueprints';
25
26
  import { todo } from '@dxos/debug';
26
27
  import { Obj } from '@dxos/echo';
27
- import { TracingService } from '@dxos/functions';
28
+ import { type FunctionInvocationService, TracingService } from '@dxos/functions';
28
29
  import { log } from '@dxos/log';
29
- import { DataType } from '@dxos/schema';
30
+ import { Message } from '@dxos/types';
30
31
 
31
32
  import { type AiAssistantError } from '../errors';
32
33
 
@@ -39,20 +40,22 @@ export type AiSessionRunRequirements =
39
40
  | LanguageModel.LanguageModel
40
41
  | ToolExecutionService
41
42
  | ToolResolverService
42
- | TracingService;
43
+ | TracingService
44
+ | FunctionInvocationService;
43
45
 
44
- export type AiSessionRunParams<Tools extends Record<string, Tool.Any>> = {
46
+ export type AiSessionOptions = {};
47
+
48
+ export type AiSessionRunProps<Tools extends Record<string, Tool.Any>> = {
45
49
  prompt: string;
50
+ // TODO(wittjosiah): Rename to systemPrompt.
46
51
  system?: string;
47
- history?: DataType.Message[];
48
- objects?: Obj.Any[];
49
- blueprints?: Blueprint.Blueprint[];
52
+ history?: Message.Message[];
53
+ objects?: Obj.Unknown[];
54
+ blueprints?: readonly Blueprint.Blueprint[];
50
55
  toolkit?: Toolkit.WithHandler<Tools>;
51
- observer?: GenerationObserver;
56
+ observer?: GenerationObserver<Tools>;
52
57
  };
53
58
 
54
- export type AiSessionOptions = {};
55
-
56
59
  /**
57
60
  * Contains message history, tools, current context.
58
61
  * Current context means the state of the app, time of day, and other contextual information.
@@ -69,13 +72,25 @@ export class AiSession {
69
72
 
70
73
  /** Message history from queue. */
71
74
  // TODO(burdon): Evolve into supporting a git-like graph of messages.
72
- private _history: DataType.Message[] = [];
75
+ private _history: Message.Message[] = [];
73
76
 
74
77
  /** Pending messages for this session (incl. the current prompt). */
75
- private _pending: DataType.Message[] = [];
78
+ private _pending: Message.Message[] = [];
79
+
80
+ private _started = 0;
81
+ private _ended = 0;
82
+ private _toolCalls = 0;
76
83
 
77
84
  constructor(private readonly _options: AiSessionOptions = {}) {}
78
85
 
86
+ get duration(): number {
87
+ return this._ended - this._started;
88
+ }
89
+
90
+ get toolCalls(): number {
91
+ return this._toolCalls;
92
+ }
93
+
79
94
  run = <Tools extends Record<string, Tool.Any>>({
80
95
  prompt,
81
96
  system: systemTemplate,
@@ -84,13 +99,14 @@ export class AiSession {
84
99
  blueprints = [],
85
100
  toolkit,
86
101
  observer = GenerationObserver.noop(),
87
- }: AiSessionRunParams<Tools>): Effect.Effect<DataType.Message[], AiSessionRunError, AiSessionRunRequirements> =>
102
+ }: AiSessionRunProps<Tools>): Effect.Effect<Message.Message[], AiSessionRunError, AiSessionRunRequirements> =>
88
103
  Effect.gen(this, function* () {
104
+ this._started = Date.now();
89
105
  this._history = [...history];
90
106
  this._pending = [];
91
107
  const pending = this._pending;
92
108
 
93
- const submitMessage = Effect.fnUntraced(function* (message: DataType.Message) {
109
+ const submitMessage = Effect.fnUntraced(function* (message: Message.Message) {
94
110
  pending.push(message);
95
111
  yield* observer.onMessage(message);
96
112
  yield* TracingService.emitConverationMessage(message);
@@ -102,7 +118,7 @@ export class AiSession {
102
118
  const promptMessage = yield* submitMessage(yield* formatUserPrompt({ prompt, history }));
103
119
 
104
120
  // Generate system and prompt messages.
105
- const system = yield* formatSystemPrompt({ system: systemTemplate, blueprints, objects });
121
+ const system = yield* formatSystemPrompt({ system: systemTemplate, blueprints, objects }).pipe(Effect.orDie);
106
122
 
107
123
  // Tool call loop.
108
124
  do {
@@ -114,15 +130,19 @@ export class AiSession {
114
130
  objects: objects?.length ?? 0,
115
131
  });
116
132
 
117
- const prompt = yield* AiPreprocessor.preprocessPrompt([...this._history, ...this._pending], { system });
133
+ const prompt = yield* AiPreprocessor.preprocessPrompt([...this._history, ...this._pending], {
134
+ system,
135
+ cacheControl: 'ephemeral',
136
+ });
118
137
 
119
138
  // Execute the stream request.
120
- // logDump('prompt', Prompt.Prompt.pipe(Schema.encodeSync)(prompt));
121
139
  const blocks = yield* LanguageModel.streamText({
122
140
  prompt,
123
141
  toolkit,
124
142
  disableToolCallResolution: true,
125
143
  }).pipe(
144
+ // Disable tool call parsing so that we can handle parameter schema errors on a per-tool call basis.
145
+ withoutToolCallParising,
126
146
  // TOOD(dmaretskyi): Error mapping.
127
147
  AiParser.parseResponse({
128
148
  onBegin: () => observer.onBegin(),
@@ -136,7 +156,7 @@ export class AiSession {
136
156
 
137
157
  // Create the response message.
138
158
  const response = yield* submitMessage(
139
- Obj.make(DataType.Message, {
159
+ Obj.make(Message.Message, {
140
160
  created: new Date().toISOString(),
141
161
  sender: { role: 'assistant' },
142
162
  blocks,
@@ -169,15 +189,18 @@ export class AiSession {
169
189
  // TODO(wittjosiah): Sometimes tool error results are added to the queue before the tool agent statuses.
170
190
  // This results in a broken execution graph.
171
191
  yield* submitMessage(
172
- Obj.make(DataType.Message, {
192
+ Obj.make(Message.Message, {
173
193
  created: new Date().toISOString(),
174
194
  sender: { role: 'tool' },
175
195
  blocks: toolResults,
176
196
  }),
177
197
  );
198
+
199
+ this._toolCalls++;
178
200
  } while (true);
179
201
 
180
- log('done', { pending: this._pending.length });
202
+ this._ended = Date.now();
203
+ log('done', { pending: this._pending.length, duration: this.duration, tools: this._toolCalls });
181
204
  return this._pending;
182
205
  }).pipe(this._semaphore.withPermits(1), Effect.withSpan('AiSession.run'));
183
206
 
@@ -187,7 +210,7 @@ export class AiSession {
187
210
  // TODO(burdon): Implement or remove.
188
211
  async runStructured<S extends Schema.Schema.AnyNoContext>(
189
212
  _schema: S,
190
- _options: AiSessionRunParams<any>,
213
+ _options: AiSessionRunProps<any>,
191
214
  ): Promise<Schema.Schema.Type<S>> {
192
215
  return todo();
193
216
  // const parser = structuredOutputParser(schema);
@@ -4,45 +4,36 @@
4
4
 
5
5
  import type * as Tool from '@effect/ai/Tool';
6
6
  import * as Toolkit from '@effect/ai/Toolkit';
7
- import type * as Context from 'effect/Context';
8
7
  import * as Effect from 'effect/Effect';
9
8
 
10
- import { type AiToolNotFoundError, ToolExecutionService, type ToolId, ToolResolverService } from '@dxos/ai';
9
+ import { type AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';
11
10
  import { type Blueprint } from '@dxos/blueprints';
12
11
  import { isTruthy } from '@dxos/util';
13
12
 
14
- export type ToolkitParams<Tools extends Record<string, Tool.Any>> = {
15
- toolkit?: Toolkit.Toolkit<Tools>;
16
- toolIds?: ToolId[];
17
- blueprints?: Blueprint.Blueprint[];
13
+ export type CreateToolkitProps = {
14
+ toolkit?: Toolkit.Any;
15
+ blueprints?: readonly Blueprint.Blueprint[];
18
16
  };
19
17
 
20
18
  /**
21
19
  * Build a combined toolkit from the blueprint tools and the provided toolkit.
22
20
  */
23
- export const createToolkit = <Tools extends Record<string, Tool.Any> = {}>({
24
- toolkit: toolkitParam,
25
- toolIds = [],
21
+ export const createToolkit = ({
22
+ toolkit: toolkitProp,
26
23
  blueprints = [],
27
- }: ToolkitParams<Tools>): Effect.Effect<
24
+ }: CreateToolkitProps): Effect.Effect<
28
25
  Toolkit.WithHandler<any>,
29
26
  AiToolNotFoundError,
30
- ToolResolverService | ToolExecutionService | Tool.HandlersFor<Tools>
27
+ ToolResolverService | ToolExecutionService | Tool.HandlersFor<any>
31
28
  > =>
32
29
  Effect.gen(function* () {
33
- const blueprintToolkit = yield* ToolResolverService.resolveToolkit([
34
- ...blueprints.flatMap(({ tools }) => tools),
35
- ...toolIds,
36
- ]);
30
+ const blueprintToolkit = yield* ToolResolverService.resolveToolkit(blueprints.flatMap(({ tools }) => tools));
31
+ const blueprintToolHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));
37
32
 
38
- const blueprintToolkitHandler: Context.Context<Tool.HandlersFor<any>> = yield* blueprintToolkit.toContext(
39
- ToolExecutionService.handlersFor(blueprintToolkit),
40
- );
41
-
42
- const toolkit = Toolkit.merge(...[toolkitParam, blueprintToolkit].filter(isTruthy));
43
- return yield* toolkit.pipe(Effect.provide(blueprintToolkitHandler)) as any as Effect.Effect<
33
+ const toolkit = Toolkit.merge(...[toolkitProp, blueprintToolkit].filter(isTruthy));
34
+ return yield* toolkit.pipe(Effect.provide(blueprintToolHandler)) as any as Effect.Effect<
44
35
  Toolkit.WithHandler<any>,
45
36
  never,
46
- Tool.HandlersFor<Tools>
37
+ Tool.HandlersFor<any>
47
38
  >;
48
39
  });
@@ -4,10 +4,10 @@
4
4
 
5
5
  import * as Schema from 'effect/Schema';
6
6
 
7
- import { type AnyEchoObject, ObjectId } from '@dxos/echo/internal';
8
- import { type ObjectVersion } from '@dxos/echo-db';
9
- import { getVersion } from '@dxos/echo-db';
10
- import { type ContentBlock } from '@dxos/schema';
7
+ import { type Obj } from '@dxos/echo';
8
+ import { type ObjectVersion, getVersion } from '@dxos/echo-db';
9
+ import { ObjectId } from '@dxos/keys';
10
+ import { type ContentBlock } from '@dxos/types';
11
11
 
12
12
  // TODO(dmaretskyi): Extract.
13
13
  const ObjectVersionSchema = Schema.Unknown as Schema.Schema<ObjectVersion>;
@@ -27,11 +27,11 @@ export interface VersionPin extends Schema.Schema.Type<typeof VersionPinSchema>
27
27
 
28
28
  export const VersionPin: typeof VersionPinSchema & {
29
29
  DISPOSITION: 'version-pin';
30
- fromObject: (object: AnyEchoObject) => VersionPin;
30
+ fromObject: (object: Obj.Unknown) => VersionPin;
31
31
  createBlock: (pin: VersionPin) => ContentBlock.Any;
32
32
  } = class extends VersionPinSchema {
33
33
  static readonly DISPOSITION = 'version-pin';
34
- static fromObject(object: AnyEchoObject): VersionPin {
34
+ static fromObject(object: Obj.Unknown): VersionPin {
35
35
  return VersionPin.make({
36
36
  objectId: object.id,
37
37
  version: getVersion(object),
@@ -45,16 +45,13 @@ You can reply with a block reference for objects if you have the DXN. For exampl
45
45
 
46
46
  Each suggestion must in the form of a proposed action enclosed in a <suggestion> tag.
47
47
  Example: <suggestion>Show on a map</suggestion>
48
+ Do not use bullets when listing suggestions.
48
49
 
49
50
  ### Multiple choice questions
50
51
 
51
52
  You may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.
52
53
  Example: <select><option>Yes</option><option>No</option></select>
53
54
 
54
- ## Toolkit
55
-
56
- If the user asks for a list of tools, then just emit a single self-closing <toolkit/> tag instead of listing the tools.
57
-
58
55
  ## Context
59
56
 
60
57
  The current date and time is {{DATETIME}}.
@@ -7,8 +7,8 @@ import defaultsDeep from 'lodash.defaultsdeep';
7
7
  import { type ModelCapabilities } from '@dxos/ai';
8
8
  import { Template } from '@dxos/blueprints';
9
9
 
10
- import FORMAT from './format.tpl?raw';
11
- import SYSTEM from './system.tpl?raw';
10
+ import FORMAT from './instructions/format.tpl?raw';
11
+ import SYSTEM from './instructions/system.tpl?raw';
12
12
 
13
13
  /**
14
14
  * System template variables.
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ export * from './layer';
@@ -0,0 +1,84 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { Registry } from '@effect-atom/atom';
6
+ import * as Layer from 'effect/Layer';
7
+ import * as Match from 'effect/Match';
8
+
9
+ import { AiService, type ModelName } from '@dxos/ai';
10
+ import { TestAiService } from '@dxos/ai/testing';
11
+ import type { Type } from '@dxos/echo';
12
+ import { CredentialsService, type FunctionDefinition, type ServiceCredential, TracingService } from '@dxos/functions';
13
+ import { TracingServiceExt, TriggerDispatcher, TriggerStateStore } from '@dxos/functions-runtime';
14
+ import { FunctionInvocationServiceLayerTest, TestDatabaseLayer } from '@dxos/functions-runtime/testing';
15
+
16
+ import { ToolExecutionServices } from '../functions';
17
+ import { GenericToolkit } from '../session';
18
+
19
+ interface TestLayerOptions {
20
+ aiServicePreset?: 'direct' | 'edge-local' | 'edge-remote';
21
+ model?: ModelName;
22
+ functions?: FunctionDefinition.Any[];
23
+ toolkits?: GenericToolkit.GenericToolkit[];
24
+ types?: Type.Entity.Any[];
25
+ credentials?: ServiceCredential[];
26
+ /*
27
+ * Tracing configuration.
28
+ * @default 'noop'
29
+ */
30
+ tracing?: 'noop' | 'console' | 'pretty';
31
+
32
+ disableLlmMemoization?: boolean;
33
+ }
34
+
35
+ export const AssistantTestLayer = ({
36
+ aiServicePreset = 'direct',
37
+ model = '@anthropic/claude-opus-4-0',
38
+ functions = [],
39
+ toolkits = [],
40
+ types = [],
41
+ credentials = [],
42
+ tracing = 'noop',
43
+ disableLlmMemoization = false,
44
+ }: TestLayerOptions = {}) => {
45
+ const toolkit = GenericToolkit.merge(...toolkits);
46
+ return Layer.mergeAll(AiService.model(model), ToolExecutionServices).pipe(
47
+ Layer.provideMerge(
48
+ FunctionInvocationServiceLayerTest({
49
+ functions,
50
+ }),
51
+ ),
52
+ Layer.provideMerge(
53
+ Layer.mergeAll(
54
+ TestAiService({ preset: aiServicePreset, disableMemoization: disableLlmMemoization }),
55
+ TestDatabaseLayer({
56
+ spaceKey: 'fixed',
57
+ types,
58
+ }),
59
+ CredentialsService.configuredLayer(credentials),
60
+ Match.value(tracing).pipe(
61
+ Match.when('noop', () => TracingService.layerNoop),
62
+ Match.when('console', () => TracingServiceExt.layerLogInfo()),
63
+ Match.when('pretty', () =>
64
+ TracingServiceExt.layerConsolePrettyPrint({
65
+ toolkit: (toolkits.length > 0 ? GenericToolkit.merge(...toolkits) : GenericToolkit.empty).toolkit as any,
66
+ }),
67
+ ),
68
+ Match.exhaustive,
69
+ ),
70
+ ),
71
+ ),
72
+ Layer.provideMerge(GenericToolkit.providerLayer(toolkit)),
73
+ );
74
+ };
75
+
76
+ interface TestLayerWithTriggersOptions extends TestLayerOptions {}
77
+
78
+ export const AssistantTestLayerWithTriggers = (options: TestLayerWithTriggersOptions) =>
79
+ Layer.mergeAll(
80
+ AssistantTestLayer(options),
81
+ TriggerDispatcher.layer({ timeControl: 'manual', startingTime: new Date('2025-09-05T15:01:00.000Z') }).pipe(
82
+ Layer.provide(Registry.layer),
83
+ ),
84
+ ).pipe(Layer.provideMerge(TriggerStateStore.layerMemory));
@@ -2,10 +2,13 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
+ import type * as Effect from 'effect/Effect';
5
6
  import * as Schema from 'effect/Schema';
6
7
 
7
- import { type ObjectId } from '@dxos/echo/internal';
8
- import { DXN, LOCAL_SPACE_TAG, type SpaceId } from '@dxos/keys';
8
+ import { type Err, Type } from '@dxos/echo';
9
+ import { Database } from '@dxos/echo';
10
+ import { EncodedReference } from '@dxos/echo-protocol';
11
+ import { DXN, LOCAL_SPACE_TAG, type ObjectId, type SpaceId } from '@dxos/keys';
9
12
  import { trim } from '@dxos/util';
10
13
 
11
14
  /**
@@ -13,21 +16,33 @@ import { trim } from '@dxos/util';
13
16
  */
14
17
  export const createArtifactElement = (id: ObjectId) => `<artifact id=${id} />`;
15
18
 
19
+ // TODO(burdon): Rename RefFromLLM? -- yes -dm
16
20
  /**
17
21
  * A model-friendly way to reference an object.
18
22
  * Supports vairous formats that will be normalized to a DXN.
23
+ *
24
+ * @deprecated Use `Type.Ref(XXX)` instead.
19
25
  */
20
- // TODO(burdon): Rename ObjectReference?
21
26
  export const ArtifactId: Schema.Schema<string> & {
22
27
  toDXN: (reference: ArtifactId, owningSpaceId?: SpaceId) => DXN;
28
+ resolve: <S extends Type.Entity.Any>(
29
+ schema: S,
30
+ ref: ArtifactId,
31
+ ) => Effect.Effect<Schema.Schema.Type<S>, Err.ObjectNotFoundError, Database.Service>;
23
32
  } = class extends Schema.String.annotations({
33
+ // TODO(dmaretskyi): This section gets overriden.
24
34
  description: trim`
25
35
  The ID of the referenced object. Formats accepted:
26
- - DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
36
+ - DXN (dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4). DXNs can be prepended with an @ symbol for compatibility with in-text references.
27
37
  - space ID, object ID tuple (spaceID:objectID)
28
- - Only object ID that is assumed to be in the current space (XXXXX)
38
+ - Only object ID that is assumed to be in the current space (01KG7R1ZXWFMWQ4DA1Q6TN1DG4)
29
39
  `,
30
- examples: ['dxn:echo:@:XXXXX', '@dxn:echo:@:XXXXX', 'spaceID:objectID', 'XXXXX'],
40
+ examples: [
41
+ 'dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4',
42
+ '@dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4',
43
+ 'BM3FSHFOMJCHCG5QW7JTVKGYABD2GAA7G:01KG7R1ZXWFMWQ4DA1Q6TN1DG4',
44
+ '01KG7R1ZXWFMWQ4DA1Q6TN1DG4',
45
+ ],
31
46
  }) {
32
47
  static toDXN(reference: ArtifactId, owningSpaceId?: SpaceId): DXN {
33
48
  // Allow @dxn: prefix for compatibility with in-text references.
@@ -47,6 +62,28 @@ export const ArtifactId: Schema.Schema<string> & {
47
62
  throw new Error(`Unable to parse object reference: ${reference}`);
48
63
  }
49
64
  }
65
+
66
+ /**
67
+ * Resolves an artifact ID to an object.
68
+ */
69
+ static resolve<S extends Type.Entity.Any>(
70
+ schema: S,
71
+ ref: ArtifactId,
72
+ ): Effect.Effect<Schema.Schema.Type<S>, Err.ObjectNotFoundError, Database.Service> {
73
+ const dxn = ArtifactId.toDXN(ref);
74
+ return Database.resolve(dxn, schema);
75
+ }
50
76
  };
51
77
 
52
78
  export type ArtifactId = Schema.Schema.Type<typeof ArtifactId>;
79
+
80
+ /**
81
+ * Schema that decodes ECHO reference object from an LLM-friendly input.
82
+ */
83
+ export const RefFromLLM = Schema.transform(ArtifactId, Type.Ref(Type.Obj), {
84
+ decode: (fromA, fromI) => EncodedReference.fromDXN(ArtifactId.toDXN(fromA)),
85
+ encode: (toI, toA) => EncodedReference.toDXN(toI).toString(),
86
+ strict: false,
87
+ }).annotations({
88
+ description: ArtifactId.ast.annotations.description as string,
89
+ });