@dxos/assistant 0.8.4-main.f9ba587 → 0.8.4-main.fffef41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/browser/extraction/index.mjs +225 -0
- package/dist/lib/browser/extraction/index.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +1114 -1843
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/extraction/index.mjs +226 -0
- package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +1114 -1844
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/conversation/context.d.ts +103 -0
- package/dist/types/src/conversation/context.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.d.ts +29 -32
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/conversation/index.d.ts +1 -0
- package/dist/types/src/conversation/index.d.ts.map +1 -1
- package/dist/types/src/diff.d.ts +15 -0
- package/dist/types/src/diff.d.ts.map +1 -0
- package/dist/types/src/diff.test.d.ts +2 -0
- package/dist/types/src/diff.test.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +71 -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-llm-function/extraction-llm-function.d.ts +1 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +195 -58
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +1 -1
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +264 -165
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +1 -1
- 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 +20 -0
- package/dist/types/src/functions/services.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -5
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/GenericToolkit.d.ts +84 -0
- package/dist/types/src/session/GenericToolkit.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 +22 -0
- package/dist/types/src/session/observer.d.ts.map +1 -0
- package/dist/types/src/session/session.d.ts +36 -268
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +15 -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 +18 -0
- package/dist/types/src/templates/system.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts +20 -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 +48 -26
- package/src/conversation/context.ts +186 -0
- package/src/conversation/conversation.ts +101 -76
- package/src/conversation/index.ts +1 -0
- package/src/diff.test.ts +43 -0
- package/src/diff.ts +84 -0
- package/src/errors.ts +15 -0
- package/src/execution-graph.test.ts +740 -0
- package/src/execution-graph.ts +509 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +14 -25
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +48 -52
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +18 -17
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +43 -41
- 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 +7 -6
- package/src/extraction/quotes.test.ts +3 -3
- package/src/extraction/quotes.ts +4 -4
- package/src/functions/index.ts +5 -0
- package/src/functions/services.ts +162 -0
- package/src/index.ts +5 -5
- package/src/session/GenericToolkit.ts +130 -0
- package/src/session/artifact-diff.ts +41 -0
- package/src/session/error-handling.ts +41 -0
- package/src/session/format.test.ts +61 -0
- package/src/session/format.ts +144 -0
- package/src/session/index.ts +5 -0
- package/src/session/observer.ts +59 -0
- package/src/session/session.conversations.json +1 -0
- package/src/session/session.test.ts +88 -214
- package/src/session/session.ts +170 -539
- package/src/session/toolkit.ts +48 -0
- package/src/session/version-pin.ts +11 -12
- package/src/templates/index.ts +5 -0
- package/src/templates/instructions/format.tpl +61 -0
- package/src/templates/instructions/system.tpl +48 -0
- package/src/templates/system.ts +41 -0
- package/src/types/artifact.ts +71 -0
- package/src/types/index.ts +5 -0
- package/src/typings.d.ts +5 -0
- package/dist/types/bin/chat.d.ts +0 -2
- package/dist/types/bin/chat.d.ts.map +0 -1
- package/dist/types/src/blueprint/blueprint.d.ts +0 -41
- package/dist/types/src/blueprint/blueprint.d.ts.map +0 -1
- package/dist/types/src/blueprint/index.d.ts +0 -3
- package/dist/types/src/blueprint/index.d.ts.map +0 -1
- package/dist/types/src/blueprint/registry.d.ts +0 -7
- package/dist/types/src/blueprint/registry.d.ts.map +0 -1
- package/dist/types/src/context/binder.d.ts +0 -34
- package/dist/types/src/context/binder.d.ts.map +0 -1
- package/dist/types/src/context/binding.d.ts +0 -38
- package/dist/types/src/context/binding.d.ts.map +0 -1
- package/dist/types/src/context/index.d.ts +0 -3
- package/dist/types/src/context/index.d.ts.map +0 -1
- package/dist/types/src/research/exa.d.ts +0 -7
- package/dist/types/src/research/exa.d.ts.map +0 -1
- package/dist/types/src/research/graph.d.ts +0 -41
- package/dist/types/src/research/graph.d.ts.map +0 -1
- package/dist/types/src/research/graph.test.d.ts +0 -2
- package/dist/types/src/research/graph.test.d.ts.map +0 -1
- package/dist/types/src/research/index.d.ts +0 -4
- package/dist/types/src/research/index.d.ts.map +0 -1
- package/dist/types/src/research/research.d.ts +0 -10
- package/dist/types/src/research/research.d.ts.map +0 -1
- package/dist/types/src/research/research.test.d.ts +0 -2
- package/dist/types/src/research/research.test.d.ts.map +0 -1
- package/dist/types/src/session/version-pin.test.d.ts +0 -2
- package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
- package/dist/types/src/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/blueprint.ts +0 -75
- package/src/blueprint/index.ts +0 -6
- package/src/blueprint/registry.ts +0 -15
- package/src/context/binder.ts +0 -101
- package/src/context/binding.ts +0 -31
- package/src/context/index.ts +0 -6
- package/src/research/exa.ts +0 -100
- package/src/research/graph.test.ts +0 -15
- package/src/research/graph.ts +0 -337
- package/src/research/index.ts +0 -7
- package/src/research/instructions-research.tpl +0 -97
- package/src/research/research.test.ts +0 -190
- package/src/research/research.ts +0 -90
- package/src/session/version-pin.test.ts +0 -26
- package/src/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
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as Tool from '@effect/ai/Tool';
|
|
6
|
+
import * as Toolkit from '@effect/ai/Toolkit';
|
|
7
|
+
import * as Effect from 'effect/Effect';
|
|
8
|
+
import * as Layer from 'effect/Layer';
|
|
9
|
+
import * as Pipeable from 'effect/Pipeable';
|
|
10
|
+
import type * as Schema from 'effect/Schema';
|
|
11
|
+
import type * as SchemaAST from 'effect/SchemaAST';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Unique identifier for generic toolkit instances.
|
|
15
|
+
*/
|
|
16
|
+
export const TypeId = '~@dxos/assistant/GenericToolkit';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Type-level representation of the generic toolkit identifier.
|
|
20
|
+
*/
|
|
21
|
+
export type TypeId = typeof TypeId;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Type-safe way to define toolkits where we don't know specific types of tools,
|
|
25
|
+
* and we want to bundle definition and handlers together.
|
|
26
|
+
*
|
|
27
|
+
* Usefull for plugin-based systems, where tools are dynamically added.
|
|
28
|
+
*
|
|
29
|
+
* @param TR requirements type to the tool invocation.
|
|
30
|
+
* @param E failure type to the handlers layer.
|
|
31
|
+
* @param R requirements type to the handlers layer.
|
|
32
|
+
*/
|
|
33
|
+
export interface GenericToolkit<TR = never, E = never, R = never> extends Pipeable.Pipeable {
|
|
34
|
+
readonly [TypeId]: TypeId;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Toolkit definition.
|
|
38
|
+
*/
|
|
39
|
+
readonly toolkit: Toolkit.Toolkit<GenericTools<TR>>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Handlers layer.
|
|
43
|
+
*/
|
|
44
|
+
readonly layer: Layer.Layer<unknown, E, R>;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Handlers effect.
|
|
48
|
+
*/
|
|
49
|
+
readonly handlers: Effect.Effect<Toolkit.WithHandler<GenericTools<TR>>, E, R>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Generic constraint.
|
|
54
|
+
*
|
|
55
|
+
* NOTE: Only use in place of `T extends PortableToolkit.Any`. Not suitable for standalone use.
|
|
56
|
+
*/
|
|
57
|
+
export interface Any {
|
|
58
|
+
readonly [TypeId]: TypeId;
|
|
59
|
+
readonly toolkit: Toolkit.Toolkit<any>;
|
|
60
|
+
readonly layer: Layer.Layer<unknown, any, any>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type InvocationRequirements<T extends Any> = T extends GenericToolkit<infer TR, infer _E, infer _R> ? TR : never;
|
|
64
|
+
export type Failure<T extends Any> = T extends GenericToolkit<infer _TR, infer E, infer _R> ? E : never;
|
|
65
|
+
export type Requirements<T extends Any> = T extends GenericToolkit<infer _TR, infer _E, infer R> ? R : never;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Creates a portable toolkit from toolkit definition and handlers layer.
|
|
69
|
+
*/
|
|
70
|
+
export const make = <Tools extends Record<string, Tool.Any>, E, R>(
|
|
71
|
+
toolkit: Toolkit.Toolkit<Tools>,
|
|
72
|
+
layer: Layer.Layer<Tool.HandlersFor<Tools>, E, R>,
|
|
73
|
+
): GenericToolkit<E, R> => {
|
|
74
|
+
return {
|
|
75
|
+
[TypeId]: TypeId,
|
|
76
|
+
toolkit: toolkit as any,
|
|
77
|
+
layer: layer as any,
|
|
78
|
+
handlers: toolkit.pipe(Effect.provide(layer)) as any,
|
|
79
|
+
pipe() {
|
|
80
|
+
// eslint-disable-next-line prefer-rest-params
|
|
81
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Merges multiple portable toolkits into a single portable toolkit.
|
|
88
|
+
*/
|
|
89
|
+
export const merge = <const Toolkits extends ReadonlyArray<Any>>(
|
|
90
|
+
/**
|
|
91
|
+
* The toolkits to merge together.
|
|
92
|
+
*/
|
|
93
|
+
...toolkits: Toolkits
|
|
94
|
+
): GenericToolkit<
|
|
95
|
+
InvocationRequirements<Toolkits[number]>,
|
|
96
|
+
Failure<Toolkits[number]>,
|
|
97
|
+
Requirements<Toolkits[number]>
|
|
98
|
+
> => {
|
|
99
|
+
return make(
|
|
100
|
+
Toolkit.merge(...toolkits.map((t) => t.toolkit)),
|
|
101
|
+
Layer.mergeAll(...(toolkits.map((t) => t.layer) as any)),
|
|
102
|
+
) as any;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Generic tools type where we cannot statically enumerate individual tools.
|
|
107
|
+
*/
|
|
108
|
+
export type GenericTools<R = never> = Record<
|
|
109
|
+
string,
|
|
110
|
+
Tool.Tool<
|
|
111
|
+
string,
|
|
112
|
+
{
|
|
113
|
+
readonly parameters: AnyStructSchemaNoContext;
|
|
114
|
+
readonly success: Schema.Schema.AnyNoContext;
|
|
115
|
+
readonly failure: typeof Schema.Never;
|
|
116
|
+
},
|
|
117
|
+
R
|
|
118
|
+
>
|
|
119
|
+
>;
|
|
120
|
+
|
|
121
|
+
export interface AnyStructSchemaNoContext extends Pipeable.Pipeable {
|
|
122
|
+
readonly [Schema.TypeId]: any;
|
|
123
|
+
readonly make: any;
|
|
124
|
+
readonly Type: any;
|
|
125
|
+
readonly Encoded: any;
|
|
126
|
+
readonly Context: never;
|
|
127
|
+
readonly ast: SchemaAST.AST;
|
|
128
|
+
readonly fields: Schema.Struct.Fields;
|
|
129
|
+
readonly annotations: any;
|
|
130
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Context from 'effect/Context';
|
|
6
|
+
|
|
7
|
+
import { type ObjectId } from '@dxos/echo/internal';
|
|
8
|
+
import { type ObjectVersion } from '@dxos/echo-db';
|
|
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,61 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { beforeAll, describe, it } from '@effect/vitest';
|
|
6
|
+
import * as Effect from 'effect/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 { Text } from '@dxos/schema';
|
|
13
|
+
import { Organization } from '@dxos/types';
|
|
14
|
+
import { trim } from '@dxos/util';
|
|
15
|
+
|
|
16
|
+
import { createSystemPrompt } from '../templates/system';
|
|
17
|
+
|
|
18
|
+
import { formatSystemPrompt } from './format';
|
|
19
|
+
|
|
20
|
+
describe('format', () => {
|
|
21
|
+
let db: EchoDatabase;
|
|
22
|
+
beforeAll(async () => {
|
|
23
|
+
const builder = await new EchoTestBuilder().open();
|
|
24
|
+
({ db } = await builder.createDatabase({
|
|
25
|
+
types: [Text.Text, Organization.Organization, Blueprint.Blueprint],
|
|
26
|
+
}));
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it.effect(
|
|
30
|
+
'should format',
|
|
31
|
+
Effect.fn(function* () {
|
|
32
|
+
const object = db.add(
|
|
33
|
+
Obj.make(Organization.Organization, {
|
|
34
|
+
name: 'Test',
|
|
35
|
+
website: 'https://www.test.com',
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const blueprint = db.add(
|
|
40
|
+
Blueprint.make({
|
|
41
|
+
key: 'example.com/blueprint/test',
|
|
42
|
+
name: 'Test',
|
|
43
|
+
instructions: Template.make({
|
|
44
|
+
source: trim`
|
|
45
|
+
Test
|
|
46
|
+
This is the test blueprint.
|
|
47
|
+
`,
|
|
48
|
+
}),
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const output = yield* formatSystemPrompt({
|
|
53
|
+
system: createSystemPrompt({}),
|
|
54
|
+
blueprints: [blueprint, blueprint],
|
|
55
|
+
objects: [object, object],
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
console.log(output);
|
|
59
|
+
}),
|
|
60
|
+
);
|
|
61
|
+
});
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Array from 'effect/Array';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Function from 'effect/Function';
|
|
8
|
+
import * as Option from 'effect/Option';
|
|
9
|
+
|
|
10
|
+
import { Template } from '@dxos/blueprints';
|
|
11
|
+
import { Obj } from '@dxos/echo';
|
|
12
|
+
import { type ObjectId } from '@dxos/echo/internal';
|
|
13
|
+
import { DatabaseService, ObjectVersion } from '@dxos/echo-db';
|
|
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
|
+
|
|
20
|
+
import { ArtifactDiffResolver } from './artifact-diff';
|
|
21
|
+
import { type AiSessionRunError, type AiSessionRunParams } from './session';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Formats the system prompt.
|
|
25
|
+
*/
|
|
26
|
+
// TODO(burdon): Move to AiPreprocessor.
|
|
27
|
+
export const formatSystemPrompt = ({
|
|
28
|
+
system,
|
|
29
|
+
blueprints = [],
|
|
30
|
+
objects = [],
|
|
31
|
+
}: Pick<AiSessionRunParams<any>, 'system' | 'blueprints' | 'objects'>) =>
|
|
32
|
+
Effect.gen(function* () {
|
|
33
|
+
const blueprintDefs = yield* Function.pipe(
|
|
34
|
+
blueprints,
|
|
35
|
+
Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)),
|
|
36
|
+
Effect.flatMap(Effect.forEach((template) => DatabaseService.loadOption(template.source))),
|
|
37
|
+
Effect.map(Array.filter(Option.isSome)),
|
|
38
|
+
Effect.map(
|
|
39
|
+
Array.map(
|
|
40
|
+
(template) => trim`
|
|
41
|
+
<blueprint>
|
|
42
|
+
${Template.process(template.value.content)}
|
|
43
|
+
</blueprint>
|
|
44
|
+
`,
|
|
45
|
+
),
|
|
46
|
+
),
|
|
47
|
+
Effect.map((blueprints) =>
|
|
48
|
+
blueprints.length > 0 ? ['## Blueprints Definitions', ...blueprints].join('\n\n') : undefined,
|
|
49
|
+
),
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const objectDefs = yield* Function.pipe(
|
|
53
|
+
objects,
|
|
54
|
+
Effect.forEach((object) =>
|
|
55
|
+
Effect.succeed(trim`
|
|
56
|
+
<object>
|
|
57
|
+
<dxn>${Obj.getDXN(object)}</dxn>
|
|
58
|
+
<typename>${Obj.getTypename(object)}</typename>
|
|
59
|
+
</object>
|
|
60
|
+
`),
|
|
61
|
+
),
|
|
62
|
+
Effect.map((objects) => (objects.length > 0 ? ['## Context Objects', ...objects].join('\n\n') : undefined)),
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
return yield* Function.pipe(
|
|
66
|
+
Effect.succeed([system, blueprintDefs, objectDefs].filter((def): def is string => def !== undefined)),
|
|
67
|
+
Effect.map((parts) => parts.join('\n\n')),
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Formats the user prompt.
|
|
73
|
+
*/
|
|
74
|
+
// TODO(burdon): Move to AiPreprocessor.
|
|
75
|
+
// TODO(burdon): Convert util below to `Effect.fn` (to preserve stack info)
|
|
76
|
+
export const formatUserPrompt = ({
|
|
77
|
+
prompt,
|
|
78
|
+
history = [],
|
|
79
|
+
}: Pick<AiSessionRunParams<any>, 'prompt' | 'history'>): Effect.Effect<Message.Message, AiSessionRunError> =>
|
|
80
|
+
Effect.gen(function* () {
|
|
81
|
+
const blocks: ContentBlock.Any[] = [];
|
|
82
|
+
|
|
83
|
+
// TODO(dmaretskyi): Evaluate other approaches as `serviceOption` isn't represented in the type system.
|
|
84
|
+
const artifactDiffResolver = yield* Effect.serviceOption(ArtifactDiffResolver);
|
|
85
|
+
if (Option.isSome(artifactDiffResolver)) {
|
|
86
|
+
const versions = gatherObjectVersions(history);
|
|
87
|
+
const artifactDiff = yield* Effect.tryPromise({
|
|
88
|
+
try: () =>
|
|
89
|
+
artifactDiffResolver.value.resolve(
|
|
90
|
+
[...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),
|
|
91
|
+
),
|
|
92
|
+
catch: AiAssistantError.wrap({ message: 'Artifact diff resolution error' }),
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
log('version', { artifactDiff, versions });
|
|
96
|
+
for (const [id, { version }] of [...artifactDiff.entries()]) {
|
|
97
|
+
if (ObjectVersion.equals(version, versions.get(id)!)) {
|
|
98
|
+
artifactDiff.delete(id);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
blocks.push({ _tag: 'anchor', objectId: id, version });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (artifactDiff.size > 0) {
|
|
106
|
+
blocks.push(createArtifactUpdateBlock(artifactDiff));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return Obj.make(Message.Message, {
|
|
111
|
+
created: new Date().toISOString(),
|
|
112
|
+
sender: { role: 'user' },
|
|
113
|
+
blocks: [...blocks, { _tag: 'text', text: prompt }],
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const gatherObjectVersions = (messages: Message.Message[]): Map<ObjectId, ObjectVersion> => {
|
|
118
|
+
const artifactIds = new Map<ObjectId, ObjectVersion>();
|
|
119
|
+
for (const message of messages) {
|
|
120
|
+
for (const block of message.blocks) {
|
|
121
|
+
if (block._tag === 'anchor') {
|
|
122
|
+
artifactIds.set(block.objectId, block.version as ObjectVersion);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return artifactIds;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const createArtifactUpdateBlock = (
|
|
131
|
+
artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
|
|
132
|
+
): ContentBlock.Any => {
|
|
133
|
+
return {
|
|
134
|
+
_tag: 'text',
|
|
135
|
+
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
136
|
+
disposition: 'artifact-update',
|
|
137
|
+
text: trim`
|
|
138
|
+
The following artifacts have been updated since the last message:
|
|
139
|
+
${[...artifactDiff.entries()]
|
|
140
|
+
.map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
|
|
141
|
+
.join('\n')}
|
|
142
|
+
`,
|
|
143
|
+
};
|
|
144
|
+
};
|
package/src/session/index.ts
CHANGED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Function from 'effect/Function';
|
|
7
|
+
|
|
8
|
+
import { type AiParser, type ConsolePrinter } from '@dxos/ai';
|
|
9
|
+
import { type Message } from '@dxos/types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Live observer of the generation process.
|
|
13
|
+
*/
|
|
14
|
+
export interface GenerationObserver extends AiParser.ParseResponseCallbacks {
|
|
15
|
+
/**
|
|
16
|
+
* Complete messages fired during the session, both from the model and from the user.
|
|
17
|
+
* This message will contain all message blocks emitted through the `onBlock` callback.
|
|
18
|
+
*/
|
|
19
|
+
onMessage: (message: Message.Message) => Effect.Effect<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const GenerationObserver = Object.freeze({
|
|
23
|
+
make: ({
|
|
24
|
+
onBegin = Function.constant(Effect.void),
|
|
25
|
+
onPart = Function.constant(Effect.void),
|
|
26
|
+
onBlock = Function.constant(Effect.void),
|
|
27
|
+
onEnd = Function.constant(Effect.void),
|
|
28
|
+
onMessage = Function.constant(Effect.void),
|
|
29
|
+
}: Partial<GenerationObserver> = {}): GenerationObserver => ({
|
|
30
|
+
onBegin,
|
|
31
|
+
onPart,
|
|
32
|
+
onBlock,
|
|
33
|
+
onEnd,
|
|
34
|
+
onMessage,
|
|
35
|
+
}),
|
|
36
|
+
|
|
37
|
+
noop: () => GenerationObserver.make(),
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Debug printer to be used in unit-tests and browser devtools.
|
|
41
|
+
*/
|
|
42
|
+
fromPrinter: (printer: ConsolePrinter) =>
|
|
43
|
+
GenerationObserver.make({
|
|
44
|
+
onBlock: (block) =>
|
|
45
|
+
Effect.sync(() => {
|
|
46
|
+
if (block.pending) {
|
|
47
|
+
return; // Only prints full blocks (better for unit-tests and browser devtools).
|
|
48
|
+
}
|
|
49
|
+
printer.printContentBlock(block);
|
|
50
|
+
}),
|
|
51
|
+
onMessage: (message) =>
|
|
52
|
+
Effect.sync(() => {
|
|
53
|
+
if (message.sender.role === 'assistant') {
|
|
54
|
+
return; // Skip assistant messages since they are printed in the `onBlock` callback.
|
|
55
|
+
}
|
|
56
|
+
printer.printMessage(message);
|
|
57
|
+
}),
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"conversations":[{"parameters":{"model":"@anthropic/claude-3-5-sonnet-20241022","stream":true,"tools":[]},"prompt":{"content":[{"role":"user","content":"Hello world!","options":{}}]},"response":[{"type":"response-metadata","id":"msg_01Bm496JGgDMgC9YwdYTFFHo","modelId":"claude-3-5-sonnet-20241022","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"text-start","id":"0","metadata":{}},{"type":"text-delta","id":"0","delta":"Hi","metadata":{}},{"type":"text-delta","id":"0","delta":"!","metadata":{}},{"type":"text-delta","id":"0","delta":" How can","metadata":{}},{"type":"text-delta","id":"0","delta":" I help you today?","metadata":{}},{"type":"text-end","id":"0","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":10,"outputTokens":12,"totalTokens":22},"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":10,"output_tokens":1,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-3-5-sonnet-20241022","stream":true,"tools":[{"name":"Calculator","description":"Basic calculator tool","inputSchema":{"type":"object","required":["input"],"properties":{"input":{"type":"string","description":"The calculation to perform."}},"additionalProperties":false}}]},"prompt":{"content":[{"role":"user","content":"What is 10 + 30?","options":{}}]},"response":[{"type":"response-metadata","id":"msg_01UNTbjtTHEpdFgGunu2k8Br","modelId":"claude-3-5-sonnet-20241022","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"text-start","id":"0","metadata":{}},{"type":"text-delta","id":"0","delta":"I","metadata":{}},{"type":"text-delta","id":"0","delta":"'ll help","metadata":{}},{"type":"text-delta","id":"0","delta":" you calculate","metadata":{}},{"type":"text-delta","id":"0","delta":" ","metadata":{}},{"type":"text-delta","id":"0","delta":"10 + 30","metadata":{}},{"type":"text-delta","id":"0","delta":" using the","metadata":{}},{"type":"text-delta","id":"0","delta":" Calculator tool.","metadata":{}},{"type":"text-end","id":"0","metadata":{}},{"type":"tool-params-start","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","name":"Calculator","providerExecuted":false,"metadata":{}},{"type":"tool-params-delta","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","delta":"","metadata":{}},{"type":"tool-params-delta","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","delta":"{\"input\": \"","metadata":{}},{"type":"tool-params-delta","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","delta":"10 + 30","metadata":{}},{"type":"tool-params-delta","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","delta":"\"}","metadata":{}},{"type":"tool-params-end","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","metadata":{}},{"type":"tool-call","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","name":"Calculator","params":{"input":"10 + 30"},"providerExecuted":false,"metadata":{}},{"type":"finish","reason":"tool-calls","usage":{"inputTokens":394,"outputTokens":73,"totalTokens":467},"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":394,"output_tokens":1,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-3-5-sonnet-20241022","stream":true,"tools":[{"name":"Calculator","description":"Basic calculator tool","inputSchema":{"type":"object","required":["input"],"properties":{"input":{"type":"string","description":"The calculation to perform."}},"additionalProperties":false}}]},"prompt":{"content":[{"role":"user","content":"What is 10 + 30?","options":{}},{"role":"assistant","content":[{"type":"text","text":"I'll help you calculate 10 + 30 using the Calculator tool.","options":{}},{"type":"tool-call","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","name":"Calculator","params":{"input":"10 + 30"},"providerExecuted":false,"options":{}}],"options":{}},{"role":"tool","content":[{"type":"tool-result","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","name":"Calculator","result":{"result":40},"options":{}}],"options":{}}]},"response":[{"type":"response-metadata","id":"msg_01AKio3GheJGn38BhvAp33rs","modelId":"claude-3-5-sonnet-20241022","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"text-start","id":"0","metadata":{}},{"type":"text-delta","id":"0","delta":"The result","metadata":{}},{"type":"text-delta","id":"0","delta":" of 10 +","metadata":{}},{"type":"text-delta","id":"0","delta":" 30 is ","metadata":{}},{"type":"text-delta","id":"0","delta":"40.","metadata":{}},{"type":"text-end","id":"0","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":482,"outputTokens":18,"totalTokens":500},"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":482,"output_tokens":3,"server_tool_use":null,"service_tier":"standard"}}}}]}]}
|