@dxos/assistant 0.8.4-main.3a94e84 → 0.8.4-main.5acf9ea

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/assistant",
3
- "version": "0.8.4-main.3a94e84",
3
+ "version": "0.8.4-main.5acf9ea",
4
4
  "description": "Natural language interface to DXOS",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -12,7 +12,8 @@
12
12
  ".": {
13
13
  "types": "./dist/types/src/index.d.ts",
14
14
  "browser": "./dist/lib/browser/index.mjs",
15
- "node": "./dist/lib/node-esm/index.mjs"
15
+ "node": "./dist/lib/node-esm/index.mjs",
16
+ "source": "./src/index.ts"
16
17
  }
17
18
  },
18
19
  "types": "dist/types/src/index.d.ts",
@@ -31,26 +32,26 @@
31
32
  "exa-js": "^1.5.12",
32
33
  "handlebars": "4.7.8",
33
34
  "lodash.defaultsdeep": "^4.6.1",
34
- "@dxos/ai": "0.8.4-main.3a94e84",
35
- "@dxos/blueprints": "0.8.4-main.3a94e84",
36
- "@dxos/async": "0.8.4-main.3a94e84",
37
- "@dxos/context": "0.8.4-main.3a94e84",
38
- "@dxos/debug": "0.8.4-main.3a94e84",
39
- "@dxos/echo": "0.8.4-main.3a94e84",
40
- "@dxos/echo-schema": "0.8.4-main.3a94e84",
41
- "@dxos/effect": "0.8.4-main.3a94e84",
42
- "@dxos/echo-db": "0.8.4-main.3a94e84",
43
- "@dxos/echo-protocol": "0.8.4-main.3a94e84",
44
- "@dxos/errors": "0.8.4-main.3a94e84",
45
- "@dxos/functions": "0.8.4-main.3a94e84",
46
- "@dxos/graph": "0.8.4-main.3a94e84",
47
- "@dxos/invariant": "0.8.4-main.3a94e84",
48
- "@dxos/keys": "0.8.4-main.3a94e84",
49
- "@dxos/live-object": "0.8.4-main.3a94e84",
50
- "@dxos/log": "0.8.4-main.3a94e84",
51
- "@dxos/schema": "0.8.4-main.3a94e84",
52
- "@dxos/node-std": "0.8.4-main.3a94e84",
53
- "@dxos/util": "0.8.4-main.3a94e84"
35
+ "@dxos/ai": "0.8.4-main.5acf9ea",
36
+ "@dxos/async": "0.8.4-main.5acf9ea",
37
+ "@dxos/blueprints": "0.8.4-main.5acf9ea",
38
+ "@dxos/context": "0.8.4-main.5acf9ea",
39
+ "@dxos/debug": "0.8.4-main.5acf9ea",
40
+ "@dxos/echo-db": "0.8.4-main.5acf9ea",
41
+ "@dxos/echo-protocol": "0.8.4-main.5acf9ea",
42
+ "@dxos/echo-schema": "0.8.4-main.5acf9ea",
43
+ "@dxos/echo": "0.8.4-main.5acf9ea",
44
+ "@dxos/effect": "0.8.4-main.5acf9ea",
45
+ "@dxos/errors": "0.8.4-main.5acf9ea",
46
+ "@dxos/functions": "0.8.4-main.5acf9ea",
47
+ "@dxos/invariant": "0.8.4-main.5acf9ea",
48
+ "@dxos/keys": "0.8.4-main.5acf9ea",
49
+ "@dxos/graph": "0.8.4-main.5acf9ea",
50
+ "@dxos/live-object": "0.8.4-main.5acf9ea",
51
+ "@dxos/log": "0.8.4-main.5acf9ea",
52
+ "@dxos/node-std": "0.8.4-main.5acf9ea",
53
+ "@dxos/util": "0.8.4-main.5acf9ea",
54
+ "@dxos/schema": "0.8.4-main.5acf9ea"
54
55
  },
55
56
  "devDependencies": {
56
57
  "@types/lodash.defaultsdeep": "^4.6.6",
@@ -6,8 +6,7 @@ import { AiTool } from '@effect/ai';
6
6
  import { Context, Effect, Layer, Match, Predicate, Record, Schema } from 'effect';
7
7
 
8
8
  import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';
9
- import { todo } from '@dxos/debug';
10
- import { FunctionError, type FunctionContext, type FunctionDefinition, type Services } from '@dxos/functions';
9
+ import { LocalFunctionExecutionService, type FunctionDefinition } from '@dxos/functions';
11
10
  import { invariant } from '@dxos/invariant';
12
11
 
13
12
  export const makeToolResolverFromFunctions = (
@@ -27,24 +26,32 @@ export const makeToolResolverFromFunctions = (
27
26
 
28
27
  export const makeToolExecutionServiceFromFunctions = (
29
28
  functions: FunctionDefinition<any, any>[],
30
- ): Layer.Layer<ToolExecutionService> => {
31
- return Layer.succeed(ToolExecutionService, {
32
- handlersFor: (toolkit) => {
33
- const makeHandler = (tool: AiTool.Any): ((params: unknown) => Effect.Effect<unknown, any, any>) => {
34
- return Effect.fn('toolFunctionHandler')(function* (input: any) {
35
- const { functionName } = Context.get(FunctionToolAnnotation)(tool.annotations as any);
36
- const fnDef = functions.find((f) => f.name === functionName);
37
- if (!fnDef) {
38
- return yield* Effect.fail(new AiToolNotFoundError(tool.name));
39
- }
40
-
41
- return yield* invokeFunction(fnDef, input).pipe(Effect.catchAllDefect((defect) => Effect.fail(defect)));
42
- });
29
+ ): Layer.Layer<ToolExecutionService, never, LocalFunctionExecutionService> => {
30
+ return Layer.effect(
31
+ ToolExecutionService,
32
+ Effect.gen(function* () {
33
+ const localFunctionExecutionService = yield* LocalFunctionExecutionService;
34
+ return {
35
+ handlersFor: (toolkit) => {
36
+ const makeHandler = (tool: AiTool.Any): ((params: unknown) => Effect.Effect<unknown, any, any>) => {
37
+ return Effect.fn('toolFunctionHandler')(function* (input: any) {
38
+ const { functionName } = Context.get(FunctionToolAnnotation)(tool.annotations as any);
39
+ const fnDef = functions.find((f) => f.name === functionName);
40
+ if (!fnDef) {
41
+ return yield* Effect.fail(new AiToolNotFoundError(tool.name));
42
+ }
43
+
44
+ return yield* localFunctionExecutionService
45
+ .invokeFunction(fnDef, input)
46
+ .pipe(Effect.catchAllDefect((defect) => Effect.fail(defect)));
47
+ });
48
+ };
49
+
50
+ return toolkit.of(Record.map(toolkit.tools, (tool, name) => makeHandler(tool)) as any) as any;
51
+ },
43
52
  };
44
-
45
- return toolkit.of(Record.map(toolkit.tools, (tool, name) => makeHandler(tool)) as any) as any;
46
- },
47
- });
53
+ }),
54
+ );
48
55
  };
49
56
 
50
57
  /**
@@ -90,50 +97,3 @@ const createStructFieldsFromSchema = (schema: Schema.Schema<any, any>): Record<s
90
97
  Match.orElseAbsurd,
91
98
  );
92
99
  };
93
-
94
- // TODO(dmaretskyi): Factor out.
95
- const invokeFunction = (fnDef: FunctionDefinition<any, any>, input: any): Effect.Effect<unknown, never, Services> =>
96
- Effect.gen(function* () {
97
- // Assert input matches schema
98
- const assertInput = fnDef.inputSchema.pipe(Schema.asserts);
99
- (assertInput as any)(input);
100
-
101
- const context: FunctionContext = {
102
- getService: () => todo(),
103
- getSpace: async (_spaceId: any) => {
104
- throw new Error('Not available. Use the database service instead.');
105
- },
106
- space: undefined,
107
- get ai(): never {
108
- throw new Error('Not available. Use the ai service instead.');
109
- },
110
- };
111
-
112
- // TODO(dmaretskyi): This should be delegated to a function invoker service.
113
- const data = yield* Effect.gen(function* () {
114
- const result = fnDef.handler({ context, data: input });
115
- if (Effect.isEffect(result)) {
116
- return yield* (result as Effect.Effect<unknown, unknown, Services>).pipe(Effect.orDie);
117
- } else if (
118
- typeof result === 'object' &&
119
- result !== null &&
120
- 'then' in result &&
121
- typeof result.then === 'function'
122
- ) {
123
- return yield* Effect.promise(() => result);
124
- } else {
125
- return result;
126
- }
127
- }).pipe(
128
- Effect.orDie,
129
- Effect.catchAllDefect((defect) =>
130
- Effect.die(new FunctionError('Error running function', { context: { name: fnDef.name }, cause: defect })),
131
- ),
132
- );
133
-
134
- // Assert output matches schema
135
- const assertOutput = fnDef.outputSchema?.pipe(Schema.asserts);
136
- (assertOutput as any)(data);
137
-
138
- return data;
139
- }).pipe(Effect.withSpan('invokeFunction', { attributes: { name: fnDef.name } }));
@@ -134,7 +134,7 @@ export const LocalSearchHandler = LocalSearchToolkit.toLayer({
134
134
  /**
135
135
  * Attached as an annotation to the writer tool.
136
136
  */
137
- class GraphWriterSchema extends Context.Tag('GraphWriterSchema')<
137
+ class GraphWriterSchema extends Context.Tag('@dxos/assistant/GraphWriterSchema')<
138
138
  GraphWriterSchema,
139
139
  { schema: Schema.Schema.AnyNoContext[] }
140
140
  >() {}
@@ -137,7 +137,7 @@ export class AiSession {
137
137
  // TODO(dmaretskyi): Loading BP from the Database should be done at the higher level. We need a type for the resolved blueprint.
138
138
  const systemPrompt = yield* pipe(
139
139
  blueprints,
140
- Effect.forEach((blueprint) => DatabaseService.loadRef(blueprint.instructions)),
140
+ Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)),
141
141
  Effect.flatMap(Effect.forEach((template) => DatabaseService.loadRef(template.source))),
142
142
  Effect.map(Array.map((template) => `\n\n<blueprint>${template.content}</blueprint>`)),
143
143
  Effect.map(Array.reduce(options.systemPrompt ?? '', String.concat)),
@@ -305,7 +305,7 @@ const createArtifactUpdateBlock = (
305
305
  * Can be optionally provided to the session run call.
306
306
  */
307
307
  // TODO(dmaretskyi): Convert to Context.Reference
308
- export class ArtifactDiffResolver extends Context.Tag('ArtifactDiffResolver')<
308
+ export class ArtifactDiffResolver extends Context.Tag('@dxos/assistant/ArtifactDiffResolver')<
309
309
  ArtifactDiffResolver,
310
310
  ArtifactDiffResolver.Service
311
311
  >() {}
@@ -78,9 +78,8 @@ const summarizationFn = defineFunction({
78
78
  ],
79
79
  }),
80
80
  );
81
- return Obj.make(DataType.Text, {
82
- content: pipe(result[0]?.blocks[0], (c) => (c?._tag === 'text' ? c.text : '')),
83
- });
81
+
82
+ return DataType.makeText(pipe(result[0]?.blocks[0], (c) => (c?._tag === 'text' ? c.text : '')));
84
83
  },
85
84
  });
86
85
 
@@ -135,9 +134,7 @@ const refinementFn = defineFunction({
135
134
  }),
136
135
  );
137
136
  return {
138
- summary: Obj.make(DataType.Text, {
139
- content: pipe(result[0]?.blocks[0], (c) => (c?._tag === 'text' ? c.text : '')),
140
- }),
137
+ summary: DataType.makeText(pipe(result[0]?.blocks[0], (c) => (c?._tag === 'text' ? c.text : ''))),
141
138
  };
142
139
  },
143
140
  });
@@ -166,9 +163,7 @@ describe.skip('Summarization', () => {
166
163
  test('keeps transcript outline', { timeout: 1000_000 }, async () => {
167
164
  const { transcriptMessages } = createTestData();
168
165
 
169
- const summary = Obj.make(DataType.Text, {
170
- content: '',
171
- });
166
+ const summary = DataType.makeText();
172
167
 
173
168
  const summaries = [];
174
169
 
@@ -179,12 +174,10 @@ describe.skip('Summarization', () => {
179
174
  transcript: blocks,
180
175
  });
181
176
  summary.content = result.content;
182
- summaries.push(Obj.make(DataType.Text, { content: result.content }));
177
+ summaries.push(DataType.makeText(result.content));
183
178
 
184
179
  console.log(blocks.at(-1));
185
180
  console.log();
186
- console.log('--------------------------------');
187
- console.log();
188
181
  console.log(summary.content);
189
182
  console.log();
190
183
 
@@ -194,12 +187,10 @@ describe.skip('Summarization', () => {
194
187
  summaries: history,
195
188
  });
196
189
  summary.content = result.summary.content;
197
- summaries.push(Obj.make(DataType.Text, { content: result.summary.content }));
190
+ summaries.push(DataType.makeText(result.summary.content));
198
191
 
199
192
  console.log('REFINED');
200
193
  console.log();
201
- console.log('--------------------------------');
202
- console.log();
203
194
  console.log(summary.content);
204
195
  console.log();
205
196
  }