@dxos/assistant 0.8.4-main.b97322e → 0.8.4-main.bc2380dfbc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +102 -5
- package/README.md +2 -1
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/neutral/extraction/index.mjs +204 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1755 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts +0 -1
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +1066 -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 +15 -4
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -5
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/request/AiRequest.d.ts +104 -0
- package/dist/types/src/request/AiRequest.d.ts.map +1 -0
- package/dist/types/src/request/artifact-diff.d.ts +27 -0
- package/dist/types/src/request/artifact-diff.d.ts.map +1 -0
- package/dist/types/src/request/format.d.ts +15 -0
- package/dist/types/src/request/format.d.ts.map +1 -0
- package/dist/types/src/request/index.d.ts +4 -0
- package/dist/types/src/request/index.d.ts.map +1 -0
- package/dist/types/src/request/observer.d.ts +23 -0
- package/dist/types/src/request/observer.d.ts.map +1 -0
- package/dist/types/src/{session → request}/version-pin.d.ts +4 -4
- package/dist/types/src/request/version-pin.d.ts.map +1 -0
- package/dist/types/src/session/AiContext.d.ts +154 -0
- package/dist/types/src/session/AiContext.d.ts.map +1 -0
- package/dist/types/src/session/AiContext.test.d.ts +2 -0
- package/dist/types/src/session/AiContext.test.d.ts.map +1 -0
- package/dist/types/src/session/AiSession.d.ts +73 -0
- package/dist/types/src/session/AiSession.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +3 -2
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +16 -0
- package/dist/types/src/session/toolkit.d.ts.map +1 -0
- package/dist/types/src/templates/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/util/artifact.d.ts +27 -0
- package/dist/types/src/util/artifact.d.ts.map +1 -0
- package/dist/types/src/util/diff.d.ts +17 -0
- package/dist/types/src/util/diff.d.ts.map +1 -0
- package/dist/types/src/util/diff.test.d.ts +2 -0
- package/dist/types/src/util/diff.test.d.ts.map +1 -0
- package/dist/types/src/util/error-handling.d.ts +4 -0
- package/dist/types/src/util/error-handling.d.ts.map +1 -0
- package/dist/types/src/util/errors.d.ts +67 -0
- package/dist/types/src/util/errors.d.ts.map +1 -0
- package/dist/types/src/util/execution-graph.d.ts +85 -0
- package/dist/types/src/util/execution-graph.d.ts.map +1 -0
- package/dist/types/src/util/execution-graph.test.d.ts +2 -0
- package/dist/types/src/util/execution-graph.test.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +7 -0
- package/dist/types/src/util/index.d.ts.map +1 -0
- package/dist/types/src/util/tracing.d.ts +233 -0
- package/dist/types/src/util/tracing.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +42 -36
- 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 +173 -98
- package/src/index.ts +2 -5
- package/src/request/AiRequest.ts +348 -0
- package/src/request/artifact-diff.ts +41 -0
- package/src/request/format.ts +147 -0
- package/src/request/index.ts +8 -0
- package/src/request/observer.ts +62 -0
- package/src/{session → request}/version-pin.ts +8 -8
- package/src/session/AiContext.test.ts +53 -0
- package/src/session/AiContext.ts +403 -0
- package/src/session/AiSession.ts +324 -0
- package/src/session/index.ts +4 -2
- package/src/session/toolkit.ts +47 -0
- package/src/templates/index.ts +1 -1
- 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/typings.d.ts +5 -0
- package/src/util/artifact.ts +88 -0
- package/src/util/diff.test.ts +74 -0
- package/src/util/diff.ts +93 -0
- package/src/util/error-handling.ts +42 -0
- package/src/util/errors.ts +15 -0
- package/src/util/execution-graph.test.ts +740 -0
- package/src/util/execution-graph.ts +519 -0
- package/src/util/index.ts +10 -0
- package/src/util/tracing.ts +57 -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/conversation/conversation.d.ts +0 -38
- package/dist/types/src/conversation/conversation.d.ts.map +0 -1
- package/dist/types/src/conversation/index.d.ts +0 -2
- package/dist/types/src/conversation/index.d.ts.map +0 -1
- package/dist/types/src/errors.d.ts +0 -38
- package/dist/types/src/errors.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +0 -4
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +0 -2
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts +0 -2
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +0 -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/session.d.ts +0 -260
- package/dist/types/src/session/session.d.ts.map +0 -1
- package/dist/types/src/session/session.test.d.ts +0 -2
- package/dist/types/src/session/session.test.d.ts.map +0 -1
- package/dist/types/src/session/version-pin.d.ts.map +0 -1
- package/dist/types/src/session/version-pin.test.d.ts +0 -2
- package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
- package/dist/types/src/summarization/summarization.test.d.ts +0 -2
- package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
- package/dist/types/src/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/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 -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/conversation/conversation.ts +0 -96
- package/src/conversation/index.ts +0 -5
- package/src/errors.ts +0 -10
- 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/session.test.ts +0 -310
- package/src/session/session.ts +0 -336
- 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/testing/index.ts +0 -11
- 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
|
@@ -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,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 '../util';
|
|
19
|
+
import type * as AiRequest from './AiRequest';
|
|
20
|
+
import { ArtifactDiffResolver } from './artifact-diff';
|
|
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<AiRequest.RunProps, '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<AiRequest.RunProps, 'prompt' | 'history'>): Effect.Effect<Message.Message, AiRequest.RunError> =>
|
|
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
|
+
};
|
|
@@ -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
|
+
});
|
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
|
-
import { type
|
|
8
|
-
import { getVersion } from '@dxos/echo-db';
|
|
9
|
-
import { ObjectId
|
|
10
|
-
import { type ContentBlock } from '@dxos/
|
|
7
|
+
import { type Obj } from '@dxos/echo';
|
|
8
|
+
import { type ObjectVersion, getVersion } from '@dxos/echo-db';
|
|
9
|
+
import { ObjectId } from '@dxos/keys';
|
|
10
|
+
import { type ContentBlock } from '@dxos/types';
|
|
11
11
|
|
|
12
12
|
// TODO(dmaretskyi): Extract.
|
|
13
13
|
const ObjectVersionSchema = Schema.Unknown as Schema.Schema<ObjectVersion>;
|
|
@@ -15,7 +15,7 @@ const ObjectVersionSchema = Schema.Unknown as Schema.Schema<ObjectVersion>;
|
|
|
15
15
|
const VersionPinSchema = Schema.Struct({
|
|
16
16
|
// TODO(dmaretskyi): Use Ref when those support encoding.
|
|
17
17
|
objectId: ObjectId,
|
|
18
|
-
// TODO(dmaretskyi): Could be opaque
|
|
18
|
+
// TODO(dmaretskyi): Could be opaque.
|
|
19
19
|
version: ObjectVersionSchema,
|
|
20
20
|
});
|
|
21
21
|
|
|
@@ -27,11 +27,11 @@ export interface VersionPin extends Schema.Schema.Type<typeof VersionPinSchema>
|
|
|
27
27
|
|
|
28
28
|
export const VersionPin: typeof VersionPinSchema & {
|
|
29
29
|
DISPOSITION: 'version-pin';
|
|
30
|
-
fromObject: (object:
|
|
30
|
+
fromObject: (object: Obj.Unknown) => VersionPin;
|
|
31
31
|
createBlock: (pin: VersionPin) => ContentBlock.Any;
|
|
32
32
|
} = class extends VersionPinSchema {
|
|
33
33
|
static readonly DISPOSITION = 'version-pin';
|
|
34
|
-
static fromObject(object:
|
|
34
|
+
static fromObject(object: Obj.Unknown): VersionPin {
|
|
35
35
|
return VersionPin.make({
|
|
36
36
|
objectId: object.id,
|
|
37
37
|
version: getVersion(object),
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Context from 'effect/Context';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Layer from 'effect/Layer';
|
|
8
|
+
import * as Runtime from 'effect/Runtime';
|
|
9
|
+
import * as Schema from 'effect/Schema';
|
|
10
|
+
import { describe, it } from 'vitest';
|
|
11
|
+
|
|
12
|
+
import { Feed, Obj, Ref, Type } from '@dxos/echo';
|
|
13
|
+
|
|
14
|
+
import * as AiContext from './AiContext';
|
|
15
|
+
|
|
16
|
+
const createMockFeedRuntime = (): Runtime.Runtime<Feed.FeedService> => {
|
|
17
|
+
const mockFeedService: Context.Tag.Service<Feed.FeedService> = {
|
|
18
|
+
append: async () => {},
|
|
19
|
+
remove: async () => {},
|
|
20
|
+
query: () =>
|
|
21
|
+
({
|
|
22
|
+
subscribe: () => () => {},
|
|
23
|
+
results: [],
|
|
24
|
+
run: async () => [],
|
|
25
|
+
}) as any,
|
|
26
|
+
sync: async () => {},
|
|
27
|
+
};
|
|
28
|
+
const layer = Layer.succeed(Feed.FeedService, mockFeedService);
|
|
29
|
+
return Effect.runSync(Effect.runtime<Feed.FeedService>().pipe(Effect.provide(layer)));
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
describe('AiContext.Binder', () => {
|
|
33
|
+
it('should handle bind with Ref', async () => {
|
|
34
|
+
const feed = Feed.make();
|
|
35
|
+
const runtime = createMockFeedRuntime();
|
|
36
|
+
const binder = new AiContext.Binder({ feed, runtime });
|
|
37
|
+
|
|
38
|
+
const TestSchema = Schema.Struct({}).pipe(
|
|
39
|
+
Type.object({
|
|
40
|
+
typename: 'org.dxos.type.example',
|
|
41
|
+
version: '0.1.0',
|
|
42
|
+
}),
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const obj = Obj.make(TestSchema, {});
|
|
46
|
+
const ref = Ref.make(obj);
|
|
47
|
+
|
|
48
|
+
await binder.bind({
|
|
49
|
+
blueprints: [],
|
|
50
|
+
objects: [ref],
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|