@dxos/assistant 0.8.4-main.fd6878d → 0.8.4-staging.60fe92afc8
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/LICENSE +102 -5
- package/README.md +2 -1
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/neutral/extraction/index.mjs +208 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1886 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts +0 -1
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +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 +1650 -281
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/index.d.ts +0 -1
- package/dist/types/src/extraction/index.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +3 -3
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +16 -5
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/request/AiRequest.d.ts +308 -0
- package/dist/types/src/request/AiRequest.d.ts.map +1 -0
- package/dist/types/src/{session → request}/artifact-diff.d.ts +5 -5
- package/dist/types/src/request/artifact-diff.d.ts.map +1 -0
- package/dist/types/src/request/format.d.ts +15 -0
- package/dist/types/src/request/format.d.ts.map +1 -0
- package/dist/types/src/request/index.d.ts +4 -0
- package/dist/types/src/request/index.d.ts.map +1 -0
- package/dist/types/src/request/observer.d.ts +23 -0
- package/dist/types/src/request/observer.d.ts.map +1 -0
- package/dist/types/src/{session → request}/version-pin.d.ts +6 -6
- package/dist/types/src/request/version-pin.d.ts.map +1 -0
- package/dist/types/src/session/AiContext.d.ts +178 -0
- package/dist/types/src/session/AiContext.d.ts.map +1 -0
- package/dist/types/src/session/AiContext.test.d.ts +2 -0
- package/dist/types/src/session/AiContext.test.d.ts.map +1 -0
- package/dist/types/src/session/AiSession.d.ts +79 -0
- package/dist/types/src/session/AiSession.d.ts.map +1 -0
- package/dist/types/src/session/AiSession.test.d.ts +2 -0
- package/dist/types/src/session/AiSession.test.d.ts.map +1 -0
- package/dist/types/src/session/SessionLink.d.ts +17 -0
- package/dist/types/src/session/SessionLink.d.ts.map +1 -0
- package/dist/types/src/session/SessionLoader.d.ts +15 -0
- package/dist/types/src/session/SessionLoader.d.ts.map +1 -0
- package/dist/types/src/session/SessionLoader.test.d.ts +2 -0
- package/dist/types/src/session/SessionLoader.test.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +5 -3
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +16 -0
- package/dist/types/src/session/toolkit.d.ts.map +1 -0
- package/dist/types/src/templates/system.d.ts +19 -14
- package/dist/types/src/templates/system.d.ts.map +1 -1
- package/dist/types/src/util/artifact.d.ts +18 -0
- package/dist/types/src/util/artifact.d.ts.map +1 -0
- package/dist/types/src/util/diff.d.ts +17 -0
- package/dist/types/src/util/diff.d.ts.map +1 -0
- package/dist/types/src/util/diff.test.d.ts +2 -0
- package/dist/types/src/util/diff.test.d.ts.map +1 -0
- package/dist/types/src/util/error-handling.d.ts +4 -0
- package/dist/types/src/util/error-handling.d.ts.map +1 -0
- package/dist/types/src/util/errors.d.ts +67 -0
- package/dist/types/src/util/errors.d.ts.map +1 -0
- package/dist/types/src/util/execution-graph.d.ts +85 -0
- package/dist/types/src/util/execution-graph.d.ts.map +1 -0
- package/dist/types/src/util/execution-graph.test.d.ts +2 -0
- package/dist/types/src/util/execution-graph.test.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +7 -0
- package/dist/types/src/util/index.d.ts.map +1 -0
- package/dist/types/src/util/tracing.d.ts +296 -0
- package/dist/types/src/util/tracing.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +37 -47
- package/src/extraction/extraction-ner-function/index.ts +0 -1
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
- package/src/extraction/extraction-ner-function/named-entity-recognition.ts +2 -0
- package/src/extraction/extraction.ts +49 -19
- package/src/extraction/index.ts +0 -1
- package/src/extraction/quotes.test.ts +2 -2
- package/src/extraction/quotes.ts +7 -9
- package/src/functions/services.ts +180 -66
- package/src/index.ts +2 -3
- package/src/request/AiRequest.ts +387 -0
- package/src/{session → request}/artifact-diff.ts +10 -6
- package/src/{session → request}/format.ts +45 -37
- package/src/request/index.ts +8 -0
- package/src/request/observer.ts +62 -0
- package/src/{session → request}/version-pin.ts +8 -8
- package/src/session/AiContext.test.ts +49 -0
- package/src/session/AiContext.ts +420 -0
- package/src/session/AiSession.test.ts +111 -0
- package/src/session/AiSession.ts +328 -0
- package/src/session/SessionLink.ts +23 -0
- package/src/session/SessionLoader.test.ts +115 -0
- package/src/session/SessionLoader.ts +55 -0
- package/src/session/index.ts +6 -3
- package/src/session/toolkit.ts +47 -0
- package/src/templates/{format.tpl → instructions/format.tpl} +6 -4
- package/src/templates/instructions/system.tpl +96 -0
- package/src/templates/system.ts +26 -16
- package/src/util/artifact.ts +96 -0
- package/src/util/diff.test.ts +74 -0
- package/src/util/diff.ts +93 -0
- package/src/util/error-handling.ts +42 -0
- package/src/util/errors.ts +15 -0
- package/src/util/execution-graph.test.ts +740 -0
- package/src/util/execution-graph.ts +520 -0
- package/src/util/index.ts +10 -0
- package/src/util/tracing.ts +60 -0
- package/dist/lib/browser/chunk-6WRGDEMH.mjs +0 -89
- package/dist/lib/browser/chunk-6WRGDEMH.mjs.map +0 -7
- package/dist/lib/browser/chunk-NZGGCEF3.mjs +0 -341
- package/dist/lib/browser/chunk-NZGGCEF3.mjs.map +0 -7
- package/dist/lib/browser/extraction/index.mjs +0 -269
- package/dist/lib/browser/extraction/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -354
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/named-entity-recognition-O46UTCLR.mjs +0 -13
- package/dist/lib/node-esm/chunk-3UU5V4FS.mjs +0 -91
- package/dist/lib/node-esm/chunk-3UU5V4FS.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-XOOPGSGW.mjs +0 -343
- package/dist/lib/node-esm/chunk-XOOPGSGW.mjs.map +0 -7
- package/dist/lib/node-esm/extraction/index.mjs +0 -270
- package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -355
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/named-entity-recognition-W4VMRCH6.mjs +0 -14
- package/dist/lib/node-esm/named-entity-recognition-W4VMRCH6.mjs.map +0 -7
- package/dist/types/src/conversation/context.d.ts +0 -90
- package/dist/types/src/conversation/context.d.ts.map +0 -1
- package/dist/types/src/conversation/conversation.d.ts +0 -50
- package/dist/types/src/conversation/conversation.d.ts.map +0 -1
- package/dist/types/src/conversation/index.d.ts +0 -3
- package/dist/types/src/conversation/index.d.ts.map +0 -1
- package/dist/types/src/errors.d.ts +0 -39
- package/dist/types/src/errors.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +0 -4
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +0 -2
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts +0 -2
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +0 -306
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +0 -2
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +0 -1
- package/dist/types/src/session/artifact-diff.d.ts.map +0 -1
- package/dist/types/src/session/format.d.ts +0 -168
- package/dist/types/src/session/format.d.ts.map +0 -1
- package/dist/types/src/session/format.test.d.ts +0 -2
- package/dist/types/src/session/format.test.d.ts.map +0 -1
- package/dist/types/src/session/session.d.ts +0 -167
- package/dist/types/src/session/session.d.ts.map +0 -1
- package/dist/types/src/session/session.test.d.ts +0 -2
- package/dist/types/src/session/session.test.d.ts.map +0 -1
- package/dist/types/src/session/version-pin.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/summarization/summarization.test.d.ts +0 -2
- package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -16
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/conversation/context.ts +0 -133
- package/src/conversation/conversation.ts +0 -125
- package/src/conversation/index.ts +0 -6
- package/src/errors.ts +0 -10
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -84
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -71
- package/src/extraction/extraction-llm-function/index.ts +0 -5
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +0 -116
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +0 -57
- package/src/session/__snapshots__/session.test.ts.snap +0 -32
- package/src/session/format.test.ts +0 -60
- package/src/session/session.test.ts +0 -305
- package/src/session/session.ts +0 -279
- package/src/session/version-pin.test.ts +0 -7
- package/src/summarization/summarization.test.ts +0 -197
- package/src/templates/system.tpl +0 -47
- package/src/types.ts +0 -52
- /package/dist/lib/{browser/named-entity-recognition-O46UTCLR.mjs.map → neutral/chunk-J5LGTIGS.mjs.map} +0 -0
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { beforeAll, describe, test } from 'vitest';
|
|
6
|
-
|
|
7
|
-
import { type EchoDatabase } from '@dxos/echo-db';
|
|
8
|
-
import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
9
|
-
import { FunctionExecutor, ServiceContainer } from '@dxos/functions';
|
|
10
|
-
import { log } from '@dxos/log';
|
|
11
|
-
import { DataType } from '@dxos/schema';
|
|
12
|
-
import { Testing, createTestData } from '@dxos/schema/testing';
|
|
13
|
-
import { range } from '@dxos/util';
|
|
14
|
-
|
|
15
|
-
import { processTranscriptMessage } from '../extraction';
|
|
16
|
-
|
|
17
|
-
import { extractionNerFn } from './extraction-ner-function';
|
|
18
|
-
|
|
19
|
-
describe.skip('NER EntityExtraction', () => {
|
|
20
|
-
let builder: EchoTestBuilder;
|
|
21
|
-
let db: EchoDatabase;
|
|
22
|
-
let executor: FunctionExecutor;
|
|
23
|
-
let testData: {
|
|
24
|
-
transcriptJosiah: DataType.Message[];
|
|
25
|
-
transcriptWoflram: DataType.Message[];
|
|
26
|
-
transcriptMessages: DataType.Message[];
|
|
27
|
-
documents: Testing.DocumentType[];
|
|
28
|
-
contacts: Record<string, DataType.Person>;
|
|
29
|
-
organizations: Record<string, DataType.Organization>;
|
|
30
|
-
};
|
|
31
|
-
const TYPES = [DataType.Organization, DataType.Person, Testing.Contact, Testing.DocumentType];
|
|
32
|
-
|
|
33
|
-
beforeAll(async () => {
|
|
34
|
-
// TODO(dmaretskyi): Helper to scaffold this from a config.
|
|
35
|
-
builder = await new EchoTestBuilder().open();
|
|
36
|
-
const { db: db1 } = await builder.createDatabase({ indexing: { vector: true } });
|
|
37
|
-
db = db1;
|
|
38
|
-
db.graph.schemaRegistry.addSchema(TYPES);
|
|
39
|
-
const data = createTestData();
|
|
40
|
-
testData = {
|
|
41
|
-
transcriptJosiah: data.transcriptJosiah,
|
|
42
|
-
transcriptWoflram: data.transcriptWoflram,
|
|
43
|
-
transcriptMessages: data.transcriptMessages,
|
|
44
|
-
documents: data.documents.map((document) => db.add(document)),
|
|
45
|
-
contacts: Object.fromEntries(Object.entries(data.contacts).map(([key, value]) => [key, db.add(value)])),
|
|
46
|
-
organizations: Object.fromEntries(Object.entries(data.organizations).map(([key, value]) => [key, db.add(value)])),
|
|
47
|
-
};
|
|
48
|
-
await db.flush();
|
|
49
|
-
|
|
50
|
-
executor = new FunctionExecutor(
|
|
51
|
-
new ServiceContainer().setServices({
|
|
52
|
-
database: { db },
|
|
53
|
-
}),
|
|
54
|
-
);
|
|
55
|
-
}, 30_000);
|
|
56
|
-
|
|
57
|
-
test('should process a transcript block', async () => {
|
|
58
|
-
const { transcriptMessages, documents, contacts } = testData;
|
|
59
|
-
log.info('context', { documents, contacts });
|
|
60
|
-
|
|
61
|
-
for (const message of transcriptMessages) {
|
|
62
|
-
log.info('input', message);
|
|
63
|
-
const { message: enhancedMessage } = await processTranscriptMessage({
|
|
64
|
-
input: {
|
|
65
|
-
message,
|
|
66
|
-
objects: [...documents, ...Object.values(contacts)],
|
|
67
|
-
},
|
|
68
|
-
function: extractionNerFn,
|
|
69
|
-
executor,
|
|
70
|
-
});
|
|
71
|
-
log.info('output', enhancedMessage);
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('computational irreducibility', async () => {
|
|
76
|
-
const { transcriptWoflram, documents, contacts } = testData;
|
|
77
|
-
|
|
78
|
-
log.info('context', { documents, contacts });
|
|
79
|
-
const message = transcriptWoflram[0];
|
|
80
|
-
log.info('input', message);
|
|
81
|
-
|
|
82
|
-
await Promise.all(
|
|
83
|
-
range(10).map(async () => {
|
|
84
|
-
const { message: enhancedMessage, timeElapsed } = await processTranscriptMessage({
|
|
85
|
-
input: {
|
|
86
|
-
message,
|
|
87
|
-
objects: [...documents, ...Object.values(contacts)],
|
|
88
|
-
},
|
|
89
|
-
function: extractionNerFn,
|
|
90
|
-
executor,
|
|
91
|
-
});
|
|
92
|
-
log.info('output', { message: enhancedMessage.blocks[0], timeElapsed });
|
|
93
|
-
}),
|
|
94
|
-
);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test('org and document linking', async () => {
|
|
98
|
-
const { transcriptJosiah, documents, contacts, organizations } = testData;
|
|
99
|
-
|
|
100
|
-
log.info('context', { contacts, organizations, documents });
|
|
101
|
-
|
|
102
|
-
for (const message of transcriptJosiah) {
|
|
103
|
-
log.info('input', message);
|
|
104
|
-
|
|
105
|
-
const { message: enhancedMessage } = await processTranscriptMessage({
|
|
106
|
-
input: {
|
|
107
|
-
message,
|
|
108
|
-
objects: [...documents, ...Object.values(contacts), ...Object.values(organizations)],
|
|
109
|
-
},
|
|
110
|
-
function: extractionNerFn,
|
|
111
|
-
executor,
|
|
112
|
-
});
|
|
113
|
-
log.info('output', enhancedMessage);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
});
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
// ISSUE(burdon): defineFunction
|
|
6
|
-
// @ts-nocheck
|
|
7
|
-
|
|
8
|
-
import { asyncTimeout } from '@dxos/async';
|
|
9
|
-
import { create } from '@dxos/echo-schema';
|
|
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';
|
|
14
|
-
|
|
15
|
-
import { ExtractionInput, ExtractionOutput } from '../extraction';
|
|
16
|
-
import { findQuotes, insertReferences } from '../quotes';
|
|
17
|
-
|
|
18
|
-
// TODO(mykola): Make it use vector index to query objects and not pass objects as input.
|
|
19
|
-
export const extractionNerFn = defineFunction({
|
|
20
|
-
description: 'Extracts named entities from a text',
|
|
21
|
-
inputSchema: ExtractionInput,
|
|
22
|
-
outputSchema: ExtractionOutput,
|
|
23
|
-
handler: async ({ data: { message, options }, context }) => {
|
|
24
|
-
log.info('input', { message, options });
|
|
25
|
-
const startTime = performance.now();
|
|
26
|
-
const { db } = context.getService(DatabaseService);
|
|
27
|
-
const { extractFullEntities } = await import('./named-entity-recognition');
|
|
28
|
-
|
|
29
|
-
const entitiesPromise = Promise.all(
|
|
30
|
-
message.blocks.map(async (block) => {
|
|
31
|
-
invariant(block._tag === 'transcript' || block._tag === 'text', 'Block must have text');
|
|
32
|
-
return extractFullEntities(block.text);
|
|
33
|
-
}),
|
|
34
|
-
).then((entities) => entities.flat());
|
|
35
|
-
const entities = options?.timeout ? await asyncTimeout(entitiesPromise, options.timeout) : await entitiesPromise;
|
|
36
|
-
log.info('entities', { entities });
|
|
37
|
-
const quoteReferences = await findQuotes(
|
|
38
|
-
entities.map((entity) => entity.word),
|
|
39
|
-
db,
|
|
40
|
-
);
|
|
41
|
-
const blocksWithReferences = message.blocks.map((block, i) => {
|
|
42
|
-
invariant(block._tag === 'transcript' || block._tag === 'text', 'Block must have text');
|
|
43
|
-
return { ...block, text: insertReferences(block._tag, quoteReferences) };
|
|
44
|
-
});
|
|
45
|
-
const messageWithReferences = create(DataType.Message, {
|
|
46
|
-
...message,
|
|
47
|
-
blocks: blocksWithReferences,
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
log.info('output', { messageWithReferences });
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
message: messageWithReferences,
|
|
54
|
-
timeElapsed: performance.now() - startTime,
|
|
55
|
-
};
|
|
56
|
-
},
|
|
57
|
-
});
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
-
|
|
3
|
-
exports[`AiSession > no tools 1`] = `
|
|
4
|
-
[
|
|
5
|
-
{
|
|
6
|
-
"blocks": ReactiveArray [
|
|
7
|
-
{
|
|
8
|
-
"_tag": "text",
|
|
9
|
-
"text": "Hello world!",
|
|
10
|
-
},
|
|
11
|
-
],
|
|
12
|
-
"created": "Sun, 20 Jul 2025 10:54:08 GMT",
|
|
13
|
-
"id": "01K0KQGFTFV3GBKGEWZW67K8HK",
|
|
14
|
-
"sender": {
|
|
15
|
-
"role": "user",
|
|
16
|
-
},
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
"blocks": ReactiveArray [
|
|
20
|
-
{
|
|
21
|
-
"_tag": "text",
|
|
22
|
-
"text": "Hi there! How can I help you today?",
|
|
23
|
-
},
|
|
24
|
-
],
|
|
25
|
-
"created": "2025-07-20T10:54:10.801Z",
|
|
26
|
-
"id": "01K0KQGHVHJ37D4YG45WG090WV",
|
|
27
|
-
"sender": {
|
|
28
|
-
"role": "assistant",
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
]
|
|
32
|
-
`;
|
|
@@ -1,60 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { AiTool, AiToolkit } from '@effect/ai';
|
|
6
|
-
import { describe, it } from '@effect/vitest';
|
|
7
|
-
import { Effect, Layer, Schema } from 'effect';
|
|
8
|
-
|
|
9
|
-
import { AiService, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
10
|
-
import { AiServiceTestingPreset } from '@dxos/ai/testing';
|
|
11
|
-
import { Obj, Type } from '@dxos/echo';
|
|
12
|
-
import { log } from '@dxos/log';
|
|
13
|
-
|
|
14
|
-
import { AiSession } from './session';
|
|
15
|
-
|
|
16
|
-
// Define a calendar event artifact schema.
|
|
17
|
-
const CalendarEventSchema = Schema.Struct({
|
|
18
|
-
title: Schema.String,
|
|
19
|
-
startTime: Schema.String,
|
|
20
|
-
endTime: Schema.String,
|
|
21
|
-
description: Schema.String,
|
|
22
|
-
}).pipe(
|
|
23
|
-
Type.Obj({
|
|
24
|
-
typename: 'example.com/type/CalendarEvent',
|
|
25
|
-
version: '0.1.0',
|
|
26
|
-
}),
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
type CalendarEvent = Schema.Schema.Type<typeof CalendarEventSchema>;
|
|
30
|
-
|
|
31
|
-
class TestToolkit extends AiToolkit.make(
|
|
32
|
-
AiTool.make('Calculator', {
|
|
33
|
-
description: 'Basic calculator tool',
|
|
34
|
-
parameters: {
|
|
35
|
-
input: Schema.String.annotations({
|
|
36
|
-
description: 'The calculation to perform.',
|
|
37
|
-
}),
|
|
38
|
-
},
|
|
39
|
-
success: Schema.Struct({
|
|
40
|
-
result: Schema.Number,
|
|
41
|
-
}),
|
|
42
|
-
failure: Schema.Never,
|
|
43
|
-
}),
|
|
44
|
-
) {}
|
|
45
|
-
|
|
46
|
-
// Tool handlers.
|
|
47
|
-
const toolkitLayer = TestToolkit.toLayer({
|
|
48
|
-
Calculator: ({ input }) =>
|
|
49
|
-
Effect.gen(function* () {
|
|
50
|
-
const result = (() => {
|
|
51
|
-
// Restrict to basic arithmetic operations for safety.
|
|
52
|
-
const sanitizedInput = input.replace(/[^0-9+\-*/().\s]/g, '');
|
|
53
|
-
log.info('calculate', { sanitizedInput });
|
|
54
|
-
|
|
55
|
-
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
56
|
-
return Function(`"use strict"; return (${sanitizedInput})`)();
|
|
57
|
-
})();
|
|
58
|
-
|
|
59
|
-
return { result };
|
|
60
|
-
}),
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
describe.runIf(process.env.DX_RUN_SLOW_TESTS)('AiSession', () => {
|
|
64
|
-
it.effect(
|
|
65
|
-
'no tools',
|
|
66
|
-
Effect.fn(
|
|
67
|
-
function* ({ expect: _ }) {
|
|
68
|
-
const session = new AiSession({ operationModel: 'configured' });
|
|
69
|
-
const response = yield* session.run({
|
|
70
|
-
prompt: 'Hello world!',
|
|
71
|
-
history: [],
|
|
72
|
-
});
|
|
73
|
-
log.info('response', { response });
|
|
74
|
-
},
|
|
75
|
-
Effect.provide(
|
|
76
|
-
AiService.model('@anthropic/claude-3-5-sonnet-20241022').pipe(
|
|
77
|
-
Layer.provideMerge(ToolResolverService.layerEmpty),
|
|
78
|
-
Layer.provideMerge(ToolExecutionService.layerEmpty),
|
|
79
|
-
Layer.provideMerge(AiServiceTestingPreset('direct')),
|
|
80
|
-
),
|
|
81
|
-
),
|
|
82
|
-
),
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
it.effect(
|
|
86
|
-
'calculator',
|
|
87
|
-
Effect.fn(
|
|
88
|
-
function* ({ expect: _ }) {
|
|
89
|
-
const session = new AiSession({ operationModel: 'configured' });
|
|
90
|
-
const response = yield* session.run({
|
|
91
|
-
prompt: 'What is 10 + 20?',
|
|
92
|
-
history: [],
|
|
93
|
-
toolkit: TestToolkit,
|
|
94
|
-
});
|
|
95
|
-
log.info('response', { response });
|
|
96
|
-
},
|
|
97
|
-
Effect.provide(
|
|
98
|
-
Layer.mergeAll(
|
|
99
|
-
toolkitLayer,
|
|
100
|
-
ToolResolverService.layerEmpty,
|
|
101
|
-
ToolExecutionService.layerEmpty,
|
|
102
|
-
AiService.model('@anthropic/claude-3-5-sonnet-20241022').pipe(
|
|
103
|
-
Layer.provideMerge(AiServiceTestingPreset('direct')),
|
|
104
|
-
),
|
|
105
|
-
),
|
|
106
|
-
),
|
|
107
|
-
),
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
// TODO(dmaretskyi): Revive test.
|
|
111
|
-
/*
|
|
112
|
-
it.skip('create calendar itinerary', { timeout: 60_000 }, async () => {
|
|
113
|
-
// overrides: { model: 'llama3.1:8b' },
|
|
114
|
-
const session = new AiSession({ operationModel: 'configured' });
|
|
115
|
-
const objects = new Set<string>();
|
|
116
|
-
|
|
117
|
-
// Define calendar artifact.
|
|
118
|
-
const calendarArtifact = defineArtifact({
|
|
119
|
-
id: 'artifact:dxos.org/plugin/calendar',
|
|
120
|
-
name: 'Calendar',
|
|
121
|
-
instructions: 'Use this to create and query calendar events.',
|
|
122
|
-
schema: CalendarEventSchema,
|
|
123
|
-
tools: [
|
|
124
|
-
createTool('calendar', {
|
|
125
|
-
name: 'query',
|
|
126
|
-
description: 'Query the calendar for events',
|
|
127
|
-
schema: Schema.Struct({}),
|
|
128
|
-
execute: async () => {
|
|
129
|
-
return ToolResult.Success(CALENDAR_EVENTS);
|
|
130
|
-
},
|
|
131
|
-
}),
|
|
132
|
-
],
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
const tableArtifact = defineArtifact({
|
|
136
|
-
id: 'artifact:dxos.org/plugin/table',
|
|
137
|
-
name: 'Table',
|
|
138
|
-
instructions: 'Use this to create and manage tables. Each table has a unique id.',
|
|
139
|
-
schema: Schema.Struct({}),
|
|
140
|
-
tools: [
|
|
141
|
-
createTool('table', {
|
|
142
|
-
name: 'create',
|
|
143
|
-
description: 'Create a table',
|
|
144
|
-
schema: Schema.Struct({
|
|
145
|
-
data: Schema.Array(Schema.Any).annotations({ description: 'Array of data payloads to add as rows' }),
|
|
146
|
-
}),
|
|
147
|
-
execute: async ({ data }) => {
|
|
148
|
-
log('create table', { data });
|
|
149
|
-
const id = DXN.fromLocalObjectId(ObjectId.random()).toString();
|
|
150
|
-
objects.add(id);
|
|
151
|
-
// TODO(dmaretskyi): consider xml for refs instead of @dxn:echo:@:XXXXX
|
|
152
|
-
return ToolResult.Success(`table @${id}`);
|
|
153
|
-
},
|
|
154
|
-
}),
|
|
155
|
-
],
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
const mapArtifact = defineArtifact({
|
|
159
|
-
id: 'artifact:dxos.org/plugin/map',
|
|
160
|
-
name: 'Map',
|
|
161
|
-
instructions:
|
|
162
|
-
'Use this to create and manage maps. Maps source data from tables. Table id is required to create a map.',
|
|
163
|
-
schema: Schema.Struct({}),
|
|
164
|
-
tools: [
|
|
165
|
-
createTool('map', {
|
|
166
|
-
name: 'create',
|
|
167
|
-
description: 'Create a map',
|
|
168
|
-
schema: Schema.Struct({
|
|
169
|
-
source: ArtifactId.annotations({
|
|
170
|
-
description: 'The table that will be used as the source of the map',
|
|
171
|
-
}),
|
|
172
|
-
}),
|
|
173
|
-
execute: async ({ source }) => {
|
|
174
|
-
// TODO(dmaretskyi): Use effect-schema decode instead of manual parsing.
|
|
175
|
-
const sourceId = ArtifactId.toDXN(source);
|
|
176
|
-
if (!objects.has(sourceId.toString())) {
|
|
177
|
-
return ToolResult.Error(`table id=${source} not found`);
|
|
178
|
-
}
|
|
179
|
-
log('create map', { sourceId });
|
|
180
|
-
const id = DXN.fromLocalObjectId(ObjectId.random()).toString();
|
|
181
|
-
objects.add(id);
|
|
182
|
-
// TODO(dmaretskyi): consider xml for refs instead of @dxn:echo:@:XXXXX
|
|
183
|
-
return ToolResult.Success(`map @${id}`);
|
|
184
|
-
},
|
|
185
|
-
}),
|
|
186
|
-
],
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
const scriptArtifact = defineArtifact({
|
|
190
|
-
id: 'artifact:dxos.org/plugin/script',
|
|
191
|
-
name: 'Script',
|
|
192
|
-
instructions: 'Use this to create and manage scripts',
|
|
193
|
-
schema: Schema.Struct({}),
|
|
194
|
-
tools: [],
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
// session.streamEvent.on((event) => {
|
|
198
|
-
// printStreamEvent(event);
|
|
199
|
-
// });
|
|
200
|
-
|
|
201
|
-
const printer = new ConsolePrinter();
|
|
202
|
-
session.message.on((message) => printer.printMessage(message));
|
|
203
|
-
session.userMessage.on((message) => printer.printMessage(message));
|
|
204
|
-
session.block.on((block) => printer.printContentBlock(block));
|
|
205
|
-
|
|
206
|
-
// session.update.on((update) => {
|
|
207
|
-
// log('update', { update });
|
|
208
|
-
// });
|
|
209
|
-
|
|
210
|
-
// Test creating an itinerary
|
|
211
|
-
const response = await session.run({
|
|
212
|
-
client: aiClient,
|
|
213
|
-
tools: [],
|
|
214
|
-
artifacts: [calendarArtifact, tableArtifact, mapArtifact, scriptArtifact],
|
|
215
|
-
requiredArtifactIds: [calendarArtifact.id, tableArtifact.id, mapArtifact.id, scriptArtifact.id],
|
|
216
|
-
history: [],
|
|
217
|
-
generationOptions: {
|
|
218
|
-
model: '@anthropic/claude-3-5-haiku-20241022',
|
|
219
|
-
},
|
|
220
|
-
prompt: 'create a table and map for a travel itinerary based on events in my calendar',
|
|
221
|
-
toolResolver: new ToolRegistry([]),
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
log('result', {
|
|
225
|
-
objects,
|
|
226
|
-
finalMessage: response.at(-1),
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
*/
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
// Travel to rome, florence, livorno, siena, madrid for conferences
|
|
233
|
-
|
|
234
|
-
const _CALENDAR_EVENTS: CalendarEvent[] = [
|
|
235
|
-
Obj.make(CalendarEventSchema, {
|
|
236
|
-
title: 'Exploring Ancient Ruins in Rome',
|
|
237
|
-
startTime: '2024-01-01T10:00:00Z',
|
|
238
|
-
endTime: '2024-01-01T11:00:00Z',
|
|
239
|
-
description: 'Tech conference at the historic Colosseum with networking opportunities',
|
|
240
|
-
}),
|
|
241
|
-
Obj.make(CalendarEventSchema, {
|
|
242
|
-
title: 'Renaissance Tech Summit in Florence',
|
|
243
|
-
startTime: '2024-01-01T11:00:00Z',
|
|
244
|
-
endTime: '2024-01-01T12:00:00Z',
|
|
245
|
-
description: 'Discussing AI innovations surrounded by Renaissance art',
|
|
246
|
-
}),
|
|
247
|
-
Obj.make(CalendarEventSchema, {
|
|
248
|
-
title: 'Travel to Livorno',
|
|
249
|
-
startTime: '2024-01-01T12:00:00Z',
|
|
250
|
-
endTime: '2024-01-01T13:00:00Z',
|
|
251
|
-
description: 'Travel to Livorno',
|
|
252
|
-
}),
|
|
253
|
-
Obj.make(CalendarEventSchema, {
|
|
254
|
-
title: 'Travel to Siena',
|
|
255
|
-
startTime: '2024-01-01T13:00:00Z',
|
|
256
|
-
endTime: '2024-01-01T14:00:00Z',
|
|
257
|
-
description: 'Travel to Siena',
|
|
258
|
-
}),
|
|
259
|
-
Obj.make(CalendarEventSchema, {
|
|
260
|
-
title: 'Travel to Madrid',
|
|
261
|
-
startTime: '2024-01-01T14:00:00Z',
|
|
262
|
-
endTime: '2024-01-01T15:00:00Z',
|
|
263
|
-
description: 'Travel to Madrid',
|
|
264
|
-
}),
|
|
265
|
-
];
|
|
266
|
-
|
|
267
|
-
// const printStreamEvent = (event: GenerationStreamEvent) => {
|
|
268
|
-
// switch (event.type) {
|
|
269
|
-
// case 'message_start': {
|
|
270
|
-
// process.stdout.write(`${event.message.role.toUpperCase()}\n\n`);
|
|
271
|
-
// for (const content of event.message.content) {
|
|
272
|
-
// printContentBlock(content);
|
|
273
|
-
// }
|
|
274
|
-
// break;
|
|
275
|
-
// }
|
|
276
|
-
// case 'content_block_start': {
|
|
277
|
-
// printContentBlock(event.content);
|
|
278
|
-
// break;
|
|
279
|
-
// }
|
|
280
|
-
// case 'content_block_delta': {
|
|
281
|
-
// switch (event.delta.type) {
|
|
282
|
-
// case 'text_delta': {
|
|
283
|
-
// process.stdout.write(event.delta.text);
|
|
284
|
-
// break;
|
|
285
|
-
// }
|
|
286
|
-
// case 'input_json_delta': {
|
|
287
|
-
// process.stdout.write(event.delta.partial_json);
|
|
288
|
-
// break;
|
|
289
|
-
// }
|
|
290
|
-
// }
|
|
291
|
-
// break;
|
|
292
|
-
// }
|
|
293
|
-
// case 'content_block_stop': {
|
|
294
|
-
// process.stdout.write('\n');
|
|
295
|
-
// break;
|
|
296
|
-
// }
|
|
297
|
-
// case 'message_delta': {
|
|
298
|
-
// break;
|
|
299
|
-
// }
|
|
300
|
-
// case 'message_stop': {
|
|
301
|
-
// process.stdout.write('\n\n');
|
|
302
|
-
// break;
|
|
303
|
-
// }
|
|
304
|
-
// }
|
|
305
|
-
// };
|