@dxos/assistant 0.8.4-main.67995b8 → 0.8.4-main.a4bbb77
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-ILL2CN3G.mjs +374 -0
- package/dist/lib/browser/chunk-ILL2CN3G.mjs.map +7 -0
- package/dist/lib/browser/{chunk-6WRGDEMH.mjs → chunk-UB26N5UF.mjs} +2 -2
- package/dist/lib/browser/chunk-UB26N5UF.mjs.map +7 -0
- package/dist/lib/browser/extraction/index.mjs +32 -29
- package/dist/lib/browser/extraction/index.mjs.map +4 -4
- package/dist/lib/browser/index.mjs +682 -1035
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/{named-entity-recognition-O46UTCLR.mjs → named-entity-recognition-QPYHML66.mjs} +2 -2
- package/dist/lib/node-esm/{chunk-3UU5V4FS.mjs → chunk-BCFXTNSK.mjs} +2 -2
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-GVKA7VL3.mjs +376 -0
- package/dist/lib/node-esm/chunk-GVKA7VL3.mjs.map +7 -0
- package/dist/lib/node-esm/extraction/index.mjs +32 -29
- package/dist/lib/node-esm/extraction/index.mjs.map +4 -4
- package/dist/lib/node-esm/index.mjs +682 -1035
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/{named-entity-recognition-W4VMRCH6.mjs → named-entity-recognition-2HDF46YX.mjs} +2 -2
- package/dist/types/src/{context/binding.d.ts → conversation/context.d.ts} +45 -3
- package/dist/types/src/conversation/context.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.d.ts +24 -21
- 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 +46 -9
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/execution-graph.d.ts +82 -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 +94 -55
- 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 +250 -216
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +15 -3
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -2
- 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 +184 -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 +3 -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 +23 -236
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +14 -0
- package/dist/types/src/session/toolkit.d.ts.map +1 -0
- package/dist/types/src/session/version-pin.d.ts +2 -2
- 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/types.d.ts +3 -0
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +34 -33
- package/src/conversation/context.ts +154 -0
- package/src/conversation/conversation.ts +76 -53
- package/src/conversation/index.ts +1 -0
- package/src/diff.test.ts +43 -0
- package/src/diff.ts +79 -0
- package/src/errors.ts +6 -1
- package/src/execution-graph.test.ts +738 -0
- package/src/execution-graph.ts +500 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +8 -7
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +12 -10
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +9 -9
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +13 -6
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +2 -1
- package/src/extraction/extraction-ner-function/named-entity-recognition.ts +2 -0
- package/src/extraction/quotes.test.ts +3 -3
- package/src/extraction/quotes.ts +2 -2
- package/src/functions/services.ts +97 -43
- package/src/index.ts +3 -2
- package/src/session/artifact-diff.ts +41 -0
- package/src/session/error-handling.ts +40 -0
- package/src/session/format.test.ts +60 -0
- package/src/session/format.ts +139 -0
- package/src/session/index.ts +3 -0
- package/src/session/observer.ts +58 -0
- package/src/session/session.conversations.json +1 -0
- package/src/session/session.test.ts +33 -197
- package/src/session/session.ts +119 -249
- package/src/session/toolkit.ts +46 -0
- package/src/session/version-pin.ts +4 -4
- package/src/summarization/summarization.test.ts +9 -7
- package/src/templates/format.tpl +60 -0
- package/src/templates/index.ts +1 -1
- package/src/templates/system.tpl +48 -0
- package/src/templates/system.ts +41 -0
- package/src/types.ts +10 -5
- package/src/typings.d.ts +5 -0
- package/dist/lib/browser/chunk-6WRGDEMH.mjs.map +0 -7
- package/dist/lib/browser/chunk-SIXTNSGV.mjs +0 -250
- package/dist/lib/browser/chunk-SIXTNSGV.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-3UU5V4FS.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-FD5NXKDP.mjs +0 -252
- package/dist/lib/node-esm/chunk-FD5NXKDP.mjs.map +0 -7
- 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.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/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/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/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/dist/lib/browser/{named-entity-recognition-O46UTCLR.mjs.map → named-entity-recognition-QPYHML66.mjs.map} +0 -0
- /package/dist/lib/node-esm/{named-entity-recognition-W4VMRCH6.mjs.map → named-entity-recognition-2HDF46YX.mjs.map} +0 -0
package/src/session/session.ts
CHANGED
|
@@ -2,44 +2,51 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import { Chunk,
|
|
7
|
-
import { Array, String } from 'effect';
|
|
5
|
+
import { type AiError, LanguageModel, type Tool, type Toolkit } from '@effect/ai';
|
|
6
|
+
import { Chunk, Effect, type Schema, Stream } from 'effect';
|
|
8
7
|
|
|
9
8
|
import {
|
|
10
9
|
AiParser,
|
|
11
10
|
AiPreprocessor,
|
|
12
|
-
getToolCalls,
|
|
13
|
-
runTool,
|
|
14
|
-
ToolExecutionService,
|
|
15
|
-
ToolResolverService,
|
|
16
|
-
type AgentStatus,
|
|
17
|
-
type AiInputPreprocessingError,
|
|
18
11
|
type AiToolNotFoundError,
|
|
19
|
-
type
|
|
12
|
+
type PromptPreprocessingError,
|
|
13
|
+
type ToolExecutionService,
|
|
14
|
+
type ToolResolverService,
|
|
15
|
+
callTool,
|
|
16
|
+
getToolCalls,
|
|
20
17
|
} from '@dxos/ai';
|
|
21
|
-
import { Event } from '@dxos/async';
|
|
22
18
|
import { type Blueprint } from '@dxos/blueprints';
|
|
23
19
|
import { todo } from '@dxos/debug';
|
|
24
20
|
import { Obj } from '@dxos/echo';
|
|
25
|
-
import {
|
|
26
|
-
import { type ObjectId } from '@dxos/echo-schema';
|
|
27
|
-
import { DatabaseService } from '@dxos/functions';
|
|
21
|
+
import { TracingService } from '@dxos/functions';
|
|
28
22
|
import { log } from '@dxos/log';
|
|
29
|
-
import { DataType
|
|
23
|
+
import { DataType } from '@dxos/schema';
|
|
30
24
|
|
|
31
|
-
import { AiAssistantError } from '../errors';
|
|
25
|
+
import { type AiAssistantError } from '../errors';
|
|
32
26
|
|
|
33
|
-
|
|
27
|
+
import { formatSystemPrompt, formatUserPrompt } from './format';
|
|
28
|
+
import { GenerationObserver } from './observer';
|
|
29
|
+
|
|
30
|
+
export type AiSessionRunError = AiError.AiError | PromptPreprocessingError | AiToolNotFoundError | AiAssistantError;
|
|
31
|
+
|
|
32
|
+
export type AiSessionRunRequirements =
|
|
33
|
+
| LanguageModel.LanguageModel
|
|
34
|
+
| ToolExecutionService
|
|
35
|
+
| ToolResolverService
|
|
36
|
+
| TracingService;
|
|
34
37
|
|
|
35
|
-
export type
|
|
38
|
+
export type AiSessionRunParams<Tools extends Record<string, Tool.Any>> = {
|
|
36
39
|
prompt: string;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
system?: string;
|
|
41
|
+
history?: DataType.Message[];
|
|
42
|
+
objects?: Obj.Any[];
|
|
40
43
|
blueprints?: Blueprint.Blueprint[];
|
|
44
|
+
toolkit?: Toolkit.WithHandler<Tools>;
|
|
45
|
+
observer?: GenerationObserver;
|
|
41
46
|
};
|
|
42
47
|
|
|
48
|
+
export type AiSessionOptions = {};
|
|
49
|
+
|
|
43
50
|
/**
|
|
44
51
|
* Contains message history, tools, current context.
|
|
45
52
|
* Current context means the state of the app, time of day, and other contextual information.
|
|
@@ -51,165 +58,130 @@ export type SessionRunOptions<Tools extends AiTool.Any> = {
|
|
|
51
58
|
* Could be personal or shared.
|
|
52
59
|
*/
|
|
53
60
|
export class AiSession {
|
|
54
|
-
|
|
61
|
+
/** Prevents concurrent execution of session. */
|
|
55
62
|
private readonly _semaphore = Effect.runSync(Effect.makeSemaphore(1));
|
|
56
63
|
|
|
57
|
-
/**
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
/** Current streaming response. */
|
|
61
|
-
private _stream: GenerationStream | undefined;
|
|
62
|
-
|
|
63
|
-
/** Prior history from queue. */
|
|
64
|
+
/** Message history from queue. */
|
|
65
|
+
// TODO(burdon): Evolve into supporting a git-like graph of messages.
|
|
64
66
|
private _history: DataType.Message[] = [];
|
|
65
67
|
|
|
66
|
-
/**
|
|
67
|
-
|
|
68
|
-
*/
|
|
69
|
-
public readonly message = new Event<DataType.Message>();
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Complete block added to Message.
|
|
73
|
-
*/
|
|
74
|
-
public readonly block = new Event<ContentBlock.Any>();
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Update partial block (while streaming).
|
|
78
|
-
*/
|
|
79
|
-
public readonly update = new Event<ContentBlock.Any>();
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Unparsed events from the underlying generation stream.
|
|
83
|
-
*/
|
|
84
|
-
public readonly streamEvent = new Event<AiResponse.Part>();
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* User prompt or tool result.
|
|
88
|
-
*/
|
|
89
|
-
public readonly userMessage = new Event<DataType.Message>();
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Agent self-reporting its status.
|
|
93
|
-
* Triggered by the model.
|
|
94
|
-
*/
|
|
95
|
-
public readonly statusReport = new Event<AgentStatus>();
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Emits when the session is done.
|
|
99
|
-
*/
|
|
100
|
-
public readonly done = new Event<void>();
|
|
68
|
+
/** Pending messages for this session (incl. the current prompt). */
|
|
69
|
+
private _pending: DataType.Message[] = [];
|
|
101
70
|
|
|
102
71
|
constructor(private readonly _options: AiSessionOptions = {}) {}
|
|
103
72
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
): Effect.Effect<
|
|
113
|
-
DataType.Message[],
|
|
114
|
-
AiAssistantError | AiInputPreprocessingError | AiToolNotFoundError | AiError.AiError,
|
|
115
|
-
AiLanguageModel.AiLanguageModel | ToolResolverService | ToolExecutionService | AiTool.ToHandler<Tools>
|
|
116
|
-
> =>
|
|
73
|
+
run = <Tools extends Record<string, Tool.Any>>({
|
|
74
|
+
prompt,
|
|
75
|
+
system: systemTemplate,
|
|
76
|
+
history = [],
|
|
77
|
+
objects = [],
|
|
78
|
+
blueprints = [],
|
|
79
|
+
toolkit,
|
|
80
|
+
observer = GenerationObserver.noop(),
|
|
81
|
+
}: AiSessionRunParams<Tools>): Effect.Effect<DataType.Message[], AiSessionRunError, AiSessionRunRequirements> =>
|
|
117
82
|
Effect.gen(this, function* () {
|
|
118
|
-
this._history = [...
|
|
83
|
+
this._history = [...history];
|
|
84
|
+
this._pending = [];
|
|
85
|
+
const pending = this._pending;
|
|
86
|
+
|
|
87
|
+
const submitMessage = Effect.fnUntraced(function* (message: DataType.Message) {
|
|
88
|
+
pending.push(message);
|
|
89
|
+
yield* observer.onMessage(message);
|
|
90
|
+
yield* TracingService.emitConverationMessage(message);
|
|
91
|
+
return message;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Submit the prompt.
|
|
95
|
+
// TODO(burdon): Remove if cancelled?
|
|
96
|
+
const promptMessage = yield* submitMessage(yield* formatUserPrompt({ prompt, history }));
|
|
119
97
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
this.userMessage.emit(promptMessages);
|
|
98
|
+
// Generate system and prompt messages.
|
|
99
|
+
const system = yield* formatSystemPrompt({ system: systemTemplate, blueprints, objects });
|
|
123
100
|
|
|
124
|
-
//
|
|
101
|
+
// Tool call loop.
|
|
125
102
|
do {
|
|
126
103
|
log('request', {
|
|
104
|
+
prompt: promptMessage,
|
|
105
|
+
system: { snippet: createSnippet(system), length: system.length },
|
|
127
106
|
pending: this._pending.length,
|
|
128
107
|
history: this._history.length,
|
|
129
|
-
|
|
108
|
+
objects: objects?.length ?? 0,
|
|
130
109
|
});
|
|
131
110
|
|
|
132
|
-
const prompt = yield* AiPreprocessor.
|
|
133
|
-
|
|
134
|
-
const blueprints = options.blueprints ?? [];
|
|
135
|
-
|
|
136
|
-
// Build system prompt from blueprint templates.
|
|
137
|
-
// TODO(dmaretskyi): Loading BP from the Database should be done at the higher level. We need a type for the resolved blueprint.
|
|
138
|
-
const systemPrompt = yield* pipe(
|
|
139
|
-
blueprints,
|
|
140
|
-
Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)),
|
|
141
|
-
Effect.flatMap(Effect.forEach((template) => DatabaseService.loadRef(template.source))),
|
|
142
|
-
Effect.map(Array.map((template) => `\n\n<blueprint>${template.content}</blueprint>`)),
|
|
143
|
-
Effect.map(Array.reduce(options.systemPrompt ?? '', String.concat)),
|
|
144
|
-
);
|
|
145
|
-
|
|
146
|
-
// Build a combined toolkit from the blueprint tools and the provided toolkit.
|
|
147
|
-
const blueprintToolkit = yield* ToolResolverService.resolveToolkit(blueprints.flatMap((bp) => bp.tools));
|
|
148
|
-
const blueprintToolkitHandler: Context.Context<AiTool.ToHandler<AiTool.Any>> =
|
|
149
|
-
yield* blueprintToolkit.toContext(yield* ToolExecutionService.handlersFor(blueprintToolkit));
|
|
150
|
-
const toolkit = options.toolkit != null ? AiToolkit.merge(options.toolkit, blueprintToolkit) : blueprintToolkit;
|
|
151
|
-
const toolkitWithBlueprintHandlers = yield* toolkit.pipe(
|
|
152
|
-
Effect.provide(blueprintToolkitHandler) as any,
|
|
153
|
-
) as Effect.Effect<AiToolkit.ToHandler<any>, never, AiTool.ToHandler<Tools>>;
|
|
111
|
+
const prompt = yield* AiPreprocessor.preprocessPrompt([...this._history, ...this._pending], { system });
|
|
154
112
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
const blocks = yield* AiLanguageModel.streamText({
|
|
113
|
+
// Execute the stream request.
|
|
114
|
+
// logDump('prompt', Prompt.Prompt.pipe(Schema.encodeSync)(prompt));
|
|
115
|
+
const blocks = yield* LanguageModel.streamText({
|
|
161
116
|
prompt,
|
|
162
|
-
|
|
163
|
-
toolkit: toolkitWithBlueprintHandlers,
|
|
117
|
+
toolkit,
|
|
164
118
|
disableToolCallResolution: true,
|
|
165
119
|
}).pipe(
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
this.block.emit(block);
|
|
173
|
-
}
|
|
174
|
-
}),
|
|
175
|
-
onPart: (part) =>
|
|
176
|
-
Effect.gen(this, function* () {
|
|
177
|
-
this.streamEvent.emit(part);
|
|
178
|
-
}),
|
|
120
|
+
// TOOD(dmaretskyi): Error mapping.
|
|
121
|
+
AiParser.parseResponse({
|
|
122
|
+
onBegin: () => observer.onBegin(),
|
|
123
|
+
onBlock: (block) => observer.onBlock(block),
|
|
124
|
+
onPart: (part) => observer.onPart(part),
|
|
125
|
+
onEnd: (summary) => observer.onEnd(summary),
|
|
179
126
|
}),
|
|
180
127
|
Stream.runCollect,
|
|
181
128
|
Effect.map(Chunk.toArray),
|
|
182
129
|
);
|
|
183
130
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
131
|
+
// Create the response message.
|
|
132
|
+
const response = yield* submitMessage(
|
|
133
|
+
Obj.make(DataType.Message, {
|
|
134
|
+
created: new Date().toISOString(),
|
|
135
|
+
sender: { role: 'assistant' },
|
|
136
|
+
blocks,
|
|
137
|
+
}),
|
|
138
|
+
);
|
|
191
139
|
|
|
140
|
+
// Parse the response for tool calls.
|
|
192
141
|
const toolCalls = getToolCalls(response);
|
|
193
142
|
if (toolCalls.length === 0) {
|
|
194
143
|
break;
|
|
144
|
+
} else if (!toolkit) {
|
|
145
|
+
throw new Error('No toolkit provided'); // TODO(burdon): Throw user error?
|
|
195
146
|
}
|
|
196
147
|
|
|
197
|
-
|
|
198
|
-
|
|
148
|
+
// Execute the tool calls.
|
|
149
|
+
// TODO(burdon): Retry errors? Write result when each completes individually?
|
|
150
|
+
const toolResults = yield* Effect.forEach(toolCalls, (toolCall) => {
|
|
151
|
+
return callTool(toolkit, toolCall).pipe(
|
|
152
|
+
Effect.provide(
|
|
153
|
+
TracingService.layerSubframe((context) => ({
|
|
154
|
+
...context,
|
|
155
|
+
parentMessage: response.id,
|
|
156
|
+
toolCallId: toolCall.toolCallId,
|
|
157
|
+
})),
|
|
158
|
+
),
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// Add to queue and continue loop.
|
|
163
|
+
// TODO(wittjosiah): Sometimes tool error results are added to the queue before the tool agent statuses.
|
|
164
|
+
// This results in a broken execution graph.
|
|
165
|
+
yield* submitMessage(
|
|
199
166
|
Obj.make(DataType.Message, {
|
|
200
167
|
created: new Date().toISOString(),
|
|
201
|
-
sender: { role: '
|
|
168
|
+
sender: { role: 'tool' },
|
|
202
169
|
blocks: toolResults,
|
|
203
170
|
}),
|
|
204
171
|
);
|
|
205
172
|
} while (true);
|
|
206
173
|
|
|
174
|
+
log('done', { pending: this._pending.length });
|
|
207
175
|
return this._pending;
|
|
208
176
|
}).pipe(this._semaphore.withPermits(1), Effect.withSpan('AiSession.run'));
|
|
209
177
|
|
|
178
|
+
/**
|
|
179
|
+
* @deprecated
|
|
180
|
+
*/
|
|
181
|
+
// TODO(burdon): Implement or remove.
|
|
210
182
|
async runStructured<S extends Schema.Schema.AnyNoContext>(
|
|
211
|
-
|
|
212
|
-
|
|
183
|
+
_schema: S,
|
|
184
|
+
_options: AiSessionRunParams<any>,
|
|
213
185
|
): Promise<Schema.Schema.Type<S>> {
|
|
214
186
|
return todo();
|
|
215
187
|
// const parser = structuredOutputParser(schema);
|
|
@@ -219,118 +191,16 @@ export class AiSession {
|
|
|
219
191
|
// });
|
|
220
192
|
// return parser.getResult(result);
|
|
221
193
|
}
|
|
222
|
-
|
|
223
|
-
private _formatUserPrompt = (prompt: string, history: DataType.Message[]) =>
|
|
224
|
-
Effect.gen(function* () {
|
|
225
|
-
const prelude: ContentBlock.Any[] = [];
|
|
226
|
-
|
|
227
|
-
// TODO(dmaretskyi): Evaluate other approaches as `serviceOption` isn't represented in the type system.
|
|
228
|
-
const artifactDiffResolver = yield* Effect.serviceOption(ArtifactDiffResolver);
|
|
229
|
-
if (Option.isSome(artifactDiffResolver)) {
|
|
230
|
-
const versions = gatherObjectVersions(history);
|
|
231
|
-
|
|
232
|
-
const artifactDiff = yield* Effect.tryPromise({
|
|
233
|
-
try: () =>
|
|
234
|
-
artifactDiffResolver.value.resolve(
|
|
235
|
-
[...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),
|
|
236
|
-
),
|
|
237
|
-
catch: AiAssistantError.wrap('Artifact diff resolution error'),
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
log.info('vision', {
|
|
241
|
-
artifactDiff,
|
|
242
|
-
versions,
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
for (const [id, { version }] of [...artifactDiff.entries()]) {
|
|
246
|
-
if (ObjectVersion.equals(version, versions.get(id)!)) {
|
|
247
|
-
artifactDiff.delete(id);
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
prelude.push({ _tag: 'anchor', objectId: id, version });
|
|
252
|
-
}
|
|
253
|
-
if (artifactDiff.size > 0) {
|
|
254
|
-
prelude.push(createArtifactUpdateBlock(artifactDiff));
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
return Obj.make(DataType.Message, {
|
|
259
|
-
created: new Date().toISOString(),
|
|
260
|
-
sender: { role: 'user' },
|
|
261
|
-
blocks: [...prelude, { _tag: 'text', text: prompt }],
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
abort(): void {
|
|
266
|
-
this._stream?.abort();
|
|
267
|
-
}
|
|
268
194
|
}
|
|
269
195
|
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
for (const message of messages) {
|
|
273
|
-
for (const block of message.blocks) {
|
|
274
|
-
if (block._tag === 'anchor') {
|
|
275
|
-
artifactIds.set(block.objectId, block.version as ObjectVersion);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
196
|
+
const createSnippet = (text: string, len = 32) =>
|
|
197
|
+
text.length <= len * 2 ? text : [text.slice(0, len), '...', text.slice(-len)].join('');
|
|
279
198
|
|
|
280
|
-
|
|
199
|
+
// TODO(dmaretskyi): Extract as a general util.
|
|
200
|
+
const logDump = (message: string, data: unknown) => {
|
|
201
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
202
|
+
const { writeFileSync } = require('node:fs');
|
|
203
|
+
const path = `/tmp/log-data-${Date.now()}.json`;
|
|
204
|
+
writeFileSync(path, JSON.stringify(data, null, 2));
|
|
205
|
+
log.info(message, { path });
|
|
281
206
|
};
|
|
282
|
-
|
|
283
|
-
const createArtifactUpdateBlock = (
|
|
284
|
-
artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
|
|
285
|
-
): ContentBlock.Any => {
|
|
286
|
-
return {
|
|
287
|
-
_tag: 'text',
|
|
288
|
-
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
289
|
-
disposition: 'artifact-update',
|
|
290
|
-
text: `
|
|
291
|
-
The following artifacts have been updated since the last message:
|
|
292
|
-
${[...artifactDiff.entries()]
|
|
293
|
-
.map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
|
|
294
|
-
.join('\n')}
|
|
295
|
-
`,
|
|
296
|
-
};
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Resolves artifact ids to their versions.
|
|
301
|
-
* Used to give the model a sense of the changes to the artifacts made by users during the conversation.
|
|
302
|
-
* The artifacts versions are pinned in the history, and whenever the artifact changes in-between assistant's steps,
|
|
303
|
-
* a diff is inserted into the conversation.
|
|
304
|
-
*
|
|
305
|
-
* Can be optionally provided to the session run call.
|
|
306
|
-
*/
|
|
307
|
-
// TODO(dmaretskyi): Convert to Context.Reference
|
|
308
|
-
export class ArtifactDiffResolver extends Context.Tag('@dxos/assistant/ArtifactDiffResolver')<
|
|
309
|
-
ArtifactDiffResolver,
|
|
310
|
-
ArtifactDiffResolver.Service
|
|
311
|
-
>() {}
|
|
312
|
-
|
|
313
|
-
export namespace ArtifactDiffResolver {
|
|
314
|
-
export type Service = {
|
|
315
|
-
resolve: (artifacts: { id: ObjectId; lastVersion: ObjectVersion }[]) => Promise<
|
|
316
|
-
Map<
|
|
317
|
-
ObjectId,
|
|
318
|
-
{
|
|
319
|
-
version: ObjectVersion;
|
|
320
|
-
diff?: string;
|
|
321
|
-
}
|
|
322
|
-
>
|
|
323
|
-
>;
|
|
324
|
-
};
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
const runTools: <Tools extends AiTool.Any>(
|
|
328
|
-
toolCalls: ContentBlock.ToolCall[],
|
|
329
|
-
toolkit: AiToolkit.AiToolkit<Tools>,
|
|
330
|
-
) => Effect.Effect<ContentBlock.ToolResult[], AiError.AiError, AiTool.ToHandler<Tools>> = Effect.fn('runTools')(
|
|
331
|
-
function* (toolCalls, toolkit) {
|
|
332
|
-
const toolkitWithHandlers = Effect.isEffect(toolkit) ? yield* toolkit : toolkit;
|
|
333
|
-
|
|
334
|
-
return yield* Effect.forEach(toolCalls, (toolCall) => runTool(toolkitWithHandlers, toolCall));
|
|
335
|
-
},
|
|
336
|
-
);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Tool, Toolkit } from '@effect/ai';
|
|
6
|
+
import { type Context, Effect } from 'effect';
|
|
7
|
+
|
|
8
|
+
import { type AiToolNotFoundError, ToolExecutionService, type ToolId, ToolResolverService } from '@dxos/ai';
|
|
9
|
+
import { type Blueprint } from '@dxos/blueprints';
|
|
10
|
+
import { isTruthy } from '@dxos/util';
|
|
11
|
+
|
|
12
|
+
export type ToolkitParams<Tools extends Record<string, Tool.Any>> = {
|
|
13
|
+
toolkit?: Toolkit.Toolkit<Tools>;
|
|
14
|
+
toolIds?: ToolId[];
|
|
15
|
+
blueprints?: Blueprint.Blueprint[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Build a combined toolkit from the blueprint tools and the provided toolkit.
|
|
20
|
+
*/
|
|
21
|
+
export const createToolkit = <Tools extends Record<string, Tool.Any> = {}>({
|
|
22
|
+
toolkit: toolkitParam,
|
|
23
|
+
toolIds = [],
|
|
24
|
+
blueprints = [],
|
|
25
|
+
}: ToolkitParams<Tools>): Effect.Effect<
|
|
26
|
+
Toolkit.WithHandler<any>,
|
|
27
|
+
AiToolNotFoundError,
|
|
28
|
+
ToolResolverService | ToolExecutionService | Tool.HandlersFor<Tools>
|
|
29
|
+
> =>
|
|
30
|
+
Effect.gen(function* () {
|
|
31
|
+
const blueprintToolkit = yield* ToolResolverService.resolveToolkit([
|
|
32
|
+
...blueprints.flatMap(({ tools }) => tools),
|
|
33
|
+
...toolIds,
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
const blueprintToolkitHandler: Context.Context<Tool.HandlersFor<any>> = yield* blueprintToolkit.toContext(
|
|
37
|
+
ToolExecutionService.handlersFor(blueprintToolkit),
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const toolkit = Toolkit.merge(...[toolkitParam, blueprintToolkit].filter(isTruthy));
|
|
41
|
+
return yield* toolkit.pipe(Effect.provide(blueprintToolkitHandler)) as any as Effect.Effect<
|
|
42
|
+
Toolkit.WithHandler<any>,
|
|
43
|
+
never,
|
|
44
|
+
Tool.HandlersFor<Tools>
|
|
45
|
+
>;
|
|
46
|
+
});
|
|
@@ -6,7 +6,7 @@ import { Schema } from 'effect';
|
|
|
6
6
|
|
|
7
7
|
import { type ObjectVersion } from '@dxos/echo-db';
|
|
8
8
|
import { getVersion } from '@dxos/echo-db';
|
|
9
|
-
import {
|
|
9
|
+
import { type AnyEchoObject, ObjectId } from '@dxos/echo-schema';
|
|
10
10
|
import { type ContentBlock } from '@dxos/schema';
|
|
11
11
|
|
|
12
12
|
// TODO(dmaretskyi): Extract.
|
|
@@ -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: AnyEchoObject) => 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: AnyEchoObject): VersionPin {
|
|
35
35
|
return VersionPin.make({
|
|
36
36
|
objectId: object.id,
|
|
37
37
|
version: getVersion(object),
|
|
@@ -2,24 +2,24 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
//
|
|
5
|
+
// ISSUE(burdon): defineFunction
|
|
6
6
|
// @ts-nocheck
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import { Schema, pipe } from 'effect';
|
|
9
9
|
import { beforeAll, describe, test } from 'vitest';
|
|
10
10
|
|
|
11
11
|
import { AiService, MixedStreamParser } from '@dxos/ai';
|
|
12
12
|
import { Obj } from '@dxos/echo';
|
|
13
13
|
import { type EchoDatabase } from '@dxos/echo-db';
|
|
14
14
|
import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
15
|
-
import {
|
|
15
|
+
import { FunctionExecutor, ServiceContainer, defineFunction } from '@dxos/functions';
|
|
16
16
|
import { type ContentBlock, DataType } from '@dxos/schema';
|
|
17
17
|
import { createTestData } from '@dxos/schema/testing';
|
|
18
18
|
import { trim } from '@dxos/util';
|
|
19
19
|
|
|
20
|
-
const REMOTE_AI = true;
|
|
21
|
-
|
|
22
20
|
const summarizationFn = defineFunction({
|
|
21
|
+
key: 'dxos.org/function/summarization/summarize',
|
|
22
|
+
name: 'Summarize transcript',
|
|
23
23
|
description: 'Summarize a document',
|
|
24
24
|
inputSchema: Schema.Struct({
|
|
25
25
|
document: Schema.optional(DataType.Text),
|
|
@@ -27,7 +27,7 @@ const summarizationFn = defineFunction({
|
|
|
27
27
|
}),
|
|
28
28
|
outputSchema: DataType.Text,
|
|
29
29
|
handler: async ({ data: { document, transcript }, context }) => {
|
|
30
|
-
const ai = context.getService(AiService);
|
|
30
|
+
const ai = context.getService(AiService.AiService);
|
|
31
31
|
const result = await new MixedStreamParser().parse(
|
|
32
32
|
await ai.client.execStream({
|
|
33
33
|
model: '@anthropic/claude-3-5-haiku-20241022',
|
|
@@ -84,6 +84,8 @@ const summarizationFn = defineFunction({
|
|
|
84
84
|
});
|
|
85
85
|
|
|
86
86
|
const refinementFn = defineFunction({
|
|
87
|
+
key: 'dxos.org/function/summarization/refine',
|
|
88
|
+
name: 'Refine transcript summary',
|
|
87
89
|
description: 'Refine a summary',
|
|
88
90
|
inputSchema: Schema.Struct({
|
|
89
91
|
summaries: Schema.Array(DataType.Text),
|
|
@@ -92,7 +94,7 @@ const refinementFn = defineFunction({
|
|
|
92
94
|
summary: DataType.Text,
|
|
93
95
|
}),
|
|
94
96
|
handler: async ({ data: { summaries }, context }) => {
|
|
95
|
-
const ai = context.getService(AiService);
|
|
97
|
+
const ai = context.getService(AiService.AiService);
|
|
96
98
|
const result = await new MixedStreamParser().parse(
|
|
97
99
|
await ai.client.execStream({
|
|
98
100
|
model: '@anthropic/claude-3-5-haiku-20241022',
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{{! System Core }}
|
|
2
|
+
|
|
3
|
+
## Input Formats
|
|
4
|
+
|
|
5
|
+
### Blueprints
|
|
6
|
+
|
|
7
|
+
You may be provided with Blueprint definitions that include specific instructions and tools for a given set of tasks.
|
|
8
|
+
Blue print instructions will be enclosed in a <blueprint> tag.
|
|
9
|
+
|
|
10
|
+
### Context objects
|
|
11
|
+
|
|
12
|
+
You may be provided with references to objects that are relevant to the current prompt.
|
|
13
|
+
Objects will be in the form of a <object> tag with a <dxn> element that is the unique key of the object required by many tools.
|
|
14
|
+
The <typename> element is the type of the object.
|
|
15
|
+
|
|
16
|
+
<object>
|
|
17
|
+
<dxn>object-id</dxn>
|
|
18
|
+
<typename>example.com/type/TypeName</typename>
|
|
19
|
+
</object>
|
|
20
|
+
|
|
21
|
+
## Response Formats
|
|
22
|
+
|
|
23
|
+
It is very important to respond in the correct format.
|
|
24
|
+
You response must be valid Markdown.
|
|
25
|
+
In some cases you should respond with well-formed XML tags along with the content.
|
|
26
|
+
Code snippets should use 2-space indents.
|
|
27
|
+
|
|
28
|
+
{{#if cot}}
|
|
29
|
+
Before responding, explain your reasoning and include your detailed chain-of-thought enclosed in a <cot> tag.
|
|
30
|
+
The <cot> tag should be the first thing in your response.
|
|
31
|
+
{{/if}}
|
|
32
|
+
|
|
33
|
+
## Block references
|
|
34
|
+
|
|
35
|
+
You can reply with a block reference for objects if you have the DXN. For example:
|
|
36
|
+
|
|
37
|
+
<object>
|
|
38
|
+
<dxn>dxn:queue:data:B6INSIBY3CBEF4M5VZRYBCMAHQMPYK5AJ:01K24XMVHSZHS97SG1VTVQDM5Z:01K24XPK464FSCKVQJAB2H662M</dxn>
|
|
39
|
+
</object>
|
|
40
|
+
<object>
|
|
41
|
+
<dxn>dxn:echo:@:01K24XPK464FSCKVQJAB2H662M</dxn>
|
|
42
|
+
</object>
|
|
43
|
+
|
|
44
|
+
### Suggestions
|
|
45
|
+
|
|
46
|
+
Each suggestion must in the form of a proposed action enclosed in a <suggestion> tag.
|
|
47
|
+
Example: <suggestion>Show on a map</suggestion>
|
|
48
|
+
|
|
49
|
+
### Multiple choice questions
|
|
50
|
+
|
|
51
|
+
You may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.
|
|
52
|
+
Example: <select><option>Yes</option><option>No</option></select>
|
|
53
|
+
|
|
54
|
+
## Toolkit
|
|
55
|
+
|
|
56
|
+
If the user asks for a list of tools, then just emit a single self-closing <toolkit/> tag instead of listing the tools.
|
|
57
|
+
|
|
58
|
+
## Context
|
|
59
|
+
|
|
60
|
+
The current date and time is {{DATETIME}}.
|
package/src/templates/index.ts
CHANGED