@dxos/assistant 0.8.3 → 0.8.4-main.1c7ec43d41
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/README.md +1 -0
- package/dist/lib/neutral/extraction/index.mjs +202 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1736 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/types/src/conversation/context.d.ts +156 -0
- package/dist/types/src/conversation/context.d.ts.map +1 -0
- package/dist/types/src/conversation/context.test.d.ts +2 -0
- package/dist/types/src/conversation/context.test.d.ts.map +1 -0
- package/dist/types/src/conversation/index.d.ts +3 -0
- package/dist/types/src/conversation/index.d.ts.map +1 -0
- package/dist/types/src/conversation/session.d.ts +73 -0
- package/dist/types/src/conversation/session.d.ts.map +1 -0
- package/dist/types/src/diff.d.ts +17 -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 +67 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/execution-graph.d.ts +85 -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-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 +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 +1066 -215
- 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/index.d.ts +2 -0
- package/dist/types/src/functions/index.d.ts.map +1 -0
- package/dist/types/src/functions/services.d.ts +17 -0
- package/dist/types/src/functions/services.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +7 -3
- 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 +15 -0
- package/dist/types/src/session/format.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +5 -1
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +23 -0
- package/dist/types/src/session/observer.d.ts.map +1 -0
- package/dist/types/src/session/request.d.ts +104 -0
- package/dist/types/src/session/request.d.ts.map +1 -0
- 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/session/version-pin.d.ts +5 -5
- 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 +17 -0
- package/dist/types/src/templates/system.d.ts.map +1 -0
- package/dist/types/src/tracing.d.ts +233 -0
- package/dist/types/src/tracing.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts +28 -0
- package/dist/types/src/types/artifact.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +2 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +43 -38
- package/src/conversation/context.test.ts +52 -0
- package/src/conversation/context.ts +403 -0
- package/src/conversation/index.ts +6 -0
- package/src/conversation/session.ts +323 -0
- package/src/diff.test.ts +74 -0
- package/src/diff.ts +93 -0
- package/src/errors.ts +15 -0
- package/src/execution-graph.test.ts +740 -0
- package/src/execution-graph.ts +519 -0
- 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 +3 -1
- package/src/extraction/extraction.ts +44 -15
- package/src/extraction/index.ts +0 -1
- package/src/extraction/quotes.test.ts +3 -3
- package/src/extraction/quotes.ts +6 -7
- package/src/functions/index.ts +5 -0
- package/src/functions/services.ts +214 -0
- package/src/index.ts +7 -3
- package/src/session/artifact-diff.ts +41 -0
- package/src/session/error-handling.ts +41 -0
- package/src/session/format.ts +147 -0
- package/src/session/index.ts +5 -1
- package/src/session/observer.ts +62 -0
- package/src/session/request.ts +347 -0
- package/src/session/toolkit.ts +47 -0
- package/src/session/version-pin.ts +12 -12
- package/src/templates/index.ts +5 -0
- package/src/templates/instructions/format.tpl +57 -0
- package/src/templates/instructions/system.tpl +96 -0
- package/src/templates/system.ts +42 -0
- package/src/tracing.ts +57 -0
- package/src/types/artifact.ts +89 -0
- package/src/types/index.ts +5 -0
- package/src/typings.d.ts +5 -0
- package/dist/lib/browser/index.mjs +0 -2158
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/node/index.cjs +0 -2178
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
- package/dist/lib/node-esm/index.mjs +0 -2160
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/types/bin/chat.d.ts +0 -2
- package/dist/types/bin/chat.d.ts.map +0 -1
- package/dist/types/src/blueprint/ai.test.d.ts +0 -2
- package/dist/types/src/blueprint/ai.test.d.ts.map +0 -1
- package/dist/types/src/blueprint/blueprint.d.ts +0 -55
- package/dist/types/src/blueprint/blueprint.d.ts.map +0 -1
- package/dist/types/src/blueprint/blueprint.test.d.ts +0 -2
- package/dist/types/src/blueprint/blueprint.test.d.ts.map +0 -1
- package/dist/types/src/blueprint/index.d.ts +0 -4
- package/dist/types/src/blueprint/index.d.ts.map +0 -1
- package/dist/types/src/blueprint/logger.d.ts +0 -25
- package/dist/types/src/blueprint/logger.d.ts.map +0 -1
- package/dist/types/src/blueprint/machine.d.ts +0 -143
- package/dist/types/src/blueprint/machine.d.ts.map +0 -1
- package/dist/types/src/blueprint/test-data.d.ts +0 -3
- package/dist/types/src/blueprint/test-data.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 -208
- 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/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/session.d.ts +0 -261
- 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.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/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/dist/types/src/types.d.ts +0 -3
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/blueprint/ai.test.ts +0 -156
- package/src/blueprint/blueprint.test.ts +0 -151
- package/src/blueprint/blueprint.ts +0 -75
- package/src/blueprint/index.ts +0 -7
- package/src/blueprint/logger.ts +0 -155
- package/src/blueprint/machine.ts +0 -251
- package/src/blueprint/test-data.ts +0 -55
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -95
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -65
- package/src/extraction/extraction-llm-function/index.ts +0 -5
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +0 -115
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +0 -54
- 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 -89
- package/src/session/session.test.ts +0 -229
- package/src/session/session.ts +0 -549
- package/src/session/version-pin.test.ts +0 -26
- package/src/summarization/summarization.test.ts +0 -221
- 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
- package/src/types.ts +0 -8
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
|
@@ -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
|
@@ -2,11 +2,10 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { ObjectId } from '@dxos/echo-schema';
|
|
7
|
+
import { type Database, Filter, Query } from '@dxos/echo';
|
|
8
|
+
import { ObjectId } from '@dxos/keys';
|
|
10
9
|
import { DXN } from '@dxos/keys';
|
|
11
10
|
import { log } from '@dxos/log';
|
|
12
11
|
|
|
@@ -28,10 +27,10 @@ export const ReferencedQuotes = Schema.Struct({
|
|
|
28
27
|
});
|
|
29
28
|
export type ReferencedQuotes = Schema.Schema.Type<typeof ReferencedQuotes>;
|
|
30
29
|
|
|
31
|
-
export const findQuotes = async (quotes: string[], db:
|
|
30
|
+
export const findQuotes = async (quotes: string[], db: Database.Database): Promise<ReferencedQuotes> => {
|
|
32
31
|
const references = await Promise.all(
|
|
33
32
|
quotes.map(async (quote) => {
|
|
34
|
-
const
|
|
33
|
+
const objects = await db.query(Query.select(Filter.text(quote, { type: 'vector' }))).run();
|
|
35
34
|
log.info('found quotes in vector index', { objects, quote });
|
|
36
35
|
return objects.length > 0 ? [{ id: objects[0].id.toString(), quote }] : [];
|
|
37
36
|
}),
|
|
@@ -51,7 +50,7 @@ export const insertReferences = (text: string, quotes: ReferencedQuotes) => {
|
|
|
51
50
|
|
|
52
51
|
// Use a case-insensitive regular expression to replace the quote.
|
|
53
52
|
const regex = new RegExp(quote.quote.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi');
|
|
54
|
-
text = text.replace(regex, `[${quote.quote}]
|
|
53
|
+
text = text.replace(regex, `[${quote.quote}](${DXN.fromLocalObjectId(quote.id).toString()})`);
|
|
55
54
|
}
|
|
56
55
|
|
|
57
56
|
return text;
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Tool from '@effect/ai/Tool';
|
|
6
|
+
import type * as Toolkit from '@effect/ai/Toolkit';
|
|
7
|
+
import * as Context from 'effect/Context';
|
|
8
|
+
import * as Effect from 'effect/Effect';
|
|
9
|
+
import * as Layer from 'effect/Layer';
|
|
10
|
+
import * as Option from 'effect/Option';
|
|
11
|
+
import * as Record from 'effect/Record';
|
|
12
|
+
import * as Schema from 'effect/Schema';
|
|
13
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
14
|
+
|
|
15
|
+
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
16
|
+
import { OpaqueToolkit } from '@dxos/ai';
|
|
17
|
+
import { Operation, OperationRegistry } from '@dxos/compute';
|
|
18
|
+
import { todo } from '@dxos/debug';
|
|
19
|
+
import { Ref } from '@dxos/echo';
|
|
20
|
+
import { invariant } from '@dxos/invariant';
|
|
21
|
+
|
|
22
|
+
import { RefFromLLM } from '../types';
|
|
23
|
+
|
|
24
|
+
export const makeToolResolverFromOperations = <R = never>({
|
|
25
|
+
toolkit: extraToolkit = OpaqueToolkit.empty,
|
|
26
|
+
}: { toolkit?: OpaqueToolkit.OpaqueToolkit<never, never, R> } = {}): Layer.Layer<
|
|
27
|
+
ToolResolverService,
|
|
28
|
+
never,
|
|
29
|
+
OpaqueToolkit.OpaqueToolkitProvider | OperationRegistry.Service | R
|
|
30
|
+
> => {
|
|
31
|
+
return Layer.effect(
|
|
32
|
+
ToolResolverService,
|
|
33
|
+
Effect.gen(function* () {
|
|
34
|
+
const toolkitProvider = yield* OpaqueToolkit.OpaqueToolkitProvider;
|
|
35
|
+
const operationRegistry = yield* OperationRegistry.Service;
|
|
36
|
+
return {
|
|
37
|
+
resolve: (id): Effect.Effect<Tool.Any, AiToolNotFoundError> =>
|
|
38
|
+
Effect.gen(function* () {
|
|
39
|
+
const toolkit = OpaqueToolkit.merge(extraToolkit, toolkitProvider.getToolkit());
|
|
40
|
+
|
|
41
|
+
const tool = toolkit.toolkit.tools[id];
|
|
42
|
+
if (tool) {
|
|
43
|
+
return tool;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return yield* operationRegistry.resolve(id).pipe(
|
|
47
|
+
Effect.flatMap(
|
|
48
|
+
Option.match({
|
|
49
|
+
onSome: (_) => Effect.succeed(projectFunctionToTool(_)),
|
|
50
|
+
onNone: () => Effect.fail(new AiToolNotFoundError(id)),
|
|
51
|
+
}),
|
|
52
|
+
),
|
|
53
|
+
);
|
|
54
|
+
}),
|
|
55
|
+
} satisfies Context.Tag.Service<ToolResolverService>;
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const makeToolExecutionService = <E, R>(opts: {
|
|
61
|
+
invoke: (tool: Tool.Any, input: unknown) => Effect.Effect<unknown>;
|
|
62
|
+
}): Layer.Layer<ToolExecutionService, never, OpaqueToolkit.OpaqueToolkitProvider> =>
|
|
63
|
+
Layer.effect(
|
|
64
|
+
ToolExecutionService,
|
|
65
|
+
Effect.gen(function* () {
|
|
66
|
+
const toolkitProvider = yield* OpaqueToolkit.OpaqueToolkitProvider;
|
|
67
|
+
const toolkit = toolkitProvider.getToolkit();
|
|
68
|
+
|
|
69
|
+
const toolkitHandler = yield* toolkit.toolkit.pipe(Effect.provide(toolkit.layer));
|
|
70
|
+
invariant(isHandlerLike(toolkitHandler));
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
handlersFor: (toolkit) => {
|
|
74
|
+
const makeHandler = (tool: Tool.Any): ((params: unknown) => Effect.Effect<unknown, any, any>) => {
|
|
75
|
+
return Effect.fn(`toolFunctionHandler ${tool.name}`)(function* (input: any) {
|
|
76
|
+
if (toolkitHandler.tools[tool.name]) {
|
|
77
|
+
if (Tool.isProviderDefined(tool)) {
|
|
78
|
+
throw new Error('Attempted to call a provider-defined tool');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// TODO(wittjosiah): Everything is `never` here.
|
|
82
|
+
const { result } = yield* (toolkitHandler.handle as any)(tool.name, input);
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return yield* opts.invoke(tool, input).pipe(Effect.catchAllDefect((defect) => Effect.fail(defect)));
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return toolkit.of(
|
|
91
|
+
Record.map(toolkit.tools, (tool, _name) => (Tool.isUserDefined(tool) ? makeHandler(tool) : null)) as any,
|
|
92
|
+
) as any;
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
export const makeToolExecutionServiceFromOperationInvoker = (): Layer.Layer<
|
|
99
|
+
ToolExecutionService,
|
|
100
|
+
never,
|
|
101
|
+
Operation.Service | OpaqueToolkit.OpaqueToolkitProvider
|
|
102
|
+
> => {
|
|
103
|
+
return Layer.unwrapEffect(
|
|
104
|
+
Effect.gen(function* () {
|
|
105
|
+
const operationInvoker = yield* Operation.Service;
|
|
106
|
+
|
|
107
|
+
return makeToolExecutionService({
|
|
108
|
+
invoke: (tool, input) =>
|
|
109
|
+
Effect.gen(function* () {
|
|
110
|
+
const operationDef = getOperationFromTool(tool).pipe(Option.getOrThrow);
|
|
111
|
+
|
|
112
|
+
return yield* operationInvoker.invoke(operationDef, input).pipe(Effect.orDie);
|
|
113
|
+
}),
|
|
114
|
+
});
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export const ToolExecutionServices = Layer.mergeAll(
|
|
120
|
+
makeToolResolverFromOperations(),
|
|
121
|
+
makeToolExecutionServiceFromOperationInvoker(),
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
class FunctionToolAnnotation extends Context.Tag('@dxos/assistant/FunctionToolAnnotation')<
|
|
125
|
+
FunctionToolAnnotation,
|
|
126
|
+
{ definition: Operation.Definition.Any }
|
|
127
|
+
>() {}
|
|
128
|
+
|
|
129
|
+
export const getOperationFromTool = (tool: Tool.Any): Option.Option<Operation.Definition.Any> => {
|
|
130
|
+
return Context.getOption(FunctionToolAnnotation)(tool.annotations).pipe(Option.map(({ definition }) => definition));
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const toolCache = new WeakMap<Operation.Definition.Any, Tool.Any>();
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Projects an `Operation.Definition` into an `AiTool`.
|
|
137
|
+
*/
|
|
138
|
+
const projectFunctionToTool = (fn: Operation.Definition.Any): Tool.Any => {
|
|
139
|
+
if (toolCache.has(fn)) {
|
|
140
|
+
return toolCache.get(fn)!;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const tool = Tool.make(makeToolName(fn.meta.name ?? fn.meta.key), {
|
|
144
|
+
description: fn.meta.description,
|
|
145
|
+
parameters: createStructFieldsFromSchema(fn.input),
|
|
146
|
+
failure: Schema.Any,
|
|
147
|
+
}).annotate(FunctionToolAnnotation, { definition: fn });
|
|
148
|
+
toolCache.set(fn, tool);
|
|
149
|
+
return tool;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @returns Tool name produced from function name by escaping invalid characters.
|
|
154
|
+
*/
|
|
155
|
+
const makeToolName = (name: string) => {
|
|
156
|
+
const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, '-');
|
|
157
|
+
invariant(toolName.match(/^[a-z_][a-z0-9-_]*$/));
|
|
158
|
+
return toolName;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// TODO(dmaretskyi): Factor out.
|
|
162
|
+
const createStructFieldsFromSchema = (schema: Schema.Schema<any, any>): Record<string, Schema.Schema<any, any>> => {
|
|
163
|
+
switch (schema.ast._tag) {
|
|
164
|
+
case 'TypeLiteral':
|
|
165
|
+
return Object.fromEntries(
|
|
166
|
+
schema.ast.propertySignatures.map((prop) => [prop.name, Schema.make(mapSchemaTypeForLLM(prop.type))]),
|
|
167
|
+
);
|
|
168
|
+
case 'VoidKeyword':
|
|
169
|
+
return {};
|
|
170
|
+
default:
|
|
171
|
+
return todo(`Unsupported schema AST: ${schema.ast._tag}`);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Picks an LLM-friendly schema type for the given schema AST.
|
|
177
|
+
* The picked schema type decodes to the original schema type.
|
|
178
|
+
*/
|
|
179
|
+
const mapSchemaTypeForLLM = (ast: SchemaAST.AST): SchemaAST.AST => {
|
|
180
|
+
if (Ref.isRefType(ast)) {
|
|
181
|
+
const description = ast.annotations.description
|
|
182
|
+
? ast.annotations.description + '\n' + RefFromLLM.ast.annotations.description
|
|
183
|
+
: (RefFromLLM.ast.annotations.description as string);
|
|
184
|
+
return RefFromLLM.annotations({ description }).ast;
|
|
185
|
+
} else if (SchemaAST.isTupleType(ast)) {
|
|
186
|
+
return new SchemaAST.TupleType(
|
|
187
|
+
ast.elements.map((t) => new SchemaAST.OptionalType(mapSchemaTypeForLLM(t.type), t.isOptional, t.annotations)),
|
|
188
|
+
ast.rest.map((t) => new SchemaAST.Type(mapSchemaTypeForLLM(t.type), t.annotations)),
|
|
189
|
+
ast.isReadonly,
|
|
190
|
+
ast.annotations,
|
|
191
|
+
);
|
|
192
|
+
} else if (SchemaAST.isTypeLiteral(ast)) {
|
|
193
|
+
return new SchemaAST.TypeLiteral(
|
|
194
|
+
ast.propertySignatures.map(
|
|
195
|
+
(p) =>
|
|
196
|
+
new SchemaAST.PropertySignature(
|
|
197
|
+
p.name,
|
|
198
|
+
mapSchemaTypeForLLM(p.type),
|
|
199
|
+
p.isOptional,
|
|
200
|
+
p.isReadonly,
|
|
201
|
+
p.annotations,
|
|
202
|
+
),
|
|
203
|
+
),
|
|
204
|
+
ast.indexSignatures,
|
|
205
|
+
ast.annotations,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return ast;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const isHandlerLike = (value: unknown): value is Toolkit.WithHandler<Record<string, Tool.Any>> => {
|
|
213
|
+
return typeof (value as any).tools === 'object' && typeof (value as any).handle === 'function';
|
|
214
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -2,8 +2,12 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
5
|
+
export * from './conversation';
|
|
6
|
+
export * from './diff';
|
|
7
|
+
export * from './errors';
|
|
8
|
+
export * from './execution-graph';
|
|
9
|
+
export * from './functions';
|
|
7
10
|
export * from './session';
|
|
11
|
+
export * from './templates';
|
|
8
12
|
export * from './types';
|
|
9
|
-
export * from './
|
|
13
|
+
export * from './tracing';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Context from 'effect/Context';
|
|
6
|
+
|
|
7
|
+
import { type ObjectVersion } from '@dxos/echo-db';
|
|
8
|
+
import { type ObjectId } from '@dxos/keys';
|
|
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,41 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as AiError from '@effect/ai/AiError';
|
|
6
|
+
import * as HttpClientError from '@effect/platform/HttpClientError';
|
|
7
|
+
import * as Effect from 'effect/Effect';
|
|
8
|
+
import * as Schema from 'effect/Schema';
|
|
9
|
+
|
|
10
|
+
import { AiModelError } from '../errors';
|
|
11
|
+
|
|
12
|
+
const AnthropicErrorResponse = Schema.Struct({
|
|
13
|
+
type: Schema.Literal('error'),
|
|
14
|
+
error: Schema.Struct({
|
|
15
|
+
type: Schema.String,
|
|
16
|
+
message: Schema.String,
|
|
17
|
+
}),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// TODO(dmaretskyi): Needs rework
|
|
21
|
+
export const mapAiError = (err: AiError.AiError): Effect.Effect<AiError.AiError> =>
|
|
22
|
+
Effect.gen(function* () {
|
|
23
|
+
const cause = err.cause;
|
|
24
|
+
if (HttpClientError.isHttpClientError(cause) && cause.reason === 'StatusCode') {
|
|
25
|
+
const body = yield* cause.response.json.pipe(
|
|
26
|
+
Effect.flatMap(Schema.decodeUnknown(AnthropicErrorResponse, { exact: false })),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const parsedCause = new AiModelError({
|
|
30
|
+
message: body.error.message,
|
|
31
|
+
context: { model: 'anthropic', type: body.error.type },
|
|
32
|
+
});
|
|
33
|
+
return AiError.UnknownError.make({
|
|
34
|
+
description: body.error.message,
|
|
35
|
+
module: err.module,
|
|
36
|
+
method: err.method,
|
|
37
|
+
cause: parsedCause,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return err;
|
|
41
|
+
}).pipe(Effect.catchAll(() => Effect.succeed(err)));
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Function from 'effect/Function';
|
|
7
|
+
import * as Option from 'effect/Option';
|
|
8
|
+
|
|
9
|
+
import { type FunctionNotFoundError, type Operation, type OperationRegistry, Template } from '@dxos/compute';
|
|
10
|
+
import { type Database, Obj } from '@dxos/echo';
|
|
11
|
+
import { ObjectVersion } from '@dxos/echo-db';
|
|
12
|
+
import type { ObjectNotFoundError } from '@dxos/echo/Err';
|
|
13
|
+
import { type ObjectId } from '@dxos/keys';
|
|
14
|
+
import { log } from '@dxos/log';
|
|
15
|
+
import { type ContentBlock, Message } from '@dxos/types';
|
|
16
|
+
import { trim } from '@dxos/util';
|
|
17
|
+
|
|
18
|
+
import { AiAssistantError } from '../errors';
|
|
19
|
+
import { ArtifactDiffResolver } from './artifact-diff';
|
|
20
|
+
import { type AiRequestRunError, type AiRequestRunProps } from './request';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Formats the system prompt.
|
|
24
|
+
*/
|
|
25
|
+
// TODO(burdon): Move to AiPreprocessor.
|
|
26
|
+
export const formatSystemPrompt = ({
|
|
27
|
+
system,
|
|
28
|
+
blueprints = [],
|
|
29
|
+
objects = [],
|
|
30
|
+
}: Pick<AiRequestRunProps, 'system' | 'blueprints' | 'objects'>): Effect.Effect<
|
|
31
|
+
string,
|
|
32
|
+
FunctionNotFoundError | ObjectNotFoundError,
|
|
33
|
+
Database.Service | OperationRegistry.Service | Operation.Service
|
|
34
|
+
> =>
|
|
35
|
+
Effect.gen(function* () {
|
|
36
|
+
const blueprintDefs = yield* Function.pipe(
|
|
37
|
+
blueprints,
|
|
38
|
+
Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)),
|
|
39
|
+
Effect.flatMap(
|
|
40
|
+
Effect.forEach((template) =>
|
|
41
|
+
Effect.gen(function* () {
|
|
42
|
+
return trim`
|
|
43
|
+
<blueprint>
|
|
44
|
+
${yield* Template.processTemplate(template)}
|
|
45
|
+
</blueprint>
|
|
46
|
+
`;
|
|
47
|
+
}),
|
|
48
|
+
),
|
|
49
|
+
),
|
|
50
|
+
Effect.map((blueprints) =>
|
|
51
|
+
blueprints.length > 0 ? ['## Blueprints Definitions', ...blueprints].join('\n\n') : undefined,
|
|
52
|
+
),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const objectDefs = yield* Function.pipe(
|
|
56
|
+
objects,
|
|
57
|
+
Effect.forEach((object) =>
|
|
58
|
+
Effect.succeed(trim`
|
|
59
|
+
<object>
|
|
60
|
+
<dxn>${Obj.getDXN(object)}</dxn>
|
|
61
|
+
<typename>${Obj.getTypename(object)}</typename>
|
|
62
|
+
</object>
|
|
63
|
+
`),
|
|
64
|
+
),
|
|
65
|
+
Effect.map((objects) => (objects.length > 0 ? ['## Context Objects', ...objects].join('\n\n') : undefined)),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
return yield* Function.pipe(
|
|
69
|
+
Effect.succeed([system, blueprintDefs, objectDefs].filter((def): def is string => def !== undefined)),
|
|
70
|
+
Effect.map((parts) => parts.join('\n\n')),
|
|
71
|
+
);
|
|
72
|
+
}).pipe(Effect.withSpan('formatSystemPrompt'));
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Formats the user prompt.
|
|
76
|
+
*/
|
|
77
|
+
// TODO(burdon): Move to AiPreprocessor.
|
|
78
|
+
// TODO(burdon): Convert util below to `Effect.fn` (to preserve stack info)
|
|
79
|
+
export const formatUserPrompt = ({
|
|
80
|
+
prompt,
|
|
81
|
+
history = [],
|
|
82
|
+
}: Pick<AiRequestRunProps, 'prompt' | 'history'>): Effect.Effect<Message.Message, AiRequestRunError> =>
|
|
83
|
+
Effect.gen(function* () {
|
|
84
|
+
const blocks: ContentBlock.Any[] = [];
|
|
85
|
+
|
|
86
|
+
// TODO(dmaretskyi): Evaluate other approaches as `serviceOption` isn't represented in the type system.
|
|
87
|
+
const artifactDiffResolver = yield* Effect.serviceOption(ArtifactDiffResolver);
|
|
88
|
+
if (Option.isSome(artifactDiffResolver)) {
|
|
89
|
+
const versions = gatherObjectVersions(history);
|
|
90
|
+
const artifactDiff = yield* Effect.tryPromise({
|
|
91
|
+
try: () =>
|
|
92
|
+
artifactDiffResolver.value.resolve(
|
|
93
|
+
[...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),
|
|
94
|
+
),
|
|
95
|
+
catch: AiAssistantError.wrap({ message: 'Artifact diff resolution error' }),
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
log('version', { artifactDiff, versions });
|
|
99
|
+
for (const [id, { version }] of [...artifactDiff.entries()]) {
|
|
100
|
+
if (ObjectVersion.equals(version, versions.get(id)!)) {
|
|
101
|
+
artifactDiff.delete(id);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
blocks.push({ _tag: 'anchor', objectId: id, version });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (artifactDiff.size > 0) {
|
|
109
|
+
blocks.push(createArtifactUpdateBlock(artifactDiff));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return Obj.make(Message.Message, {
|
|
114
|
+
created: new Date().toISOString(),
|
|
115
|
+
sender: { role: 'user' },
|
|
116
|
+
blocks: typeof prompt === 'string' ? [...blocks, { _tag: 'text', text: prompt }] : [...blocks, ...prompt],
|
|
117
|
+
});
|
|
118
|
+
}).pipe(Effect.withSpan('formatUserPrompt'));
|
|
119
|
+
|
|
120
|
+
const gatherObjectVersions = (messages: Message.Message[]): Map<ObjectId, ObjectVersion> => {
|
|
121
|
+
const artifactIds = new Map<ObjectId, ObjectVersion>();
|
|
122
|
+
for (const message of messages) {
|
|
123
|
+
for (const block of message.blocks) {
|
|
124
|
+
if (block._tag === 'anchor') {
|
|
125
|
+
artifactIds.set(block.objectId, block.version as ObjectVersion);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return artifactIds;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const createArtifactUpdateBlock = (
|
|
134
|
+
artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
|
|
135
|
+
): ContentBlock.Any => {
|
|
136
|
+
return {
|
|
137
|
+
_tag: 'text',
|
|
138
|
+
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
139
|
+
disposition: 'synthetic',
|
|
140
|
+
text: trim`
|
|
141
|
+
The following artifacts have been updated since the last message:
|
|
142
|
+
${[...artifactDiff.entries()]
|
|
143
|
+
.map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
|
|
144
|
+
.join('\n')}
|
|
145
|
+
`,
|
|
146
|
+
};
|
|
147
|
+
};
|
package/src/session/index.ts
CHANGED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as Tool from '@effect/ai/Tool';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Function from 'effect/Function';
|
|
8
|
+
|
|
9
|
+
import { type AiParser, type ConsolePrinter } from '@dxos/ai';
|
|
10
|
+
import { type Message } from '@dxos/types';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Live observer of the generation process.
|
|
14
|
+
*/
|
|
15
|
+
export interface GenerationObserver<
|
|
16
|
+
Tools extends Record<string, Tool.Any> = any,
|
|
17
|
+
> extends AiParser.ParseResponseCallbacks<Tools> {
|
|
18
|
+
/**
|
|
19
|
+
* Complete messages fired during the session, both from the model and from the user.
|
|
20
|
+
* This message will contain all message blocks emitted through the `onBlock` callback.
|
|
21
|
+
*/
|
|
22
|
+
onMessage: (message: Message.Message) => Effect.Effect<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const GenerationObserver = Object.freeze({
|
|
26
|
+
make: ({
|
|
27
|
+
onBegin = Function.constant(Effect.void),
|
|
28
|
+
onPart = Function.constant(Effect.void),
|
|
29
|
+
onBlock = Function.constant(Effect.void),
|
|
30
|
+
onEnd = Function.constant(Effect.void),
|
|
31
|
+
onMessage = Function.constant(Effect.void),
|
|
32
|
+
}: Partial<GenerationObserver> = {}): GenerationObserver => ({
|
|
33
|
+
onBegin,
|
|
34
|
+
onPart,
|
|
35
|
+
onBlock,
|
|
36
|
+
onEnd,
|
|
37
|
+
onMessage,
|
|
38
|
+
}),
|
|
39
|
+
|
|
40
|
+
noop: () => GenerationObserver.make(),
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Debug printer to be used in unit-tests and browser devtools.
|
|
44
|
+
*/
|
|
45
|
+
fromPrinter: (printer: ConsolePrinter) =>
|
|
46
|
+
GenerationObserver.make({
|
|
47
|
+
onBlock: (block) =>
|
|
48
|
+
Effect.sync(() => {
|
|
49
|
+
if (block.pending) {
|
|
50
|
+
return; // Only prints full blocks (better for unit-tests and browser devtools).
|
|
51
|
+
}
|
|
52
|
+
printer.printContentBlock(block);
|
|
53
|
+
}),
|
|
54
|
+
onMessage: (message) =>
|
|
55
|
+
Effect.sync(() => {
|
|
56
|
+
if (message.sender.role === 'assistant') {
|
|
57
|
+
return; // Skip assistant messages since they are printed in the `onBlock` callback.
|
|
58
|
+
}
|
|
59
|
+
printer.printMessage(message);
|
|
60
|
+
}),
|
|
61
|
+
}),
|
|
62
|
+
});
|