@dxos/assistant 0.8.4-main.406dc2a → 0.8.4-main.548089c

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 (82) hide show
  1. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  2. package/dist/lib/browser/extraction/index.mjs +114 -161
  3. package/dist/lib/browser/extraction/index.mjs.map +4 -4
  4. package/dist/lib/browser/index.mjs +508 -151
  5. package/dist/lib/browser/index.mjs.map +4 -4
  6. package/dist/lib/browser/meta.json +1 -1
  7. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  8. package/dist/lib/node-esm/extraction/index.mjs +114 -161
  9. package/dist/lib/node-esm/extraction/index.mjs.map +4 -4
  10. package/dist/lib/node-esm/index.mjs +508 -151
  11. package/dist/lib/node-esm/index.mjs.map +4 -4
  12. package/dist/lib/node-esm/meta.json +1 -1
  13. package/dist/types/src/conversation/conversation.d.ts +4 -7
  14. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  15. package/dist/types/src/diff.d.ts.map +1 -1
  16. package/dist/types/src/errors.d.ts +8 -16
  17. package/dist/types/src/errors.d.ts.map +1 -1
  18. package/dist/types/src/execution-graph.d.ts +3 -0
  19. package/dist/types/src/execution-graph.d.ts.map +1 -1
  20. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  21. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +28 -24
  22. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  23. package/dist/types/src/extraction/extraction.d.ts +14 -7
  24. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  25. package/dist/types/src/functions/services.d.ts +2 -1
  26. package/dist/types/src/functions/services.d.ts.map +1 -1
  27. package/dist/types/src/session/GenericToolkit.d.ts +84 -0
  28. package/dist/types/src/session/GenericToolkit.d.ts.map +1 -0
  29. package/dist/types/src/session/format.d.ts +16 -16
  30. package/dist/types/src/session/format.d.ts.map +1 -1
  31. package/dist/types/src/session/index.d.ts +1 -0
  32. package/dist/types/src/session/index.d.ts.map +1 -1
  33. package/dist/types/src/session/observer.d.ts +2 -2
  34. package/dist/types/src/session/observer.d.ts.map +1 -1
  35. package/dist/types/src/session/session.d.ts +3 -3
  36. package/dist/types/src/session/session.d.ts.map +1 -1
  37. package/dist/types/src/session/version-pin.d.ts +1 -1
  38. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  39. package/dist/types/src/types/artifact.d.ts +4 -0
  40. package/dist/types/src/types/artifact.d.ts.map +1 -1
  41. package/dist/types/tsconfig.tsbuildinfo +1 -1
  42. package/package.json +22 -20
  43. package/src/conversation/conversation.ts +8 -12
  44. package/src/diff.test.ts +3 -3
  45. package/src/diff.ts +5 -0
  46. package/src/execution-graph.test.ts +44 -42
  47. package/src/execution-graph.ts +19 -10
  48. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +2 -2
  49. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +44 -54
  50. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +10 -9
  51. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +36 -44
  52. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +10 -5
  53. package/src/extraction/extraction.ts +5 -4
  54. package/src/functions/services.ts +11 -7
  55. package/src/session/GenericToolkit.ts +130 -0
  56. package/src/session/format.test.ts +4 -3
  57. package/src/session/format.ts +4 -5
  58. package/src/session/index.ts +2 -0
  59. package/src/session/observer.ts +2 -2
  60. package/src/session/session.test.ts +2 -2
  61. package/src/session/session.ts +9 -8
  62. package/src/session/version-pin.ts +2 -3
  63. package/src/templates/{format.tpl → instructions/format.tpl} +1 -0
  64. package/src/templates/system.ts +2 -2
  65. package/src/types/artifact.ts +20 -1
  66. package/dist/lib/browser/chunk-CWGVG235.mjs +0 -380
  67. package/dist/lib/browser/chunk-CWGVG235.mjs.map +0 -7
  68. package/dist/lib/browser/chunk-UB26N5UF.mjs +0 -89
  69. package/dist/lib/browser/chunk-UB26N5UF.mjs.map +0 -7
  70. package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +0 -13
  71. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +0 -91
  72. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +0 -7
  73. package/dist/lib/node-esm/chunk-OV5PMQPB.mjs +0 -382
  74. package/dist/lib/node-esm/chunk-OV5PMQPB.mjs.map +0 -7
  75. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
  76. package/dist/types/src/summarization/summarization.test.d.ts +0 -2
  77. package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
  78. package/src/summarization/summarization.test.ts +0 -202
  79. /package/dist/lib/browser/{named-entity-recognition-QPYHML66.mjs.map → chunk-J5LGTIGS.mjs.map} +0 -0
  80. /package/dist/lib/node-esm/{named-entity-recognition-2HDF46YX.mjs.map → chunk-HSLMI22Q.mjs.map} +0 -0
  81. /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
  82. /package/src/templates/{system.tpl → instructions/system.tpl} +0 -0
@@ -2,20 +2,9 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- // ISSUE(burdon): defineFunction
6
- // @ts-nocheck
7
-
8
- import { AiService } from '@dxos/ai';
9
- import { Obj } from '@dxos/echo';
10
- import { create } from '@dxos/echo/internal';
11
5
  import { type FunctionDefinition, defineFunction } from '@dxos/functions';
12
- import { type ContentBlock, DataType } from '@dxos/schema';
13
6
 
14
- import { AiSession } from '../../session';
15
7
  import { ExtractionInput, ExtractionOutput } from '../extraction';
16
- import { ReferencedQuotes, insertReferences } from '../quotes';
17
-
18
- import PROMPT from './instructions.tpl?raw';
19
8
 
20
9
  export const extractionAnthropicFunction: FunctionDefinition<ExtractionInput, ExtractionOutput> = defineFunction({
21
10
  key: 'dxos.org/function/extraction/extract-entities',
@@ -24,49 +13,50 @@ export const extractionAnthropicFunction: FunctionDefinition<ExtractionInput, Ex
24
13
  inputSchema: ExtractionInput,
25
14
  outputSchema: ExtractionOutput,
26
15
  handler: async ({ data: { message, objects }, context }) => {
27
- const startTime = performance.now();
28
- const ai = context.getService(AiService.AiService);
29
- const session = new AiSession({ operationModel: 'configured' });
30
- const result = await session.runStructured(ReferencedQuotes, {
31
- generationOptions: {
32
- model: '@anthropic/claude-3-5-haiku-20241022',
33
- },
34
- client: ai.client,
35
- systemPrompt: PROMPT,
36
- history: [
37
- Obj.make(DataType.Message, {
38
- created: new Date().toISOString(),
39
- sender: { role: 'user' },
40
- blocks: [
41
- {
42
- _tag: 'text',
43
- text: `<context>${JSON.stringify(objects)}</context>`,
44
- } satisfies ContentBlock.Text,
45
- {
46
- _tag: 'text',
47
- text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`,
48
- } satisfies ContentBlock.Text,
49
- ],
50
- }),
51
- ],
52
- artifacts: [],
53
- prompt: '',
54
- tools: [],
55
- } as any); // TODO(burdon): Rewrite test.
16
+ throw new Error('not implemented');
17
+ // const startTime = performance.now();
18
+ // const ai = context.getService(AiService.AiService);
19
+ // const session = new AiSession({ operationModel: 'configured' });
20
+ // const result = await session.runStructured(ReferencedQuotes, {
21
+ // generationOptions: {
22
+ // model: '@anthropic/claude-3-5-haiku-20241022',
23
+ // },
24
+ // client: ai.client,
25
+ // systemPrompt: PROMPT,
26
+ // history: [
27
+ // Obj.make(DataType.Message.Message, {
28
+ // created: new Date().toISOString(),
29
+ // sender: { role: 'user' },
30
+ // blocks: [
31
+ // {
32
+ // _tag: 'text',
33
+ // text: `<context>${JSON.stringify(objects)}</context>`,
34
+ // } satisfies DataType.ContentBlock.Text,
35
+ // {
36
+ // _tag: 'text',
37
+ // text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`,
38
+ // } satisfies DataType.ContentBlock.Text,
39
+ // ],
40
+ // }),
41
+ // ],
42
+ // artifacts: [],
43
+ // prompt: '',
44
+ // tools: [],
45
+ // } as any); // TODO(burdon): Rewrite test.
56
46
 
57
- return {
58
- timeElapsed: performance.now() - startTime,
59
- message: create(DataType.Message, {
60
- ...message,
61
- blocks: message.blocks.map((block) =>
62
- block._tag !== 'transcript'
63
- ? block
64
- : {
65
- ...block,
66
- text: insertReferences(block.text, result),
67
- },
68
- ),
69
- }),
70
- };
47
+ // return {
48
+ // timeElapsed: performance.now() - startTime,
49
+ // message: create(DataType.Message.Message, {
50
+ // ...message,
51
+ // blocks: message.blocks.map((block) =>
52
+ // block._tag !== 'transcript'
53
+ // ? block
54
+ // : {
55
+ // ...block,
56
+ // text: insertReferences(block.text, result),
57
+ // },
58
+ // ),
59
+ // }),
60
+ // };
71
61
  },
72
62
  });
@@ -6,10 +6,11 @@ import { beforeAll, describe, test } from 'vitest';
6
6
 
7
7
  import { type EchoDatabase } from '@dxos/echo-db';
8
8
  import { EchoTestBuilder } from '@dxos/echo-db/testing';
9
- import { FunctionExecutor, ServiceContainer } from '@dxos/functions';
9
+ import { FunctionExecutor, ServiceContainer } from '@dxos/functions-runtime';
10
10
  import { log } from '@dxos/log';
11
- import { DataType } from '@dxos/schema';
12
- import { Testing, createTestData } from '@dxos/schema/testing';
11
+ import { Testing } from '@dxos/schema/testing';
12
+ import { type Message, Organization, Person } from '@dxos/types';
13
+ import { createTestData } from '@dxos/types/testing';
13
14
  import { range } from '@dxos/util';
14
15
 
15
16
  import { processTranscriptMessage } from '../extraction';
@@ -21,15 +22,15 @@ describe.skip('NER EntityExtraction', () => {
21
22
  let db: EchoDatabase;
22
23
  let executor: FunctionExecutor;
23
24
  let testData: {
24
- transcriptJosiah: DataType.Message[];
25
- transcriptWoflram: DataType.Message[];
26
- transcriptMessages: DataType.Message[];
25
+ transcriptJosiah: Message.Message[];
26
+ transcriptWoflram: Message.Message[];
27
+ transcriptMessages: Message.Message[];
27
28
  documents: Testing.DocumentType[];
28
- contacts: Record<string, DataType.Person>;
29
- organizations: Record<string, DataType.Organization>;
29
+ contacts: Record<string, Person.Person>;
30
+ organizations: Record<string, Organization.Organization>;
30
31
  };
31
32
 
32
- const TYPES = [DataType.Organization, DataType.Person, Testing.Contact, Testing.DocumentType];
33
+ const TYPES = [Organization.Organization, Person.Person, Testing.Person, Testing.DocumentType];
33
34
 
34
35
  beforeAll(async () => {
35
36
  // TODO(dmaretskyi): Helper to scaffold this from a config.
@@ -2,18 +2,9 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- // ISSUE(burdon): defineFunction
6
- // @ts-nocheck
7
-
8
- import { asyncTimeout } from '@dxos/async';
9
- import { create } from '@dxos/echo/internal';
10
- import { DatabaseService, defineFunction } from '@dxos/functions';
11
- import { invariant } from '@dxos/invariant';
12
- import { log } from '@dxos/log';
13
- import { DataType } from '@dxos/schema';
5
+ import { defineFunction } from '@dxos/functions';
14
6
 
15
7
  import { ExtractionInput, ExtractionOutput } from '../extraction';
16
- import { findQuotes, insertReferences } from '../quotes';
17
8
 
18
9
  /**
19
10
  * Named Entity Recognition model
@@ -26,39 +17,40 @@ export const extractionNerFunction = defineFunction({
26
17
  inputSchema: ExtractionInput,
27
18
  outputSchema: ExtractionOutput,
28
19
  handler: async ({ data: { message, options }, context }) => {
29
- log.info('input', { message, options });
30
- const startTime = performance.now();
31
- const { db } = context.getService(DatabaseService);
32
- const { extractFullEntities } = await import('./named-entity-recognition');
33
-
34
- const entitiesPromise = Promise.all(
35
- message.blocks.map(async (block) => {
36
- invariant(block._tag === 'transcript' || block._tag === 'text');
37
- return extractFullEntities(block.text);
38
- }),
39
- ).then((entities) => entities.flat());
40
- const entities = options?.timeout ? await asyncTimeout(entitiesPromise, options.timeout) : await entitiesPromise;
41
-
42
- log.info('entities', { entities });
43
- const quoteReferences = await findQuotes(
44
- entities.map((entity) => entity.word),
45
- db,
46
- );
47
-
48
- const blocksWithReferences = message.blocks.map((block) => {
49
- invariant(block._tag === 'transcript' || block._tag === 'text');
50
- return { ...block, text: insertReferences(block._tag, quoteReferences) };
51
- });
52
-
53
- const messageWithReferences = create(DataType.Message, {
54
- ...message,
55
- blocks: blocksWithReferences,
56
- });
57
-
58
- log.info('output', { messageWithReferences });
59
- return {
60
- message: messageWithReferences,
61
- timeElapsed: performance.now() - startTime,
62
- };
20
+ throw new Error('not implemented');
21
+ // log.info('input', { message, options });
22
+ // const startTime = performance.now();
23
+ // const { db } = context.getService(DatabaseService);
24
+ // const { extractFullEntities } = await import('./named-entity-recognition');
25
+
26
+ // const entitiesPromise = Promise.all(
27
+ // message.blocks.map(async (block) => {
28
+ // invariant(block._tag === 'transcript' || block._tag === 'text');
29
+ // return extractFullEntities(block.text);
30
+ // }),
31
+ // ).then((entities) => entities.flat());
32
+ // const entities = options?.timeout ? await asyncTimeout(entitiesPromise, options.timeout) : await entitiesPromise;
33
+
34
+ // log.info('entities', { entities });
35
+ // const quoteReferences = await findQuotes(
36
+ // entities.map((entity) => entity.word),
37
+ // db,
38
+ // );
39
+
40
+ // const blocksWithReferences = message.blocks.map((block) => {
41
+ // invariant(block._tag === 'transcript' || block._tag === 'text');
42
+ // return { ...block, text: insertReferences(block._tag, quoteReferences) };
43
+ // });
44
+
45
+ // const messageWithReferences = create(Message.Message, {
46
+ // ...message,
47
+ // blocks: blocksWithReferences,
48
+ // });
49
+
50
+ // log.info('output', { messageWithReferences });
51
+ // return {
52
+ // message: messageWithReferences,
53
+ // timeElapsed: performance.now() - startTime,
54
+ // };
63
55
  },
64
56
  });
@@ -6,7 +6,8 @@ import { type TokenClassificationOutput, type TokenClassificationPipelineType }
6
6
  import { beforeAll, describe, expect, test } from 'vitest';
7
7
 
8
8
  import { log } from '@dxos/log';
9
- import { createTestData } from '@dxos/schema/testing';
9
+ import { type ContentBlock, type Message } from '@dxos/types';
10
+ import { createTestData } from '@dxos/types/testing';
10
11
 
11
12
  import { combineNerTokens, createTokenGroups, extractFullEntities, getNer } from './named-entity-recognition';
12
13
 
@@ -57,8 +58,10 @@ describe.skip('NamedEntityRecognition', () => {
57
58
  test('own data', async () => {
58
59
  const { transcriptWoflram, transcriptJosiah } = await createTestData();
59
60
 
60
- const blocks = [...transcriptWoflram, ...transcriptJosiah].flatMap((message) =>
61
- message.blocks.flatMap((block) => (block._tag === 'transcript' ? [block.text] : [])),
61
+ const blocks = [...transcriptWoflram, ...transcriptJosiah].flatMap((message: Message.Message) =>
62
+ message.blocks.flatMap((block: ContentBlock.Any) =>
63
+ block._tag === 'transcript' ? [(block as ContentBlock.Transcript).text] : [],
64
+ ),
62
65
  );
63
66
 
64
67
  for (const block of blocks) {
@@ -70,8 +73,10 @@ describe.skip('NamedEntityRecognition', () => {
70
73
  test('more own data', async () => {
71
74
  const { transcriptMessages } = await createTestData();
72
75
 
73
- const blocks = transcriptMessages.flatMap((message) =>
74
- message.blocks.flatMap((block) => (block._tag === 'transcript' ? [block.text] : [])),
76
+ const blocks = transcriptMessages.flatMap((message: Message.Message) =>
77
+ message.blocks.flatMap((block: ContentBlock.Any) =>
78
+ block._tag === 'transcript' ? [(block as ContentBlock.Transcript).text] : [],
79
+ ),
75
80
  );
76
81
 
77
82
  for (const block of blocks) {
@@ -6,12 +6,13 @@ import * as Schema from 'effect/Schema';
6
6
 
7
7
  import { asyncTimeout } from '@dxos/async';
8
8
  import { Expando } from '@dxos/echo/internal';
9
- import { type FunctionDefinition, type FunctionExecutor } from '@dxos/functions';
9
+ import { type FunctionDefinition } from '@dxos/functions';
10
+ import { type FunctionExecutor } from '@dxos/functions-runtime';
10
11
  import { log } from '@dxos/log';
11
- import { DataType } from '@dxos/schema';
12
+ import { Message } from '@dxos/types';
12
13
 
13
14
  export const ExtractionInput = Schema.Struct({
14
- message: DataType.Message,
15
+ message: Message.Message,
15
16
  objects: Schema.optional(Schema.Array(Expando)),
16
17
  options: Schema.optional(
17
18
  Schema.Struct({
@@ -23,7 +24,7 @@ export const ExtractionInput = Schema.Struct({
23
24
  export interface ExtractionInput extends Schema.Schema.Type<typeof ExtractionInput> {}
24
25
 
25
26
  export const ExtractionOutput = Schema.Struct({
26
- message: DataType.Message,
27
+ message: Message.Message,
27
28
  timeElapsed: Schema.Number,
28
29
  });
29
30
  export interface ExtractionOutput extends Schema.Schema.Type<typeof ExtractionOutput> {}
@@ -13,7 +13,8 @@ import * as Schema from 'effect/Schema';
13
13
  import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';
14
14
  import { todo } from '@dxos/debug';
15
15
  import { Query } from '@dxos/echo';
16
- import { DatabaseService, FunctionDefinition, FunctionInvocationService, FunctionType } from '@dxos/functions';
16
+ import { DatabaseService } from '@dxos/echo-db';
17
+ import { Function, FunctionDefinition, FunctionInvocationService } from '@dxos/functions';
17
18
  import { invariant } from '@dxos/invariant';
18
19
 
19
20
  /**
@@ -45,7 +46,7 @@ export const makeToolResolverFromFunctions = (
45
46
 
46
47
  const {
47
48
  objects: [dbFunction],
48
- } = yield* DatabaseService.runQuery(Query.type(FunctionType, { key: id }));
49
+ } = yield* DatabaseService.runQuery(Query.type(Function.Function, { key: id }));
49
50
 
50
51
  const functionDef = dbFunction
51
52
  ? FunctionDefinition.deserialize(dbFunction)
@@ -82,7 +83,8 @@ export const makeToolExecutionServiceFromFunctions = (
82
83
  throw new Error('Attempted to call a provider-defined tool');
83
84
  }
84
85
  // TODO(wittjosiah): Everything is `never` here.
85
- return yield* (toolkitHandler.handle as any)(tool.name, input);
86
+ const { result } = yield* (toolkitHandler.handle as any)(tool.name, input);
87
+ return result;
86
88
  }
87
89
 
88
90
  const { definition: functionDef } = Context.get(FunctionToolAnnotation)(tool.annotations as any);
@@ -114,14 +116,16 @@ const toolCache = new WeakMap<FunctionDefinition<any, any>, Tool.Any>();
114
116
  *
115
117
  * @param fn The function definition to project into a tool.
116
118
  * @param meta Optional projection metadata.
117
- * @param meta.deployedFunctionId Backend deployment ID used for remote invocation when present. This is the
118
- * EDGE service's function deployment identifier (not the ECHO object ID/DXN and not `FunctionDefinition.key`).
119
+ * @param meta.deployedFunctionId Backend deployment ID used for remote invocation when present.
120
+ * This is the EDGE service's function deployment identifier (not the ECHO object ID/DXN and not `FunctionDefinition.key`).
119
121
  */
120
122
  const projectFunctionToTool = (fn: FunctionDefinition<any, any>): Tool.Any => {
121
123
  if (toolCache.has(fn)) {
122
124
  return toolCache.get(fn)!;
123
125
  }
124
126
 
127
+ // TODO(burdon): Use and map function.key?
128
+ // TODO(dmaretskyi): Use function key instead.
125
129
  const tool = Tool.make(makeToolName(fn.name), {
126
130
  description: fn.description,
127
131
  parameters: createStructFieldsFromSchema(fn.inputSchema),
@@ -136,8 +140,8 @@ const projectFunctionToTool = (fn: FunctionDefinition<any, any>): Tool.Any => {
136
140
  * @returns Tool name produced from function name by escaping invalid characters.
137
141
  */
138
142
  const makeToolName = (name: string) => {
139
- const toolName = name.replace(/[^a-zA-Z0-9]/g, '_');
140
- invariant(toolName.match(/^[a-zA-Z_][a-zA-Z0-9-_]*$/));
143
+ const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, '-');
144
+ invariant(toolName.match(/^[a-z_][a-z0-9-_]*$/));
141
145
  return toolName;
142
146
  };
143
147
 
@@ -0,0 +1,130 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import type * as Tool from '@effect/ai/Tool';
6
+ import * as Toolkit from '@effect/ai/Toolkit';
7
+ import * as Effect from 'effect/Effect';
8
+ import * as Layer from 'effect/Layer';
9
+ import * as Pipeable from 'effect/Pipeable';
10
+ import type * as Schema from 'effect/Schema';
11
+ import type * as SchemaAST from 'effect/SchemaAST';
12
+
13
+ /**
14
+ * Unique identifier for generic toolkit instances.
15
+ */
16
+ export const TypeId = '~@dxos/assistant/GenericToolkit';
17
+
18
+ /**
19
+ * Type-level representation of the generic toolkit identifier.
20
+ */
21
+ export type TypeId = typeof TypeId;
22
+
23
+ /**
24
+ * Type-safe way to define toolkits where we don't know specific types of tools,
25
+ * and we want to bundle definition and handlers together.
26
+ *
27
+ * Usefull for plugin-based systems, where tools are dynamically added.
28
+ *
29
+ * @param TR requirements type to the tool invocation.
30
+ * @param E failure type to the handlers layer.
31
+ * @param R requirements type to the handlers layer.
32
+ */
33
+ export interface GenericToolkit<TR = never, E = never, R = never> extends Pipeable.Pipeable {
34
+ readonly [TypeId]: TypeId;
35
+
36
+ /**
37
+ * Toolkit definition.
38
+ */
39
+ readonly toolkit: Toolkit.Toolkit<GenericTools<TR>>;
40
+
41
+ /**
42
+ * Handlers layer.
43
+ */
44
+ readonly layer: Layer.Layer<unknown, E, R>;
45
+
46
+ /**
47
+ * Handlers effect.
48
+ */
49
+ readonly handlers: Effect.Effect<Toolkit.WithHandler<GenericTools<TR>>, E, R>;
50
+ }
51
+
52
+ /**
53
+ * Generic constraint.
54
+ *
55
+ * NOTE: Only use in place of `T extends PortableToolkit.Any`. Not suitable for standalone use.
56
+ */
57
+ export interface Any {
58
+ readonly [TypeId]: TypeId;
59
+ readonly toolkit: Toolkit.Toolkit<any>;
60
+ readonly layer: Layer.Layer<unknown, any, any>;
61
+ }
62
+
63
+ export type InvocationRequirements<T extends Any> = T extends GenericToolkit<infer TR, infer _E, infer _R> ? TR : never;
64
+ export type Failure<T extends Any> = T extends GenericToolkit<infer _TR, infer E, infer _R> ? E : never;
65
+ export type Requirements<T extends Any> = T extends GenericToolkit<infer _TR, infer _E, infer R> ? R : never;
66
+
67
+ /**
68
+ * Creates a portable toolkit from toolkit definition and handlers layer.
69
+ */
70
+ export const make = <Tools extends Record<string, Tool.Any>, E, R>(
71
+ toolkit: Toolkit.Toolkit<Tools>,
72
+ layer: Layer.Layer<Tool.HandlersFor<Tools>, E, R>,
73
+ ): GenericToolkit<E, R> => {
74
+ return {
75
+ [TypeId]: TypeId,
76
+ toolkit: toolkit as any,
77
+ layer: layer as any,
78
+ handlers: toolkit.pipe(Effect.provide(layer)) as any,
79
+ pipe() {
80
+ // eslint-disable-next-line prefer-rest-params
81
+ return Pipeable.pipeArguments(this, arguments);
82
+ },
83
+ };
84
+ };
85
+
86
+ /**
87
+ * Merges multiple portable toolkits into a single portable toolkit.
88
+ */
89
+ export const merge = <const Toolkits extends ReadonlyArray<Any>>(
90
+ /**
91
+ * The toolkits to merge together.
92
+ */
93
+ ...toolkits: Toolkits
94
+ ): GenericToolkit<
95
+ InvocationRequirements<Toolkits[number]>,
96
+ Failure<Toolkits[number]>,
97
+ Requirements<Toolkits[number]>
98
+ > => {
99
+ return make(
100
+ Toolkit.merge(...toolkits.map((t) => t.toolkit)),
101
+ Layer.mergeAll(...(toolkits.map((t) => t.layer) as any)),
102
+ ) as any;
103
+ };
104
+
105
+ /**
106
+ * Generic tools type where we cannot statically enumerate individual tools.
107
+ */
108
+ export type GenericTools<R = never> = Record<
109
+ string,
110
+ Tool.Tool<
111
+ string,
112
+ {
113
+ readonly parameters: AnyStructSchemaNoContext;
114
+ readonly success: Schema.Schema.AnyNoContext;
115
+ readonly failure: typeof Schema.Never;
116
+ },
117
+ R
118
+ >
119
+ >;
120
+
121
+ export interface AnyStructSchemaNoContext extends Pipeable.Pipeable {
122
+ readonly [Schema.TypeId]: any;
123
+ readonly make: any;
124
+ readonly Type: any;
125
+ readonly Encoded: any;
126
+ readonly Context: never;
127
+ readonly ast: SchemaAST.AST;
128
+ readonly fields: Schema.Struct.Fields;
129
+ readonly annotations: any;
130
+ }
@@ -9,7 +9,8 @@ import { Blueprint, Template } from '@dxos/blueprints';
9
9
  import { Obj } from '@dxos/echo';
10
10
  import { type EchoDatabase } from '@dxos/echo-db';
11
11
  import { EchoTestBuilder } from '@dxos/echo-db/testing';
12
- import { DataType } from '@dxos/schema';
12
+ import { Text } from '@dxos/schema';
13
+ import { Organization } from '@dxos/types';
13
14
  import { trim } from '@dxos/util';
14
15
 
15
16
  import { createSystemPrompt } from '../templates/system';
@@ -21,7 +22,7 @@ describe('format', () => {
21
22
  beforeAll(async () => {
22
23
  const builder = await new EchoTestBuilder().open();
23
24
  ({ db } = await builder.createDatabase({
24
- types: [DataType.Text, DataType.Organization, Blueprint.Blueprint],
25
+ types: [Text.Text, Organization.Organization, Blueprint.Blueprint],
25
26
  }));
26
27
  });
27
28
 
@@ -29,7 +30,7 @@ describe('format', () => {
29
30
  'should format',
30
31
  Effect.fn(function* () {
31
32
  const object = db.add(
32
- Obj.make(DataType.Organization, {
33
+ Obj.make(Organization.Organization, {
33
34
  name: 'Test',
34
35
  website: 'https://www.test.com',
35
36
  }),
@@ -10,10 +10,9 @@ import * as Option from 'effect/Option';
10
10
  import { Template } from '@dxos/blueprints';
11
11
  import { Obj } from '@dxos/echo';
12
12
  import { type ObjectId } from '@dxos/echo/internal';
13
- import { ObjectVersion } from '@dxos/echo-db';
14
- import { DatabaseService } from '@dxos/functions';
13
+ import { DatabaseService, ObjectVersion } from '@dxos/echo-db';
15
14
  import { log } from '@dxos/log';
16
- import { type ContentBlock, DataType } from '@dxos/schema';
15
+ import { type ContentBlock, Message } from '@dxos/types';
17
16
  import { trim } from '@dxos/util';
18
17
 
19
18
  import { AiAssistantError } from '../errors';
@@ -105,14 +104,14 @@ export const formatUserPrompt = ({ prompt, history = [] }: Pick<AiSessionRunPara
105
104
  }
106
105
  }
107
106
 
108
- return Obj.make(DataType.Message, {
107
+ return Obj.make(Message.Message, {
109
108
  created: new Date().toISOString(),
110
109
  sender: { role: 'user' },
111
110
  blocks: [...blocks, { _tag: 'text', text: prompt }],
112
111
  });
113
112
  });
114
113
 
115
- const gatherObjectVersions = (messages: DataType.Message[]): Map<ObjectId, ObjectVersion> => {
114
+ const gatherObjectVersions = (messages: Message.Message[]): Map<ObjectId, ObjectVersion> => {
116
115
  const artifactIds = new Map<ObjectId, ObjectVersion>();
117
116
  for (const message of messages) {
118
117
  for (const block of message.blocks) {
@@ -7,3 +7,5 @@ export * from './observer';
7
7
  export * from './session';
8
8
  export * from './toolkit';
9
9
  export * from './version-pin';
10
+
11
+ export * as GenericToolkit from './GenericToolkit';
@@ -6,7 +6,7 @@ import * as Effect from 'effect/Effect';
6
6
  import * as Function from 'effect/Function';
7
7
 
8
8
  import { type AiParser, type ConsolePrinter } from '@dxos/ai';
9
- import { type DataType } from '@dxos/schema';
9
+ import { type Message } from '@dxos/types';
10
10
 
11
11
  /**
12
12
  * Live observer of the generation process.
@@ -16,7 +16,7 @@ export interface GenerationObserver extends AiParser.ParseResponseCallbacks {
16
16
  * Complete messages fired during the session, both from the model and from the user.
17
17
  * This message will contain all message blocks emitted through the `onBlock` callback.
18
18
  */
19
- onMessage: (message: DataType.Message) => Effect.Effect<void>;
19
+ onMessage: (message: Message.Message) => Effect.Effect<void>;
20
20
  }
21
21
 
22
22
  export const GenerationObserver = Object.freeze({
@@ -33,7 +33,7 @@ const CalendarEventSchema = Schema.Struct({
33
33
 
34
34
  type CalendarEvent = Schema.Schema.Type<typeof CalendarEventSchema>;
35
35
 
36
- class TestToolkit extends Toolkit.make(
36
+ const TestToolkit = Toolkit.make(
37
37
  Tool.make('Calculator', {
38
38
  description: 'Basic calculator tool',
39
39
  parameters: {
@@ -46,7 +46,7 @@ class TestToolkit extends Toolkit.make(
46
46
  }),
47
47
  failure: Schema.Never,
48
48
  }),
49
- ) {}
49
+ );
50
50
 
51
51
  // Tool handlers.
52
52
  const toolkitLayer = TestToolkit.toLayer({