@dxos/assistant 0.8.4-main.b97322e → 0.8.4-main.bcb3aa67d6
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/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/neutral/chunk-KVYJFFLF.mjs +1805 -0
- package/dist/lib/neutral/chunk-KVYJFFLF.mjs.map +7 -0
- package/dist/lib/neutral/extraction/index.mjs +219 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +551 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/testing/index.mjs +112 -0
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +148 -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/conversation.d.ts +53 -25
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.test.d.ts +2 -0
- package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
- 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 +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 +47 -18
- package/dist/types/src/errors.d.ts.map +1 -1
- 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.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +1060 -273
- 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 +37 -4
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/functions.test.d.ts +2 -0
- package/dist/types/src/functions.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/service/AgentService.d.ts +62 -0
- package/dist/types/src/service/AgentService.d.ts.map +1 -0
- package/dist/types/src/service/AgentService.test.d.ts +2 -0
- package/dist/types/src/service/AgentService.test.d.ts.map +1 -0
- package/dist/types/src/service/agent-process.d.ts +17 -0
- package/dist/types/src/service/agent-process.d.ts.map +1 -0
- package/dist/types/src/service/index.d.ts +3 -0
- package/dist/types/src/service/index.d.ts.map +1 -0
- 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 +12 -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 +4 -0
- 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/session.d.ts +87 -235
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +18 -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 +1 -1
- 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/testing/index.d.ts +1 -2
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +35 -0
- package/dist/types/src/testing/layer.d.ts.map +1 -0
- package/dist/types/src/tracing.d.ts +264 -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 +58 -35
- package/src/conversation/context.test.ts +46 -0
- package/src/conversation/context.ts +369 -0
- package/src/conversation/conversation.test.ts +65 -0
- package/src/conversation/conversation.ts +232 -62
- package/src/conversation/index.ts +1 -0
- package/src/diff.test.ts +74 -0
- package/src/diff.ts +93 -0
- package/src/errors.ts +6 -1
- package/src/execution-graph.test.ts +740 -0
- package/src/execution-graph.ts +509 -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 +2 -0
- 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/services.ts +286 -96
- package/src/functions.conversations.json +1 -0
- package/src/functions.test.ts +82 -0
- package/src/index.ts +5 -3
- package/src/service/AgentService.conversations.json +1 -0
- package/src/service/AgentService.test.ts +169 -0
- package/src/service/AgentService.ts +171 -0
- package/src/service/agent-process.ts +360 -0
- package/src/service/index.ts +6 -0
- package/src/session/artifact-diff.ts +41 -0
- package/src/session/error-handling.ts +41 -0
- package/src/session/format.test.ts +133 -0
- package/src/session/format.ts +143 -0
- package/src/session/index.ts +4 -0
- package/src/session/observer.ts +62 -0
- package/src/session/session.conversations.json +1 -0
- package/src/session/session.test.ts +82 -201
- package/src/session/session.ts +282 -270
- package/src/session/toolkit.ts +44 -0
- package/src/session/version-pin.ts +8 -8
- package/src/templates/index.ts +1 -1
- package/src/templates/instructions/format.tpl +57 -0
- package/src/templates/instructions/system.tpl +57 -0
- package/src/templates/system.ts +42 -0
- package/src/testing/index.ts +2 -8
- package/src/testing/layer.ts +263 -0
- package/src/tracing.ts +112 -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 -1831
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/node-esm/index.mjs +0 -1833
- 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/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 -56
- 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/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 -302
- 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 -12
- package/dist/types/src/research/exa.d.ts.map +0 -1
- package/dist/types/src/research/graph.d.ts +0 -51
- 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/summarization/summarization.test.d.ts +0 -2
- package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
- package/dist/types/src/templates/prompt.d.ts +0 -30
- package/dist/types/src/templates/prompt.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/types.d.ts +0 -13
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/context/binder.ts +0 -103
- package/src/context/binding.ts +0 -32
- package/src/context/index.ts +0 -6
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -83
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -70
- 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 -57
- package/src/research/exa.ts +0 -97
- package/src/research/graph.test.ts +0 -64
- package/src/research/graph.ts +0 -369
- package/src/research/index.ts +0 -7
- package/src/research/instructions-research.tpl +0 -98
- package/src/research/research.test.ts +0 -190
- package/src/research/research.ts +0 -87
- package/src/session/__snapshots__/session.test.ts.snap +0 -32
- package/src/session/version-pin.test.ts +0 -7
- package/src/summarization/summarization.test.ts +0 -208
- package/src/templates/assistant.tpl +0 -111
- package/src/templates/prompt.ts +0 -45
- 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/typedefs.d.ts +0 -8
- package/src/types.ts +0 -47
- /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;
|
|
@@ -2,72 +2,264 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
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 Record from 'effect/Record';
|
|
11
|
+
import * as Schema from 'effect/Schema';
|
|
12
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
7
13
|
|
|
8
14
|
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
15
|
+
import { GenericToolkit } from '@dxos/ai';
|
|
9
16
|
import { todo } from '@dxos/debug';
|
|
10
|
-
import {
|
|
17
|
+
import { Ref } from '@dxos/echo';
|
|
18
|
+
import { FunctionInvocationService, FunctionNotFoundError } from '@dxos/functions';
|
|
11
19
|
import { invariant } from '@dxos/invariant';
|
|
20
|
+
import { Operation, OperationRegistry } from '@dxos/operation';
|
|
21
|
+
import * as Option from 'effect/Option';
|
|
12
22
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
import { RefFromLLM } from '../types';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Constructs a `ToolResolverService` whose `resolve(id)` looks up tools in the following order:
|
|
27
|
+
* 1. Toolkit: return an existing tool from the provided `toolkit` if one is present under `id`.
|
|
28
|
+
* 2. Operations in DB: query by `key=id`; if found, deserialize and project to a tool.
|
|
29
|
+
* 3. Registered operations: fall back to a matching `Operation.Definition` from the handler set.
|
|
30
|
+
*
|
|
31
|
+
* If none of the above yield a match, the effect fails with `AiToolNotFoundError`.
|
|
32
|
+
*
|
|
33
|
+
* Requires `Database.Service` in the environment.
|
|
34
|
+
*
|
|
35
|
+
* @deprecated Moving to operation-based services.
|
|
36
|
+
*/
|
|
37
|
+
export const makeToolResolverFromFunctions = (): Layer.Layer<
|
|
38
|
+
ToolResolverService,
|
|
39
|
+
never,
|
|
40
|
+
GenericToolkit.GenericToolkitProvider | FunctionInvocationService
|
|
41
|
+
> => {
|
|
42
|
+
return Layer.effect(
|
|
43
|
+
ToolResolverService,
|
|
44
|
+
Effect.gen(function* () {
|
|
45
|
+
const toolkitProvider = yield* GenericToolkit.GenericToolkitProvider;
|
|
46
|
+
const functionInvocationService = yield* FunctionInvocationService;
|
|
47
|
+
return {
|
|
48
|
+
resolve: (id): Effect.Effect<Tool.Any, AiToolNotFoundError> =>
|
|
49
|
+
Effect.gen(function* () {
|
|
50
|
+
const toolkit = toolkitProvider.getToolkit();
|
|
51
|
+
|
|
52
|
+
const tool = toolkit.toolkit.tools[id];
|
|
53
|
+
if (tool) {
|
|
54
|
+
return tool;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return yield* functionInvocationService.resolveFunction(id).pipe(
|
|
58
|
+
Effect.map(projectFunctionToTool),
|
|
59
|
+
Effect.catchTag('FunctionNotFound', () => Effect.fail(new AiToolNotFoundError(id))),
|
|
60
|
+
);
|
|
61
|
+
}),
|
|
62
|
+
} satisfies Context.Tag.Service<ToolResolverService>;
|
|
24
63
|
}),
|
|
25
|
-
|
|
64
|
+
);
|
|
26
65
|
};
|
|
27
66
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
67
|
+
/**
|
|
68
|
+
* @deprecated Moving to operation-based services.
|
|
69
|
+
*/
|
|
70
|
+
export const makeToolExecutionServiceFromFunctions = (): Layer.Layer<
|
|
71
|
+
ToolExecutionService,
|
|
72
|
+
never,
|
|
73
|
+
FunctionInvocationService | GenericToolkit.GenericToolkitProvider
|
|
74
|
+
> => {
|
|
75
|
+
return Layer.effect(
|
|
76
|
+
ToolExecutionService,
|
|
77
|
+
Effect.gen(function* () {
|
|
78
|
+
const toolkitProvider = yield* GenericToolkit.GenericToolkitProvider;
|
|
79
|
+
const toolkit = toolkitProvider.getToolkit();
|
|
80
|
+
|
|
81
|
+
const toolkitHandler = yield* toolkit.toolkit.pipe(Effect.provide(toolkit.layer));
|
|
82
|
+
invariant(isHandlerLike(toolkitHandler));
|
|
83
|
+
const functionInvocationService = yield* FunctionInvocationService;
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
handlersFor: (toolkit) => {
|
|
87
|
+
const makeHandler = (tool: Tool.Any): ((params: unknown) => Effect.Effect<unknown, any, any>) => {
|
|
88
|
+
return Effect.fn(`toolFunctionHandler ${tool.name}`)(function* (input: any) {
|
|
89
|
+
if (toolkitHandler.tools[tool.name]) {
|
|
90
|
+
if (Tool.isProviderDefined(tool)) {
|
|
91
|
+
throw new Error('Attempted to call a provider-defined tool');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// TODO(wittjosiah): Everything is `never` here.
|
|
95
|
+
const { result } = yield* (toolkitHandler.handle as any)(tool.name, input);
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const functionDef = getOperationFromTool(tool).pipe(Option.getOrThrow);
|
|
100
|
+
|
|
101
|
+
return yield* functionInvocationService
|
|
102
|
+
.invokeFunction(functionDef, input as any)
|
|
103
|
+
.pipe(Effect.catchAllDefect((defect) => Effect.fail(defect)));
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
return toolkit.of(
|
|
108
|
+
Record.map(toolkit.tools, (tool, _name) => (Tool.isUserDefined(tool) ? makeHandler(tool) : null)) as any,
|
|
109
|
+
) as any;
|
|
110
|
+
},
|
|
43
111
|
};
|
|
112
|
+
}),
|
|
113
|
+
);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export const makeToolResolverFromOperations = <R = never>({
|
|
117
|
+
toolkit: extraToolkit = GenericToolkit.empty,
|
|
118
|
+
}: { toolkit?: GenericToolkit.GenericToolkit<never, never, R> } = {}): Layer.Layer<
|
|
119
|
+
ToolResolverService,
|
|
120
|
+
never,
|
|
121
|
+
GenericToolkit.GenericToolkitProvider | OperationRegistry.Service | R
|
|
122
|
+
> => {
|
|
123
|
+
return Layer.effect(
|
|
124
|
+
ToolResolverService,
|
|
125
|
+
Effect.gen(function* () {
|
|
126
|
+
const toolkitProvider = yield* GenericToolkit.GenericToolkitProvider;
|
|
127
|
+
const operationRegistry = yield* OperationRegistry.Service;
|
|
128
|
+
return {
|
|
129
|
+
resolve: (id): Effect.Effect<Tool.Any, AiToolNotFoundError> =>
|
|
130
|
+
Effect.gen(function* () {
|
|
131
|
+
const toolkit = GenericToolkit.merge(extraToolkit, toolkitProvider.getToolkit());
|
|
132
|
+
|
|
133
|
+
const tool = toolkit.toolkit.tools[id];
|
|
134
|
+
if (tool) {
|
|
135
|
+
return tool;
|
|
136
|
+
}
|
|
44
137
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
138
|
+
return yield* operationRegistry.resolve(id).pipe(
|
|
139
|
+
Effect.flatMap(
|
|
140
|
+
Option.match({
|
|
141
|
+
onSome: (_) => Effect.succeed(projectFunctionToTool(_)),
|
|
142
|
+
onNone: () => Effect.fail(new AiToolNotFoundError(id)),
|
|
143
|
+
}),
|
|
144
|
+
),
|
|
145
|
+
);
|
|
146
|
+
}),
|
|
147
|
+
} satisfies Context.Tag.Service<ToolResolverService>;
|
|
148
|
+
}),
|
|
149
|
+
);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export const makeToolExecutionService = <E, R>(opts: {
|
|
153
|
+
invoke: (tool: Tool.Any, input: unknown) => Effect.Effect<unknown>;
|
|
154
|
+
}): Layer.Layer<ToolExecutionService, never, GenericToolkit.GenericToolkitProvider> =>
|
|
155
|
+
Layer.effect(
|
|
156
|
+
ToolExecutionService,
|
|
157
|
+
Effect.gen(function* () {
|
|
158
|
+
const toolkitProvider = yield* GenericToolkit.GenericToolkitProvider;
|
|
159
|
+
const toolkit = toolkitProvider.getToolkit();
|
|
160
|
+
|
|
161
|
+
const toolkitHandler = yield* toolkit.toolkit.pipe(Effect.provide(toolkit.layer));
|
|
162
|
+
invariant(isHandlerLike(toolkitHandler));
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
handlersFor: (toolkit) => {
|
|
166
|
+
const makeHandler = (tool: Tool.Any): ((params: unknown) => Effect.Effect<unknown, any, any>) => {
|
|
167
|
+
return Effect.fn(`toolFunctionHandler ${tool.name}`)(function* (input: any) {
|
|
168
|
+
if (toolkitHandler.tools[tool.name]) {
|
|
169
|
+
if (Tool.isProviderDefined(tool)) {
|
|
170
|
+
throw new Error('Attempted to call a provider-defined tool');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// TODO(wittjosiah): Everything is `never` here.
|
|
174
|
+
const { result } = yield* (toolkitHandler.handle as any)(tool.name, input);
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return yield* opts.invoke(tool, input).pipe(Effect.catchAllDefect((defect) => Effect.fail(defect)));
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
return toolkit.of(
|
|
183
|
+
Record.map(toolkit.tools, (tool, _name) => (Tool.isUserDefined(tool) ? makeHandler(tool) : null)) as any,
|
|
184
|
+
) as any;
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}),
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
export const makeToolExecutionServiceFromOperationInvoker = (): Layer.Layer<
|
|
191
|
+
ToolExecutionService,
|
|
192
|
+
never,
|
|
193
|
+
Operation.Service | GenericToolkit.GenericToolkitProvider
|
|
194
|
+
> => {
|
|
195
|
+
return Layer.unwrapEffect(
|
|
196
|
+
Effect.gen(function* () {
|
|
197
|
+
const operationInvoker = yield* Operation.Service;
|
|
198
|
+
|
|
199
|
+
return makeToolExecutionService({
|
|
200
|
+
invoke: (tool, input) =>
|
|
201
|
+
Effect.gen(function* () {
|
|
202
|
+
const operationDef = getOperationFromTool(tool).pipe(Option.getOrThrow);
|
|
203
|
+
|
|
204
|
+
return yield* operationInvoker.invoke(operationDef, input).pipe(Effect.orDie);
|
|
205
|
+
}),
|
|
206
|
+
});
|
|
207
|
+
}),
|
|
208
|
+
);
|
|
48
209
|
};
|
|
49
210
|
|
|
50
211
|
/**
|
|
51
|
-
*
|
|
212
|
+
* @deprecated Bridge beetwen deprecated function invocataion service and operation services until we remove function invocation service.
|
|
52
213
|
*/
|
|
53
|
-
|
|
214
|
+
export const functionInvocationServiceFromOperations: Layer.Layer<
|
|
215
|
+
FunctionInvocationService,
|
|
216
|
+
never,
|
|
217
|
+
OperationRegistry.Service | Operation.Service
|
|
218
|
+
> = Layer.effect(
|
|
219
|
+
FunctionInvocationService,
|
|
220
|
+
Effect.gen(function* () {
|
|
221
|
+
const operationRegistry = yield* OperationRegistry.Service;
|
|
222
|
+
const operationInvoker = yield* Operation.Service;
|
|
223
|
+
return {
|
|
224
|
+
invokeFunction: (operationDef, input) => operationInvoker.invoke(operationDef, input).pipe(Effect.orDie),
|
|
225
|
+
resolveFunction: (key) =>
|
|
226
|
+
operationRegistry.resolve(key).pipe(
|
|
227
|
+
Effect.flatten,
|
|
228
|
+
Effect.catchTag('NoSuchElementException', () => Effect.fail(new FunctionNotFoundError(key))),
|
|
229
|
+
),
|
|
230
|
+
} satisfies Context.Tag.Service<FunctionInvocationService>;
|
|
231
|
+
}),
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
export const ToolExecutionServices = Layer.mergeAll(
|
|
235
|
+
makeToolResolverFromFunctions(),
|
|
236
|
+
makeToolExecutionServiceFromFunctions(),
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
class FunctionToolAnnotation extends Context.Tag('@dxos/assistant/FunctionToolAnnotation')<
|
|
54
240
|
FunctionToolAnnotation,
|
|
55
|
-
{
|
|
56
|
-
functionName: string;
|
|
57
|
-
}
|
|
241
|
+
{ definition: Operation.Definition.Any }
|
|
58
242
|
>() {}
|
|
59
243
|
|
|
60
|
-
const
|
|
61
|
-
|
|
244
|
+
export const getOperationFromTool = (tool: Tool.Any): Option.Option<Operation.Definition.Any> => {
|
|
245
|
+
return Context.getOption(FunctionToolAnnotation)(tool.annotations).pipe(Option.map(({ definition }) => definition));
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const toolCache = new WeakMap<Operation.Definition.Any, Tool.Any>();
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Projects an `Operation.Definition` into an `AiTool`.
|
|
252
|
+
*/
|
|
253
|
+
const projectFunctionToTool = (fn: Operation.Definition.Any): Tool.Any => {
|
|
62
254
|
if (toolCache.has(fn)) {
|
|
63
255
|
return toolCache.get(fn)!;
|
|
64
256
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
failure: Schema.Any,
|
|
70
|
-
}).annotate(FunctionToolAnnotation, {
|
|
257
|
+
|
|
258
|
+
const tool = Tool.make(makeToolName(fn.meta.name ?? fn.meta.key), {
|
|
259
|
+
description: fn.meta.description,
|
|
260
|
+
parameters: createStructFieldsFromSchema(fn.input),
|
|
261
|
+
failure: Schema.Any,
|
|
262
|
+
}).annotate(FunctionToolAnnotation, { definition: fn });
|
|
71
263
|
toolCache.set(fn, tool);
|
|
72
264
|
return tool;
|
|
73
265
|
};
|
|
@@ -76,64 +268,62 @@ const projectFunctionToTool = (fn: FunctionDefinition<any, any>): AiTool.Any =>
|
|
|
76
268
|
* @returns Tool name produced from function name by escaping invalid characters.
|
|
77
269
|
*/
|
|
78
270
|
const makeToolName = (name: string) => {
|
|
79
|
-
const toolName = name.replace(/[^a-zA-Z0-9]/g, '');
|
|
80
|
-
invariant(toolName.match(/^[a-
|
|
271
|
+
const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, '-');
|
|
272
|
+
invariant(toolName.match(/^[a-z_][a-z0-9-_]*$/));
|
|
81
273
|
return toolName;
|
|
82
274
|
};
|
|
83
275
|
|
|
84
276
|
// TODO(dmaretskyi): Factor out.
|
|
85
277
|
const createStructFieldsFromSchema = (schema: Schema.Schema<any, any>): Record<string, Schema.Schema<any, any>> => {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return Object.fromEntries(
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
278
|
+
switch (schema.ast._tag) {
|
|
279
|
+
case 'TypeLiteral':
|
|
280
|
+
return Object.fromEntries(
|
|
281
|
+
schema.ast.propertySignatures.map((prop) => [prop.name, Schema.make(mapSchemaTypeForLLM(prop.type))]),
|
|
282
|
+
);
|
|
283
|
+
case 'VoidKeyword':
|
|
284
|
+
return {};
|
|
285
|
+
default:
|
|
286
|
+
return todo(`Unsupported schema AST: ${schema.ast._tag}`);
|
|
287
|
+
}
|
|
92
288
|
};
|
|
93
289
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
) {
|
|
123
|
-
return yield* Effect.promise(() => result);
|
|
124
|
-
} else {
|
|
125
|
-
return result;
|
|
126
|
-
}
|
|
127
|
-
}).pipe(
|
|
128
|
-
Effect.orDie,
|
|
129
|
-
Effect.catchAllDefect((defect) =>
|
|
130
|
-
Effect.die(new FunctionError('Error running function', { context: { name: fnDef.name }, cause: defect })),
|
|
290
|
+
/**
|
|
291
|
+
* Picks an LLM-friendly schema type for the given schema AST.
|
|
292
|
+
* The picked schema type decodes to the original schema type.
|
|
293
|
+
*/
|
|
294
|
+
const mapSchemaTypeForLLM = (ast: SchemaAST.AST): SchemaAST.AST => {
|
|
295
|
+
if (Ref.isRefType(ast)) {
|
|
296
|
+
const description = ast.annotations.description
|
|
297
|
+
? ast.annotations.description + '\n' + RefFromLLM.ast.annotations.description
|
|
298
|
+
: (RefFromLLM.ast.annotations.description as string);
|
|
299
|
+
return RefFromLLM.annotations({ description }).ast;
|
|
300
|
+
} else if (SchemaAST.isTupleType(ast)) {
|
|
301
|
+
return new SchemaAST.TupleType(
|
|
302
|
+
ast.elements.map((t) => new SchemaAST.OptionalType(mapSchemaTypeForLLM(t.type), t.isOptional, t.annotations)),
|
|
303
|
+
ast.rest.map((t) => new SchemaAST.Type(mapSchemaTypeForLLM(t.type), t.annotations)),
|
|
304
|
+
ast.isReadonly,
|
|
305
|
+
ast.annotations,
|
|
306
|
+
);
|
|
307
|
+
} else if (SchemaAST.isTypeLiteral(ast)) {
|
|
308
|
+
return new SchemaAST.TypeLiteral(
|
|
309
|
+
ast.propertySignatures.map(
|
|
310
|
+
(p) =>
|
|
311
|
+
new SchemaAST.PropertySignature(
|
|
312
|
+
p.name,
|
|
313
|
+
mapSchemaTypeForLLM(p.type),
|
|
314
|
+
p.isOptional,
|
|
315
|
+
p.isReadonly,
|
|
316
|
+
p.annotations,
|
|
317
|
+
),
|
|
131
318
|
),
|
|
319
|
+
ast.indexSignatures,
|
|
320
|
+
ast.annotations,
|
|
132
321
|
);
|
|
322
|
+
}
|
|
133
323
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
(assertOutput as any)(data);
|
|
324
|
+
return ast;
|
|
325
|
+
};
|
|
137
326
|
|
|
138
|
-
|
|
139
|
-
|
|
327
|
+
const isHandlerLike = (value: unknown): value is Toolkit.WithHandler<Record<string, Tool.Any>> => {
|
|
328
|
+
return typeof (value as any).tools === 'object' && typeof (value as any).handle === 'function';
|
|
329
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"conversations":[{"parameters":{"model":"@anthropic/claude-opus-4-6","stream":true,"tools":[{"name":"read-name","description":"Reads the name of an organization.","inputSchema":{"type":"object","required":["org"],"properties":{"org":{"type":"string","description":"The ID of the referenced object. Formats accepted:\n- DXN (dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4). DXNs can be prepended with an @ symbol for compatibility with in-text references.\n- space ID, object ID tuple (spaceID:objectID)\n- Only object ID that is assumed to be in the current space (01KG7R1ZXWFMWQ4DA1Q6TN1DG4)","examples":["dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4","@dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4","BM3FSHFOMJCHCG5QW7JTVKGYABD2GAA7G:01KG7R1ZXWFMWQ4DA1Q6TN1DG4","01KG7R1ZXWFMWQ4DA1Q6TN1DG4"]}},"additionalProperties":false}}]},"prompt":{"content":[{"role":"user","content":"What is the name of the organization? 01JGFJJZ00G0WKQSJGMAKCNT89","options":{"anthropic":{"cacheControl":{"ttl":"5m","type":"ephemeral"}}}}]},"response":[{"type":"response-metadata","id":"msg_01FqHQGxFVdQKeMXjodHavsF","modelId":"claude-opus-4-20250514","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"reasoning-start","id":"0","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"The user is asking for the name of","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" an organization an","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"d has provided what","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" appears to be an","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" object","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" ID: 01JGFJ","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"JZ00G0WK","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"QSJGMAKCNT89","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"\n\nI","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" need to use the read-name function","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" to get the name of this organization.","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" According","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" to the function description, I can","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" pass","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" just","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" the object","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" ID if","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" it's assume","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"d to be in the current space.","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"\n\nLet me call","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" the function with this","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" ID.","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"","metadata":{"anthropic":{"type":"thinking","signature":"EokECkYICxgCKkDkOVtqWk3+YJ1nk31yy/hYUygSTbpQxoD7UKl2RqwyS+tJSV2rou35JEFIl30ogJZiohCBXqY0IZ3YO1SwhWq1EgztHHPI5rGjH4gehN8aDBh3BbEfM6OCraoAQiIwDSGtzFCbT7K+yTBtmtri6uuGvPwNGmRshKURPnFRCjdAf0gj1Jwge4pmIYnlGpRGKvACF4kjtsTHsn8f7ktuuzSEpVNaC6d5VYA6/Fm5ZFaiQPn0/F4fQXy1PNEyo4007gP21Ate8+98CCtlfkOnEE53hElq58doXrVOzpL+uH5b6PBPtM9f1+L9cKjhTPV6djIcewwHTTSJDTM6fr9c82f9u5EDvzNe1QODqlqCiVtNsjWCYOWNZUqILdl0TQ43Hzjf2T+WN6ARHfd3mGVsAtg8vcBM8D4mPvThsTnoswYIeB6Y5aDAA+Xu32jGRmJGclevuy25lJFZG8lAhOXJOdiP88zTG5XCN5ED3GLo2rjKBnQzwGpzg1M0ARKkrNzuLbNZ31uk36ayo+1T381Ld0yIQAX/ZUrLNZ8ovJ1sIHW18hmnyec69+4qWuy//NBWlAfyFOQnQfXM1f2XO5AFBrSILGtHxWM1+KINm1Xh68gHZpjjpyM5g8zl61nDLGxNq3OBugM7NSghygmGPmy2DlxU147cVzOHh7ZXh8fFxBfZlW0YAQ=="}}},{"type":"reasoning-end","id":"0","metadata":{}},{"type":"text-start","id":"1","metadata":{}},{"type":"text-delta","id":"1","delta":"I","metadata":{}},{"type":"text-delta","id":"1","delta":"'ll look","metadata":{}},{"type":"text-delta","id":"1","delta":" up the name of that","metadata":{}},{"type":"text-delta","id":"1","delta":" organization for you.","metadata":{}},{"type":"text-end","id":"1","metadata":{}},{"type":"tool-params-start","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","name":"read-name","providerExecuted":false,"metadata":{}},{"type":"tool-params-delta","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","delta":"","metadata":{}},{"type":"tool-params-delta","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","delta":"{\"or","metadata":{}},{"type":"tool-params-delta","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","delta":"g\": \"01JGFJJ","metadata":{}},{"type":"tool-params-delta","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","delta":"Z00G0WKQ","metadata":{}},{"type":"tool-params-delta","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","delta":"SJGM","metadata":{}},{"type":"tool-params-delta","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","delta":"AKCNT89\"}","metadata":{}},{"type":"tool-params-end","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","metadata":{}},{"type":"tool-call","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","name":"read-name","providerExecuted":false,"metadata":{},"params":{"org":"dxn:echo:@:01JGFJJZ00G0WKQSJGMAKCNT89"}},{"type":"finish","reason":"tool-calls","usage":{"inputTokens":729,"outputTokens":187,"totalTokens":916},"metadata":{"anthropic":{"usage":{"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"input_tokens":729,"output_tokens":8,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-opus-4-6","stream":true,"tools":[{"name":"read-name","description":"Reads the name of an organization.","inputSchema":{"type":"object","required":["org"],"properties":{"org":{"type":"string","description":"The ID of the referenced object. Formats accepted:\n- DXN (dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4). DXNs can be prepended with an @ symbol for compatibility with in-text references.\n- space ID, object ID tuple (spaceID:objectID)\n- Only object ID that is assumed to be in the current space (01KG7R1ZXWFMWQ4DA1Q6TN1DG4)","examples":["dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4","@dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4","BM3FSHFOMJCHCG5QW7JTVKGYABD2GAA7G:01KG7R1ZXWFMWQ4DA1Q6TN1DG4","01KG7R1ZXWFMWQ4DA1Q6TN1DG4"]}},"additionalProperties":false}}]},"prompt":{"content":[{"role":"user","content":"What is the name of the organization? 01JGFJJZ00G0WKQSJGMAKCNT89","options":{}},{"role":"assistant","content":[{"type":"reasoning","text":"The user is asking for the name of an organization and has provided what appears to be an object ID: 01JGFJJZ00G0WKQSJGMAKCNT89\n\nI need to use the read-name function to get the name of this organization. According to the function description, I can pass just the object ID if it's assumed to be in the current space.\n\nLet me call the function with this ID.","options":{}},{"type":"text","text":"I'll look up the name of that organization for you.","options":{}},{"type":"tool-call","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","name":"read-name","params":{"org":"01JGFJJZ00G0WKQSJGMAKCNT89"},"providerExecuted":false,"options":{}}],"options":{}},{"role":"tool","content":[{"type":"tool-result","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","name":"read-name","isFailure":false,"result":"BlueYard","providerExecuted":false,"options":{}}],"options":{"anthropic":{"cacheControl":{"ttl":"5m","type":"ephemeral"}}}}]},"response":[{"type":"response-metadata","id":"msg_01X5xvtDymPG7tTUMLZz3eAD","modelId":"claude-opus-4-20250514","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"text-start","id":"0","metadata":{}},{"type":"text-delta","id":"0","delta":"The name of the organization with","metadata":{}},{"type":"text-delta","id":"0","delta":" ID 01JGFJJ","metadata":{}},{"type":"text-delta","id":"0","delta":"Z00G0WKQ","metadata":{}},{"type":"text-delta","id":"0","delta":"SJGMAKCNT89 is","metadata":{}},{"type":"text-delta","id":"0","delta":" **BlueYard**.","metadata":{}},{"type":"text-end","id":"0","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":801,"outputTokens":39,"totalTokens":840},"metadata":{"anthropic":{"usage":{"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"input_tokens":801,"output_tokens":6,"server_tool_use":null,"service_tier":"standard"}}}}]}]}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, it } from '@effect/vitest';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
8
|
+
|
|
9
|
+
import { ConsolePrinter } from '@dxos/ai';
|
|
10
|
+
import { MemoizedAiService } from '@dxos/ai/testing';
|
|
11
|
+
import { AiSession, GenerationObserver, createToolkit } from '@dxos/assistant';
|
|
12
|
+
import { Blueprint } from '@dxos/blueprints';
|
|
13
|
+
import { Database, Obj, Ref } from '@dxos/echo';
|
|
14
|
+
import { TestHelpers } from '@dxos/effect/testing';
|
|
15
|
+
import { ObjectId } from '@dxos/keys';
|
|
16
|
+
import { Operation, OperationHandlerSet } from '@dxos/operation';
|
|
17
|
+
import { Organization } from '@dxos/types';
|
|
18
|
+
|
|
19
|
+
import { AssistantTestLayer } from './testing';
|
|
20
|
+
|
|
21
|
+
ObjectId.dangerouslyDisableRandomness();
|
|
22
|
+
|
|
23
|
+
const ReadName = Operation.make({
|
|
24
|
+
meta: {
|
|
25
|
+
key: 'org.dxos.function.read-name',
|
|
26
|
+
name: 'Read Name',
|
|
27
|
+
description: 'Reads the name of an organization.',
|
|
28
|
+
},
|
|
29
|
+
input: Schema.Struct({
|
|
30
|
+
org: Ref.Ref(Organization.Organization),
|
|
31
|
+
}),
|
|
32
|
+
output: Schema.String,
|
|
33
|
+
services: [Database.Service],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const Handlers = OperationHandlerSet.make(
|
|
37
|
+
Operation.withHandler(
|
|
38
|
+
ReadName,
|
|
39
|
+
Effect.fnUntraced(function* ({ org }) {
|
|
40
|
+
const resolved = yield* Database.load(org);
|
|
41
|
+
return resolved.name ?? '<no org>';
|
|
42
|
+
}),
|
|
43
|
+
),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const blueprint = Blueprint.make({
|
|
47
|
+
key: 'org.dxos.blueprint.test',
|
|
48
|
+
name: 'Test blueprint',
|
|
49
|
+
tools: Blueprint.toolDefinitions({ operations: [ReadName] }),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const TestLayer = AssistantTestLayer({
|
|
53
|
+
aiServicePreset: 'edge-remote',
|
|
54
|
+
operationHandlers: Handlers,
|
|
55
|
+
types: [Organization.Organization],
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('Research', () => {
|
|
59
|
+
it.effect(
|
|
60
|
+
'call a function with a ref input',
|
|
61
|
+
Effect.fnUntraced(
|
|
62
|
+
function* (_) {
|
|
63
|
+
const org = yield* Database.add(
|
|
64
|
+
Obj.make(Organization.Organization, {
|
|
65
|
+
name: 'BlueYard',
|
|
66
|
+
website: 'https://blueyard.com',
|
|
67
|
+
}),
|
|
68
|
+
);
|
|
69
|
+
yield* Database.flush();
|
|
70
|
+
yield* new AiSession({ observer: GenerationObserver.fromPrinter(new ConsolePrinter()) }).run({
|
|
71
|
+
prompt: `What is the name of the organization? ${org.id}`,
|
|
72
|
+
toolkit: yield* createToolkit({
|
|
73
|
+
blueprints: [blueprint],
|
|
74
|
+
}),
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
Effect.provide(TestLayer),
|
|
78
|
+
TestHelpers.provideTestContext,
|
|
79
|
+
),
|
|
80
|
+
MemoizedAiService.isGenerationEnabled() ? 240_000 : 30_000,
|
|
81
|
+
);
|
|
82
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export * from './context';
|
|
6
5
|
export * from './conversation';
|
|
7
|
-
export * from './
|
|
6
|
+
export * from './diff';
|
|
7
|
+
export * from './errors';
|
|
8
|
+
export * from './execution-graph';
|
|
8
9
|
export * from './functions';
|
|
9
|
-
export * from './research';
|
|
10
10
|
export * from './session';
|
|
11
11
|
export * from './templates';
|
|
12
12
|
export * from './types';
|
|
13
|
+
export * from './service';
|
|
14
|
+
export * from './tracing';
|