@dxos/assistant 0.8.4-main.c1de068 → 0.8.4-main.c85a9c8dae
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-BE6SRJ4N.mjs +193 -0
- package/dist/lib/neutral/chunk-BE6SRJ4N.mjs.map +7 -0
- package/dist/lib/neutral/extraction/index.mjs +223 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1328 -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 +38 -0
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +146 -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-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 +290 -237
- 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.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +349 -270
- package/dist/types/src/extraction/extraction.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 +17 -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 +3 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/artifact-diff.d.ts +27 -0
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
- package/dist/types/src/session/error-handling.d.ts +4 -0
- package/dist/types/src/session/error-handling.d.ts.map +1 -0
- package/dist/types/src/session/format.d.ts +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 +41 -240
- 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 +18 -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 +22 -0
- package/dist/types/src/testing/layer.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 +57 -35
- package/src/conversation/context.test.ts +46 -0
- package/src/conversation/context.ts +330 -0
- package/src/conversation/conversation.test.ts +65 -0
- package/src/conversation/conversation.ts +214 -63
- 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-llm-function/extraction-llm-function.test.ts +10 -9
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +48 -56
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +23 -22
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +42 -43
- 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 +10 -9
- package/src/extraction/quotes.test.ts +3 -3
- package/src/extraction/quotes.ts +6 -7
- package/src/functions/services.ts +165 -95
- package/src/functions.conversations.json +1 -0
- package/src/functions.test.ts +74 -0
- package/src/index.ts +3 -3
- package/src/session/artifact-diff.ts +41 -0
- package/src/session/error-handling.ts +41 -0
- package/src/session/format.test.ts +124 -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 +81 -200
- package/src/session/session.ts +176 -261
- 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 +48 -0
- package/src/templates/system.ts +41 -0
- package/src/testing/index.ts +2 -8
- package/src/testing/layer.ts +85 -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/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/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
|
@@ -0,0 +1,143 @@
|
|
|
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 { Template } from '@dxos/blueprints';
|
|
10
|
+
import { Obj } from '@dxos/echo';
|
|
11
|
+
import { ObjectVersion } from '@dxos/echo-db';
|
|
12
|
+
import { type ObjectId } from '@dxos/keys';
|
|
13
|
+
import { log } from '@dxos/log';
|
|
14
|
+
import { type ContentBlock, Message } from '@dxos/types';
|
|
15
|
+
import { trim } from '@dxos/util';
|
|
16
|
+
|
|
17
|
+
import { AiAssistantError } from '../errors';
|
|
18
|
+
|
|
19
|
+
import { ArtifactDiffResolver } from './artifact-diff';
|
|
20
|
+
import { type AiSessionRunError, type AiSessionRunProps } from './session';
|
|
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<AiSessionRunProps<any>, 'system' | 'blueprints' | 'objects'>) =>
|
|
31
|
+
Effect.gen(function* () {
|
|
32
|
+
const blueprintDefs = yield* Function.pipe(
|
|
33
|
+
blueprints,
|
|
34
|
+
Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)),
|
|
35
|
+
Effect.flatMap(
|
|
36
|
+
Effect.forEach((template) =>
|
|
37
|
+
Effect.gen(function* () {
|
|
38
|
+
return trim`
|
|
39
|
+
<blueprint>
|
|
40
|
+
${yield* Template.processTemplate(template)}
|
|
41
|
+
</blueprint>
|
|
42
|
+
`;
|
|
43
|
+
}),
|
|
44
|
+
),
|
|
45
|
+
),
|
|
46
|
+
Effect.map((blueprints) =>
|
|
47
|
+
blueprints.length > 0 ? ['## Blueprints Definitions', ...blueprints].join('\n\n') : undefined,
|
|
48
|
+
),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const objectDefs = yield* Function.pipe(
|
|
52
|
+
objects,
|
|
53
|
+
Effect.forEach((object) =>
|
|
54
|
+
Effect.succeed(trim`
|
|
55
|
+
<object>
|
|
56
|
+
<dxn>${Obj.getDXN(object)}</dxn>
|
|
57
|
+
<typename>${Obj.getTypename(object)}</typename>
|
|
58
|
+
</object>
|
|
59
|
+
`),
|
|
60
|
+
),
|
|
61
|
+
Effect.map((objects) => (objects.length > 0 ? ['## Context Objects', ...objects].join('\n\n') : undefined)),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return yield* Function.pipe(
|
|
65
|
+
Effect.succeed([system, blueprintDefs, objectDefs].filter((def): def is string => def !== undefined)),
|
|
66
|
+
Effect.map((parts) => parts.join('\n\n')),
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Formats the user prompt.
|
|
72
|
+
*/
|
|
73
|
+
// TODO(burdon): Move to AiPreprocessor.
|
|
74
|
+
// TODO(burdon): Convert util below to `Effect.fn` (to preserve stack info)
|
|
75
|
+
export const formatUserPrompt = ({
|
|
76
|
+
prompt,
|
|
77
|
+
history = [],
|
|
78
|
+
}: Pick<AiSessionRunProps<any>, 'prompt' | 'history'>): Effect.Effect<Message.Message, AiSessionRunError> =>
|
|
79
|
+
Effect.gen(function* () {
|
|
80
|
+
const blocks: ContentBlock.Any[] = [];
|
|
81
|
+
|
|
82
|
+
// TODO(dmaretskyi): Evaluate other approaches as `serviceOption` isn't represented in the type system.
|
|
83
|
+
const artifactDiffResolver = yield* Effect.serviceOption(ArtifactDiffResolver);
|
|
84
|
+
if (Option.isSome(artifactDiffResolver)) {
|
|
85
|
+
const versions = gatherObjectVersions(history);
|
|
86
|
+
const artifactDiff = yield* Effect.tryPromise({
|
|
87
|
+
try: () =>
|
|
88
|
+
artifactDiffResolver.value.resolve(
|
|
89
|
+
[...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),
|
|
90
|
+
),
|
|
91
|
+
catch: AiAssistantError.wrap({ message: 'Artifact diff resolution error' }),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
log('version', { artifactDiff, versions });
|
|
95
|
+
for (const [id, { version }] of [...artifactDiff.entries()]) {
|
|
96
|
+
if (ObjectVersion.equals(version, versions.get(id)!)) {
|
|
97
|
+
artifactDiff.delete(id);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
blocks.push({ _tag: 'anchor', objectId: id, version });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (artifactDiff.size > 0) {
|
|
105
|
+
blocks.push(createArtifactUpdateBlock(artifactDiff));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return Obj.make(Message.Message, {
|
|
110
|
+
created: new Date().toISOString(),
|
|
111
|
+
sender: { role: 'user' },
|
|
112
|
+
blocks: [...blocks, { _tag: 'text', text: prompt }],
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const gatherObjectVersions = (messages: Message.Message[]): Map<ObjectId, ObjectVersion> => {
|
|
117
|
+
const artifactIds = new Map<ObjectId, ObjectVersion>();
|
|
118
|
+
for (const message of messages) {
|
|
119
|
+
for (const block of message.blocks) {
|
|
120
|
+
if (block._tag === 'anchor') {
|
|
121
|
+
artifactIds.set(block.objectId, block.version as ObjectVersion);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return artifactIds;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const createArtifactUpdateBlock = (
|
|
130
|
+
artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
|
|
131
|
+
): ContentBlock.Any => {
|
|
132
|
+
return {
|
|
133
|
+
_tag: 'text',
|
|
134
|
+
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
135
|
+
disposition: 'artifact-update',
|
|
136
|
+
text: trim`
|
|
137
|
+
The following artifacts have been updated since the last message:
|
|
138
|
+
${[...artifactDiff.entries()]
|
|
139
|
+
.map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
|
|
140
|
+
.join('\n')}
|
|
141
|
+
`,
|
|
142
|
+
};
|
|
143
|
+
};
|
package/src/session/index.ts
CHANGED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as Tool from '@effect/ai/Tool';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Function from 'effect/Function';
|
|
8
|
+
|
|
9
|
+
import { type AiParser, type ConsolePrinter } from '@dxos/ai';
|
|
10
|
+
import { type Message } from '@dxos/types';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Live observer of the generation process.
|
|
14
|
+
*/
|
|
15
|
+
export interface GenerationObserver<
|
|
16
|
+
Tools extends Record<string, Tool.Any> = any,
|
|
17
|
+
> extends AiParser.ParseResponseCallbacks<Tools> {
|
|
18
|
+
/**
|
|
19
|
+
* Complete messages fired during the session, both from the model and from the user.
|
|
20
|
+
* This message will contain all message blocks emitted through the `onBlock` callback.
|
|
21
|
+
*/
|
|
22
|
+
onMessage: (message: Message.Message) => Effect.Effect<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const GenerationObserver = Object.freeze({
|
|
26
|
+
make: ({
|
|
27
|
+
onBegin = Function.constant(Effect.void),
|
|
28
|
+
onPart = Function.constant(Effect.void),
|
|
29
|
+
onBlock = Function.constant(Effect.void),
|
|
30
|
+
onEnd = Function.constant(Effect.void),
|
|
31
|
+
onMessage = Function.constant(Effect.void),
|
|
32
|
+
}: Partial<GenerationObserver> = {}): GenerationObserver => ({
|
|
33
|
+
onBegin,
|
|
34
|
+
onPart,
|
|
35
|
+
onBlock,
|
|
36
|
+
onEnd,
|
|
37
|
+
onMessage,
|
|
38
|
+
}),
|
|
39
|
+
|
|
40
|
+
noop: () => GenerationObserver.make(),
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Debug printer to be used in unit-tests and browser devtools.
|
|
44
|
+
*/
|
|
45
|
+
fromPrinter: (printer: ConsolePrinter) =>
|
|
46
|
+
GenerationObserver.make({
|
|
47
|
+
onBlock: (block) =>
|
|
48
|
+
Effect.sync(() => {
|
|
49
|
+
if (block.pending) {
|
|
50
|
+
return; // Only prints full blocks (better for unit-tests and browser devtools).
|
|
51
|
+
}
|
|
52
|
+
printer.printContentBlock(block);
|
|
53
|
+
}),
|
|
54
|
+
onMessage: (message) =>
|
|
55
|
+
Effect.sync(() => {
|
|
56
|
+
if (message.sender.role === 'assistant') {
|
|
57
|
+
return; // Skip assistant messages since they are printed in the `onBlock` callback.
|
|
58
|
+
}
|
|
59
|
+
printer.printMessage(message);
|
|
60
|
+
}),
|
|
61
|
+
}),
|
|
62
|
+
});
|