@dxos/assistant 0.8.4-main.84f28bd → 0.8.4-main.a4bbb77
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/dist/lib/browser/chunk-ILL2CN3G.mjs +374 -0
- package/dist/lib/browser/chunk-ILL2CN3G.mjs.map +7 -0
- package/dist/lib/browser/chunk-UB26N5UF.mjs +89 -0
- package/dist/lib/browser/chunk-UB26N5UF.mjs.map +7 -0
- package/dist/lib/browser/extraction/index.mjs +272 -0
- package/dist/lib/browser/extraction/index.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +712 -1870
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +13 -0
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +91 -0
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-GVKA7VL3.mjs +376 -0
- package/dist/lib/node-esm/chunk-GVKA7VL3.mjs.map +7 -0
- package/dist/lib/node-esm/extraction/index.mjs +273 -0
- package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +712 -1871
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +14 -0
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +96 -0
- package/dist/types/src/conversation/context.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.d.ts +28 -31
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/conversation/index.d.ts +1 -0
- package/dist/types/src/conversation/index.d.ts.map +1 -1
- package/dist/types/src/diff.d.ts +15 -0
- package/dist/types/src/diff.d.ts.map +1 -0
- package/dist/types/src/diff.test.d.ts +2 -0
- package/dist/types/src/diff.test.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +75 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/execution-graph.d.ts +82 -0
- package/dist/types/src/execution-graph.d.ts.map +1 -0
- package/dist/types/src/execution-graph.test.d.ts +2 -0
- package/dist/types/src/execution-graph.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +1 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +198 -65
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +1 -1
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +254 -162
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/functions/index.d.ts +2 -0
- package/dist/types/src/functions/index.d.ts.map +1 -0
- package/dist/types/src/functions/services.d.ts +18 -0
- package/dist/types/src/functions/services.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -5
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/artifact-diff.d.ts +27 -0
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
- package/dist/types/src/session/error-handling.d.ts +4 -0
- package/dist/types/src/session/error-handling.d.ts.map +1 -0
- package/dist/types/src/session/format.d.ts +184 -0
- package/dist/types/src/session/format.d.ts.map +1 -0
- package/dist/types/src/session/format.test.d.ts +2 -0
- package/dist/types/src/session/format.test.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +3 -0
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +22 -0
- package/dist/types/src/session/observer.d.ts.map +1 -0
- package/dist/types/src/session/session.d.ts +32 -268
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +14 -0
- package/dist/types/src/session/toolkit.d.ts.map +1 -0
- package/dist/types/src/session/version-pin.d.ts +4 -4
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/templates/index.d.ts +2 -0
- package/dist/types/src/templates/index.d.ts.map +1 -0
- package/dist/types/src/templates/system.d.ts +18 -0
- package/dist/types/src/templates/system.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +13 -0
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +50 -24
- package/src/conversation/context.ts +154 -0
- package/src/conversation/conversation.ts +89 -73
- package/src/conversation/index.ts +1 -0
- package/src/diff.test.ts +43 -0
- package/src/diff.ts +79 -0
- package/src/errors.ts +15 -0
- package/src/execution-graph.test.ts +738 -0
- package/src/execution-graph.ts +500 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +13 -24
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +27 -21
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +9 -9
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +17 -7
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +4 -3
- package/src/extraction/extraction-ner-function/named-entity-recognition.ts +3 -1
- package/src/extraction/quotes.test.ts +3 -3
- package/src/extraction/quotes.ts +2 -2
- package/src/functions/index.ts +5 -0
- package/src/functions/services.ts +153 -0
- package/src/index.ts +5 -5
- package/src/session/artifact-diff.ts +41 -0
- package/src/session/error-handling.ts +40 -0
- package/src/session/format.test.ts +60 -0
- package/src/session/format.ts +139 -0
- package/src/session/index.ts +3 -0
- package/src/session/observer.ts +58 -0
- package/src/session/session.conversations.json +1 -0
- package/src/session/session.test.ts +86 -215
- package/src/session/session.ts +163 -539
- package/src/session/toolkit.ts +46 -0
- package/src/session/version-pin.ts +9 -9
- package/src/summarization/summarization.test.ts +86 -106
- package/src/templates/format.tpl +60 -0
- package/src/templates/index.ts +5 -0
- package/src/templates/system.tpl +48 -0
- package/src/templates/system.ts +41 -0
- package/src/types.ts +45 -1
- package/src/typings.d.ts +5 -0
- package/dist/types/bin/chat.d.ts +0 -2
- package/dist/types/bin/chat.d.ts.map +0 -1
- package/dist/types/src/blueprint/blueprint.d.ts +0 -41
- package/dist/types/src/blueprint/blueprint.d.ts.map +0 -1
- package/dist/types/src/blueprint/index.d.ts +0 -3
- package/dist/types/src/blueprint/index.d.ts.map +0 -1
- package/dist/types/src/blueprint/registry.d.ts +0 -7
- package/dist/types/src/blueprint/registry.d.ts.map +0 -1
- package/dist/types/src/context/binder.d.ts +0 -34
- package/dist/types/src/context/binder.d.ts.map +0 -1
- package/dist/types/src/context/binding.d.ts +0 -38
- package/dist/types/src/context/binding.d.ts.map +0 -1
- package/dist/types/src/context/index.d.ts +0 -3
- package/dist/types/src/context/index.d.ts.map +0 -1
- package/dist/types/src/research/exa.d.ts +0 -7
- package/dist/types/src/research/exa.d.ts.map +0 -1
- package/dist/types/src/research/graph.d.ts +0 -41
- package/dist/types/src/research/graph.d.ts.map +0 -1
- package/dist/types/src/research/graph.test.d.ts +0 -2
- package/dist/types/src/research/graph.test.d.ts.map +0 -1
- package/dist/types/src/research/index.d.ts +0 -4
- package/dist/types/src/research/index.d.ts.map +0 -1
- package/dist/types/src/research/research.d.ts +0 -10
- package/dist/types/src/research/research.d.ts.map +0 -1
- package/dist/types/src/research/research.test.d.ts +0 -2
- package/dist/types/src/research/research.test.d.ts.map +0 -1
- package/dist/types/src/session/version-pin.test.d.ts +0 -2
- package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
- package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
- package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
- package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
- package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
- package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
- package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/src/blueprint/blueprint.ts +0 -75
- package/src/blueprint/index.ts +0 -6
- package/src/blueprint/registry.ts +0 -15
- package/src/context/binder.ts +0 -101
- package/src/context/binding.ts +0 -31
- package/src/context/index.ts +0 -6
- package/src/research/exa.ts +0 -100
- package/src/research/graph.test.ts +0 -15
- package/src/research/graph.ts +0 -337
- package/src/research/index.ts +0 -7
- package/src/research/instructions-research.tpl +0 -97
- package/src/research/research.test.ts +0 -190
- package/src/research/research.ts +0 -90
- package/src/session/version-pin.test.ts +0 -26
- package/src/testing/exa-search-1748337321991.ts +0 -131
- package/src/testing/exa-search-1748337331526.ts +0 -144
- package/src/testing/exa-search-1748337344119.ts +0 -133
- package/src/testing/index.ts +0 -11
- package/src/typedefs.d.ts +0 -8
|
@@ -2,19 +2,20 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { describe, test
|
|
5
|
+
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
9
|
import { FunctionExecutor, ServiceContainer } from '@dxos/functions';
|
|
10
10
|
import { log } from '@dxos/log';
|
|
11
11
|
import { DataType } from '@dxos/schema';
|
|
12
|
-
import {
|
|
12
|
+
import { Testing, createTestData } from '@dxos/schema/testing';
|
|
13
13
|
import { range } from '@dxos/util';
|
|
14
14
|
|
|
15
|
-
import { extractionNerFn } from './extraction-ner-function';
|
|
16
15
|
import { processTranscriptMessage } from '../extraction';
|
|
17
16
|
|
|
17
|
+
import { extractionNerFunction } from './extraction-ner-function';
|
|
18
|
+
|
|
18
19
|
describe.skip('NER EntityExtraction', () => {
|
|
19
20
|
let builder: EchoTestBuilder;
|
|
20
21
|
let db: EchoDatabase;
|
|
@@ -27,6 +28,7 @@ describe.skip('NER EntityExtraction', () => {
|
|
|
27
28
|
contacts: Record<string, DataType.Person>;
|
|
28
29
|
organizations: Record<string, DataType.Organization>;
|
|
29
30
|
};
|
|
31
|
+
|
|
30
32
|
const TYPES = [DataType.Organization, DataType.Person, Testing.Contact, Testing.DocumentType];
|
|
31
33
|
|
|
32
34
|
beforeAll(async () => {
|
|
@@ -44,8 +46,8 @@ describe.skip('NER EntityExtraction', () => {
|
|
|
44
46
|
contacts: Object.fromEntries(Object.entries(data.contacts).map(([key, value]) => [key, db.add(value)])),
|
|
45
47
|
organizations: Object.fromEntries(Object.entries(data.organizations).map(([key, value]) => [key, db.add(value)])),
|
|
46
48
|
};
|
|
47
|
-
await db.flush();
|
|
48
49
|
|
|
50
|
+
await db.flush();
|
|
49
51
|
executor = new FunctionExecutor(
|
|
50
52
|
new ServiceContainer().setServices({
|
|
51
53
|
database: { db },
|
|
@@ -64,7 +66,7 @@ describe.skip('NER EntityExtraction', () => {
|
|
|
64
66
|
message,
|
|
65
67
|
objects: [...documents, ...Object.values(contacts)],
|
|
66
68
|
},
|
|
67
|
-
function:
|
|
69
|
+
function: extractionNerFunction,
|
|
68
70
|
executor,
|
|
69
71
|
});
|
|
70
72
|
log.info('output', enhancedMessage);
|
|
@@ -73,7 +75,6 @@ describe.skip('NER EntityExtraction', () => {
|
|
|
73
75
|
|
|
74
76
|
test('computational irreducibility', async () => {
|
|
75
77
|
const { transcriptWoflram, documents, contacts } = testData;
|
|
76
|
-
|
|
77
78
|
log.info('context', { documents, contacts });
|
|
78
79
|
const message = transcriptWoflram[0];
|
|
79
80
|
log.info('input', message);
|
|
@@ -85,7 +86,7 @@ describe.skip('NER EntityExtraction', () => {
|
|
|
85
86
|
message,
|
|
86
87
|
objects: [...documents, ...Object.values(contacts)],
|
|
87
88
|
},
|
|
88
|
-
function:
|
|
89
|
+
function: extractionNerFunction,
|
|
89
90
|
executor,
|
|
90
91
|
});
|
|
91
92
|
log.info('output', { message: enhancedMessage.blocks[0], timeElapsed });
|
|
@@ -95,7 +96,6 @@ describe.skip('NER EntityExtraction', () => {
|
|
|
95
96
|
|
|
96
97
|
test('org and document linking', async () => {
|
|
97
98
|
const { transcriptJosiah, documents, contacts, organizations } = testData;
|
|
98
|
-
|
|
99
99
|
log.info('context', { contacts, organizations, documents });
|
|
100
100
|
|
|
101
101
|
for (const message of transcriptJosiah) {
|
|
@@ -106,7 +106,7 @@ describe.skip('NER EntityExtraction', () => {
|
|
|
106
106
|
message,
|
|
107
107
|
objects: [...documents, ...Object.values(contacts), ...Object.values(organizations)],
|
|
108
108
|
},
|
|
109
|
-
function:
|
|
109
|
+
function: extractionNerFunction,
|
|
110
110
|
executor,
|
|
111
111
|
});
|
|
112
112
|
log.info('output', enhancedMessage);
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
// ISSUE(burdon): defineFunction
|
|
6
|
+
// @ts-nocheck
|
|
7
|
+
|
|
5
8
|
import { asyncTimeout } from '@dxos/async';
|
|
6
9
|
import { create } from '@dxos/echo-schema';
|
|
7
10
|
import { DatabaseService, defineFunction } from '@dxos/functions';
|
|
@@ -9,12 +12,16 @@ import { invariant } from '@dxos/invariant';
|
|
|
9
12
|
import { log } from '@dxos/log';
|
|
10
13
|
import { DataType } from '@dxos/schema';
|
|
11
14
|
|
|
12
|
-
import { extractFullEntities } from './named-entity-recognition';
|
|
13
15
|
import { ExtractionInput, ExtractionOutput } from '../extraction';
|
|
14
16
|
import { findQuotes, insertReferences } from '../quotes';
|
|
15
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Named Entity Recognition model
|
|
20
|
+
*/
|
|
16
21
|
// TODO(mykola): Make it use vector index to query objects and not pass objects as input.
|
|
17
|
-
export const
|
|
22
|
+
export const extractionNerFunction = defineFunction({
|
|
23
|
+
key: 'dxos.org/function/extraction/extract-entities',
|
|
24
|
+
name: 'Extract Entities',
|
|
18
25
|
description: 'Extracts named entities from a text',
|
|
19
26
|
inputSchema: ExtractionInput,
|
|
20
27
|
outputSchema: ExtractionOutput,
|
|
@@ -22,30 +29,33 @@ export const extractionNerFn = defineFunction({
|
|
|
22
29
|
log.info('input', { message, options });
|
|
23
30
|
const startTime = performance.now();
|
|
24
31
|
const { db } = context.getService(DatabaseService);
|
|
32
|
+
const { extractFullEntities } = await import('./named-entity-recognition');
|
|
25
33
|
|
|
26
34
|
const entitiesPromise = Promise.all(
|
|
27
35
|
message.blocks.map(async (block) => {
|
|
28
|
-
invariant(block.
|
|
36
|
+
invariant(block._tag === 'transcript' || block._tag === 'text');
|
|
29
37
|
return extractFullEntities(block.text);
|
|
30
38
|
}),
|
|
31
39
|
).then((entities) => entities.flat());
|
|
32
40
|
const entities = options?.timeout ? await asyncTimeout(entitiesPromise, options.timeout) : await entitiesPromise;
|
|
41
|
+
|
|
33
42
|
log.info('entities', { entities });
|
|
34
43
|
const quoteReferences = await findQuotes(
|
|
35
44
|
entities.map((entity) => entity.word),
|
|
36
45
|
db,
|
|
37
46
|
);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
47
|
+
|
|
48
|
+
const blocksWithReferences = message.blocks.map((block) => {
|
|
49
|
+
invariant(block._tag === 'transcript' || block._tag === 'text');
|
|
50
|
+
return { ...block, text: insertReferences(block._tag, quoteReferences) };
|
|
41
51
|
});
|
|
52
|
+
|
|
42
53
|
const messageWithReferences = create(DataType.Message, {
|
|
43
54
|
...message,
|
|
44
55
|
blocks: blocksWithReferences,
|
|
45
56
|
});
|
|
46
57
|
|
|
47
58
|
log.info('output', { messageWithReferences });
|
|
48
|
-
|
|
49
59
|
return {
|
|
50
60
|
message: messageWithReferences,
|
|
51
61
|
timeElapsed: performance.now() - startTime,
|
|
@@ -10,7 +10,8 @@ import { createTestData } from '@dxos/schema/testing';
|
|
|
10
10
|
|
|
11
11
|
import { combineNerTokens, createTokenGroups, extractFullEntities, getNer } from './named-entity-recognition';
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
// TODO(mykola): This should not run on CI.
|
|
14
|
+
describe.skip('NamedEntityRecognition', () => {
|
|
14
15
|
let ner: TokenClassificationPipelineType;
|
|
15
16
|
|
|
16
17
|
beforeAll(async () => {
|
|
@@ -57,7 +58,7 @@ describe('NamedEntityRecognition', () => {
|
|
|
57
58
|
const { transcriptWoflram, transcriptJosiah } = await createTestData();
|
|
58
59
|
|
|
59
60
|
const blocks = [...transcriptWoflram, ...transcriptJosiah].flatMap((message) =>
|
|
60
|
-
message.blocks.flatMap((block) => (block.
|
|
61
|
+
message.blocks.flatMap((block) => (block._tag === 'transcript' ? [block.text] : [])),
|
|
61
62
|
);
|
|
62
63
|
|
|
63
64
|
for (const block of blocks) {
|
|
@@ -70,7 +71,7 @@ describe('NamedEntityRecognition', () => {
|
|
|
70
71
|
const { transcriptMessages } = await createTestData();
|
|
71
72
|
|
|
72
73
|
const blocks = transcriptMessages.flatMap((message) =>
|
|
73
|
-
message.blocks.flatMap((block) => (block.
|
|
74
|
+
message.blocks.flatMap((block) => (block._tag === 'transcript' ? [block.text] : [])),
|
|
74
75
|
);
|
|
75
76
|
|
|
76
77
|
for (const block of blocks) {
|
|
@@ -4,14 +4,15 @@
|
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
6
|
type TokenClassificationOutput,
|
|
7
|
+
type TokenClassificationPipelineType,
|
|
7
8
|
type TokenClassificationSingle,
|
|
8
9
|
pipeline,
|
|
9
|
-
type TokenClassificationPipelineType,
|
|
10
10
|
} from '@xenova/transformers';
|
|
11
11
|
|
|
12
12
|
import { log } from '@dxos/log';
|
|
13
13
|
|
|
14
14
|
let _ner: Promise<TokenClassificationPipelineType>;
|
|
15
|
+
|
|
15
16
|
/**
|
|
16
17
|
* Named Entity Recognition pipeline.
|
|
17
18
|
* Initializes the pipeline on first call.
|
|
@@ -24,6 +25,7 @@ export const getNer = () => {
|
|
|
24
25
|
return ner;
|
|
25
26
|
});
|
|
26
27
|
}
|
|
28
|
+
|
|
27
29
|
return _ner;
|
|
28
30
|
};
|
|
29
31
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { describe,
|
|
5
|
+
import { describe, expect, test } from 'vitest';
|
|
6
6
|
|
|
7
7
|
import { insertReferences } from './quotes';
|
|
8
8
|
|
|
@@ -13,7 +13,7 @@ describe('insertReferences', () => {
|
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
expect(insertReferences('This is a computational irreducibility test.', quotes)).toBe(
|
|
16
|
-
'This is a [computational irreducibility]
|
|
16
|
+
'This is a [computational irreducibility](dxn:echo:@:01JTG9JW11XGWJZ32AW8ET93D1) test.',
|
|
17
17
|
);
|
|
18
18
|
expect(
|
|
19
19
|
insertReferences(
|
|
@@ -21,7 +21,7 @@ describe('insertReferences', () => {
|
|
|
21
21
|
quotes,
|
|
22
22
|
),
|
|
23
23
|
).toBe(
|
|
24
|
-
"And what I'd like to talk today about is Steven Wolfram's concept of a [computational irreducibility]
|
|
24
|
+
"And what I'd like to talk today about is Steven Wolfram's concept of a [computational irreducibility](dxn:echo:@:01JTG9JW11XGWJZ32AW8ET93D1).",
|
|
25
25
|
);
|
|
26
26
|
});
|
|
27
27
|
});
|
package/src/extraction/quotes.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { Schema } from 'effect';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { Filter, Query } from '@dxos/echo';
|
|
8
8
|
import { type EchoDatabase } from '@dxos/echo-db';
|
|
9
9
|
import { ObjectId } from '@dxos/echo-schema';
|
|
10
10
|
import { DXN } from '@dxos/keys';
|
|
@@ -51,7 +51,7 @@ export const insertReferences = (text: string, quotes: ReferencedQuotes) => {
|
|
|
51
51
|
|
|
52
52
|
// Use a case-insensitive regular expression to replace the quote.
|
|
53
53
|
const regex = new RegExp(quote.quote.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi');
|
|
54
|
-
text = text.replace(regex, `[${quote.quote}]
|
|
54
|
+
text = text.replace(regex, `[${quote.quote}](${DXN.fromLocalObjectId(quote.id).toString()})`);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
return text;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Tool, type Toolkit } from '@effect/ai';
|
|
6
|
+
import { Context, Effect, Layer, Record, Schema } from 'effect';
|
|
7
|
+
|
|
8
|
+
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
9
|
+
import { todo } from '@dxos/debug';
|
|
10
|
+
import { Query } from '@dxos/echo';
|
|
11
|
+
import { DatabaseService, FunctionDefinition, FunctionInvocationService, FunctionType } from '@dxos/functions';
|
|
12
|
+
import { invariant } from '@dxos/invariant';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Constructs a `ToolResolverService` whose `resolve(id)` looks up tools in the following order:
|
|
16
|
+
* 1. Toolkit: return an existing tool from the provided `toolkit` if one is present under `id`.
|
|
17
|
+
* 2. Functions in DB: query `FunctionType` by `key=id`; if found, deserialize and project to a tool
|
|
18
|
+
* (propagating any `deployedFunctionId` discovered in the object's metadata).
|
|
19
|
+
* 3. Functions passed in: fall back to a matching `FunctionDefinition` from the `functions` array.
|
|
20
|
+
*
|
|
21
|
+
* If none of the above yield a match, the effect fails with `AiToolNotFoundError`.
|
|
22
|
+
*
|
|
23
|
+
* Requires `DatabaseService` in the environment.
|
|
24
|
+
*/
|
|
25
|
+
export const makeToolResolverFromFunctions = (
|
|
26
|
+
functions: FunctionDefinition<any, any>[],
|
|
27
|
+
toolkit: Toolkit.Toolkit<any>,
|
|
28
|
+
): Layer.Layer<ToolResolverService, never, DatabaseService> => {
|
|
29
|
+
return Layer.effect(
|
|
30
|
+
ToolResolverService,
|
|
31
|
+
Effect.gen(function* () {
|
|
32
|
+
const dbService = yield* DatabaseService;
|
|
33
|
+
return {
|
|
34
|
+
resolve: (id): Effect.Effect<Tool.Any, AiToolNotFoundError> =>
|
|
35
|
+
Effect.gen(function* () {
|
|
36
|
+
const tool = toolkit.tools[id];
|
|
37
|
+
if (tool) {
|
|
38
|
+
return tool;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const {
|
|
42
|
+
objects: [dbFunction],
|
|
43
|
+
} = yield* DatabaseService.runQuery(Query.type(FunctionType, { key: id }));
|
|
44
|
+
|
|
45
|
+
const functionDef = dbFunction
|
|
46
|
+
? FunctionDefinition.deserialize(dbFunction)
|
|
47
|
+
: functions.find((fn) => fn.key === id);
|
|
48
|
+
|
|
49
|
+
if (!functionDef) {
|
|
50
|
+
return yield* Effect.fail(new AiToolNotFoundError(id));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return projectFunctionToTool(functionDef);
|
|
54
|
+
}).pipe(Effect.provideService(DatabaseService, dbService)),
|
|
55
|
+
} satisfies Context.Tag.Service<ToolResolverService>;
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const makeToolExecutionServiceFromFunctions = (
|
|
61
|
+
toolkit: Toolkit.Toolkit<any>,
|
|
62
|
+
handlersLayer: Layer.Layer<Tool.Handler<any>, never, never>,
|
|
63
|
+
): Layer.Layer<ToolExecutionService, never, FunctionInvocationService> => {
|
|
64
|
+
return Layer.effect(
|
|
65
|
+
ToolExecutionService,
|
|
66
|
+
Effect.gen(function* () {
|
|
67
|
+
const toolkitHandler = yield* toolkit.pipe(Effect.provide(handlersLayer));
|
|
68
|
+
invariant(isHandlerLike(toolkitHandler));
|
|
69
|
+
const functionInvocationService = yield* FunctionInvocationService;
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
handlersFor: (toolkit) => {
|
|
73
|
+
const makeHandler = (tool: Tool.Any): ((params: unknown) => Effect.Effect<unknown, any, any>) => {
|
|
74
|
+
return Effect.fn(`toolFunctionHandler ${tool.name}`)(function* (input: any) {
|
|
75
|
+
if (toolkitHandler.tools[tool.name]) {
|
|
76
|
+
if (Tool.isProviderDefined(tool)) {
|
|
77
|
+
throw new Error('Attempted to call a provider-defined tool');
|
|
78
|
+
}
|
|
79
|
+
// TODO(wittjosiah): Everything is `never` here.
|
|
80
|
+
return yield* (toolkitHandler.handle as any)(tool.name, input);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const { definition: functionDef } = Context.get(FunctionToolAnnotation)(tool.annotations as any);
|
|
84
|
+
|
|
85
|
+
return yield* functionInvocationService
|
|
86
|
+
.invokeFunction(functionDef, input as any)
|
|
87
|
+
.pipe(Effect.catchAllDefect((defect) => Effect.fail(defect)));
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
return toolkit.of(
|
|
92
|
+
Record.map(toolkit.tools, (tool, _name) => (Tool.isUserDefined(tool) ? makeHandler(tool) : null)) as any,
|
|
93
|
+
) as any;
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
class FunctionToolAnnotation extends Context.Tag('@dxos/assistant/FunctionToolAnnotation')<
|
|
101
|
+
FunctionToolAnnotation,
|
|
102
|
+
{ definition: FunctionDefinition<any, any> }
|
|
103
|
+
>() {}
|
|
104
|
+
|
|
105
|
+
const toolCache = new WeakMap<FunctionDefinition<any, any>, Tool.Any>();
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Projects a `FunctionDefinition` into an `AiTool`.
|
|
109
|
+
*
|
|
110
|
+
* @param fn The function definition to project into a tool.
|
|
111
|
+
* @param meta Optional projection metadata.
|
|
112
|
+
* @param meta.deployedFunctionId Backend deployment ID used for remote invocation when present. This is the
|
|
113
|
+
* EDGE service's function deployment identifier (not the ECHO object ID/DXN and not `FunctionDefinition.key`).
|
|
114
|
+
*/
|
|
115
|
+
const projectFunctionToTool = (fn: FunctionDefinition<any, any>): Tool.Any => {
|
|
116
|
+
if (toolCache.has(fn)) {
|
|
117
|
+
return toolCache.get(fn)!;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const tool = Tool.make(makeToolName(fn.name), {
|
|
121
|
+
description: fn.description,
|
|
122
|
+
parameters: createStructFieldsFromSchema(fn.inputSchema),
|
|
123
|
+
// TODO(dmaretskyi): Include output schema.
|
|
124
|
+
failure: Schema.Any, // TODO(dmaretskyi): Better type for the failure?
|
|
125
|
+
}).annotate(FunctionToolAnnotation, { definition: fn });
|
|
126
|
+
toolCache.set(fn, tool);
|
|
127
|
+
return tool;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @returns Tool name produced from function name by escaping invalid characters.
|
|
132
|
+
*/
|
|
133
|
+
const makeToolName = (name: string) => {
|
|
134
|
+
const toolName = name.replace(/[^a-zA-Z0-9]/g, '_');
|
|
135
|
+
invariant(toolName.match(/^[a-zA-Z_][a-zA-Z0-9-_]*$/));
|
|
136
|
+
return toolName;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// TODO(dmaretskyi): Factor out.
|
|
140
|
+
const createStructFieldsFromSchema = (schema: Schema.Schema<any, any>): Record<string, Schema.Schema<any, any>> => {
|
|
141
|
+
switch (schema.ast._tag) {
|
|
142
|
+
case 'TypeLiteral':
|
|
143
|
+
return Object.fromEntries(schema.ast.propertySignatures.map((prop) => [prop.name, Schema.make(prop.type)]));
|
|
144
|
+
case 'VoidKeyword':
|
|
145
|
+
return {};
|
|
146
|
+
default:
|
|
147
|
+
return todo(`Unsupported schema AST: ${schema.ast._tag}`);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const isHandlerLike = (value: unknown): value is Toolkit.WithHandler<Record<string, Tool.Any>> => {
|
|
152
|
+
return typeof (value as any).tools === 'object' && typeof (value as any).handle === 'function';
|
|
153
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export * from './blueprint';
|
|
6
|
-
export * from './context';
|
|
7
5
|
export * from './conversation';
|
|
8
|
-
export * from './
|
|
9
|
-
export * from './
|
|
6
|
+
export * from './diff';
|
|
7
|
+
export * from './errors';
|
|
8
|
+
export * from './execution-graph';
|
|
9
|
+
export * from './functions';
|
|
10
10
|
export * from './session';
|
|
11
|
+
export * from './templates';
|
|
11
12
|
export * from './types';
|
|
12
|
-
export * from './conversation';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Context } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { type ObjectVersion } from '@dxos/echo-db';
|
|
8
|
+
import { type ObjectId } from '@dxos/echo-schema';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Resolves artifact ids to their versions.
|
|
12
|
+
* Used to give the model a sense of the changes to the artifacts made by users during the conversation.
|
|
13
|
+
* The artifacts versions are pinned in the history, and whenever the artifact changes in-between assistant's steps,
|
|
14
|
+
* a diff is inserted into the conversation.
|
|
15
|
+
*
|
|
16
|
+
* Can be optionally provided to the session run call.
|
|
17
|
+
*/
|
|
18
|
+
// TODO(dmaretskyi): Convert to Context.Reference
|
|
19
|
+
export class ArtifactDiffResolver extends Context.Tag('@dxos/assistant/ArtifactDiffResolver')<
|
|
20
|
+
ArtifactDiffResolver,
|
|
21
|
+
ArtifactDiffResolver.Service
|
|
22
|
+
>() {}
|
|
23
|
+
|
|
24
|
+
export namespace ArtifactDiffResolver {
|
|
25
|
+
export type Service = {
|
|
26
|
+
resolve: (
|
|
27
|
+
artifacts: {
|
|
28
|
+
id: ObjectId;
|
|
29
|
+
lastVersion: ObjectVersion;
|
|
30
|
+
}[],
|
|
31
|
+
) => Promise<
|
|
32
|
+
Map<
|
|
33
|
+
ObjectId,
|
|
34
|
+
{
|
|
35
|
+
version: ObjectVersion;
|
|
36
|
+
diff?: string;
|
|
37
|
+
}
|
|
38
|
+
>
|
|
39
|
+
>;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { AiError } from '@effect/ai';
|
|
6
|
+
import { HttpClientError } from '@effect/platform';
|
|
7
|
+
import { Effect, Schema } from 'effect';
|
|
8
|
+
|
|
9
|
+
import { AiModelError } from '../errors';
|
|
10
|
+
|
|
11
|
+
const AnthropicErrorResponse = Schema.Struct({
|
|
12
|
+
type: Schema.Literal('error'),
|
|
13
|
+
error: Schema.Struct({
|
|
14
|
+
type: Schema.String,
|
|
15
|
+
message: Schema.String,
|
|
16
|
+
}),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// TODO(dmaretskyi): Needs rework
|
|
20
|
+
export const mapAiError = (err: AiError.AiError): Effect.Effect<AiError.AiError> =>
|
|
21
|
+
Effect.gen(function* () {
|
|
22
|
+
const cause = err.cause;
|
|
23
|
+
if (HttpClientError.isHttpClientError(cause) && cause.reason === 'StatusCode') {
|
|
24
|
+
const body = yield* cause.response.json.pipe(
|
|
25
|
+
Effect.flatMap(Schema.decodeUnknown(AnthropicErrorResponse, { exact: false })),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const parsedCause = new AiModelError({
|
|
29
|
+
message: body.error.message,
|
|
30
|
+
context: { model: 'anthropic', type: body.error.type },
|
|
31
|
+
});
|
|
32
|
+
return AiError.UnknownError.make({
|
|
33
|
+
description: body.error.message,
|
|
34
|
+
module: err.module,
|
|
35
|
+
method: err.method,
|
|
36
|
+
cause: parsedCause,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return err;
|
|
40
|
+
}).pipe(Effect.catchAll(() => Effect.succeed(err)));
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { beforeAll, describe, it } from '@effect/vitest';
|
|
6
|
+
import { Effect } from 'effect';
|
|
7
|
+
|
|
8
|
+
import { Blueprint, Template } from '@dxos/blueprints';
|
|
9
|
+
import { Obj } from '@dxos/echo';
|
|
10
|
+
import { type EchoDatabase } from '@dxos/echo-db';
|
|
11
|
+
import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
12
|
+
import { DataType } from '@dxos/schema';
|
|
13
|
+
import { trim } from '@dxos/util';
|
|
14
|
+
|
|
15
|
+
import { createSystemPrompt } from '../templates/system';
|
|
16
|
+
|
|
17
|
+
import { formatSystemPrompt } from './format';
|
|
18
|
+
|
|
19
|
+
describe('format', () => {
|
|
20
|
+
let db: EchoDatabase;
|
|
21
|
+
beforeAll(async () => {
|
|
22
|
+
const builder = await new EchoTestBuilder().open();
|
|
23
|
+
({ db } = await builder.createDatabase({
|
|
24
|
+
types: [DataType.Text, DataType.Organization, Blueprint.Blueprint],
|
|
25
|
+
}));
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it.effect(
|
|
29
|
+
'should format',
|
|
30
|
+
Effect.fn(function* () {
|
|
31
|
+
const object = db.add(
|
|
32
|
+
Obj.make(DataType.Organization, {
|
|
33
|
+
name: 'Test',
|
|
34
|
+
website: 'https://www.test.com',
|
|
35
|
+
}),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const blueprint = db.add(
|
|
39
|
+
Blueprint.make({
|
|
40
|
+
key: 'example.com/blueprint/test',
|
|
41
|
+
name: 'Test',
|
|
42
|
+
instructions: Template.make({
|
|
43
|
+
source: trim`
|
|
44
|
+
Test
|
|
45
|
+
This is the test blueprint.
|
|
46
|
+
`,
|
|
47
|
+
}),
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const output = yield* formatSystemPrompt({
|
|
52
|
+
system: createSystemPrompt({}),
|
|
53
|
+
blueprints: [blueprint, blueprint],
|
|
54
|
+
objects: [object, object],
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
console.log(output);
|
|
58
|
+
}),
|
|
59
|
+
);
|
|
60
|
+
});
|