@dxos/assistant 0.8.4-main.dedc0f3 → 0.8.4-main.dfabb4ec29
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/extraction/index.mjs +202 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1736 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/types/src/conversation/context.d.ts +124 -64
- package/dist/types/src/conversation/context.d.ts.map +1 -1
- 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/index.d.ts +1 -1
- package/dist/types/src/conversation/index.d.ts.map +1 -1
- package/dist/types/src/conversation/session.d.ts +73 -0
- package/dist/types/src/conversation/session.d.ts.map +1 -0
- package/dist/types/src/diff.d.ts +3 -1
- package/dist/types/src/diff.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +40 -48
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/execution-graph.d.ts +85 -0
- package/dist/types/src/execution-graph.d.ts.map +1 -0
- package/dist/types/src/execution-graph.test.d.ts +2 -0
- package/dist/types/src/execution-graph.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts +0 -1
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +1066 -303
- 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 -5
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/artifact-diff.d.ts +2 -2
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
- package/dist/types/src/session/error-handling.d.ts +2 -2
- package/dist/types/src/session/error-handling.d.ts.map +1 -1
- package/dist/types/src/session/format.d.ts +8 -175
- package/dist/types/src/session/format.d.ts.map +1 -1
- package/dist/types/src/session/index.d.ts +2 -1
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +7 -6
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/request.d.ts +104 -0
- package/dist/types/src/session/request.d.ts.map +1 -0
- package/dist/types/src/session/toolkit.d.ts +11 -9
- package/dist/types/src/session/toolkit.d.ts.map +1 -1
- 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/system.d.ts +1 -2
- package/dist/types/src/templates/system.d.ts.map +1 -1
- package/dist/types/src/tracing.d.ts +233 -0
- package/dist/types/src/tracing.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts +28 -0
- package/dist/types/src/types/artifact.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +2 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +35 -46
- package/src/conversation/context.test.ts +52 -0
- package/src/conversation/context.ts +324 -75
- package/src/conversation/index.ts +1 -1
- package/src/conversation/session.ts +323 -0
- package/src/diff.test.ts +37 -6
- package/src/diff.ts +18 -4
- package/src/errors.ts +4 -4
- package/src/execution-graph.test.ts +740 -0
- package/src/execution-graph.ts +519 -0
- package/src/extraction/extraction-ner-function/index.ts +0 -1
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
- package/src/extraction/extraction.ts +44 -15
- package/src/extraction/index.ts +0 -1
- package/src/extraction/quotes.test.ts +2 -2
- package/src/extraction/quotes.ts +6 -7
- package/src/functions/services.ts +156 -61
- package/src/index.ts +3 -1
- package/src/session/artifact-diff.ts +2 -2
- package/src/session/error-handling.ts +11 -6
- package/src/session/format.ts +35 -27
- package/src/session/index.ts +2 -1
- package/src/session/observer.ts +8 -4
- package/src/session/request.ts +347 -0
- package/src/session/toolkit.ts +32 -27
- package/src/session/version-pin.ts +7 -7
- package/src/templates/{format.tpl → instructions/format.tpl} +1 -4
- package/src/templates/instructions/system.tpl +96 -0
- package/src/templates/system.ts +9 -8
- package/src/tracing.ts +57 -0
- package/src/{types.ts → types/artifact.ts} +44 -7
- package/src/types/index.ts +5 -0
- package/dist/lib/browser/chunk-5GJYG2ML.mjs +0 -396
- package/dist/lib/browser/chunk-5GJYG2ML.mjs.map +0 -7
- package/dist/lib/browser/chunk-UB26N5UF.mjs +0 -89
- package/dist/lib/browser/chunk-UB26N5UF.mjs.map +0 -7
- package/dist/lib/browser/extraction/index.mjs +0 -268
- package/dist/lib/browser/extraction/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -469
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +0 -13
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +0 -91
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-FLFCEB4P.mjs +0 -398
- package/dist/lib/node-esm/chunk-FLFCEB4P.mjs.map +0 -7
- package/dist/lib/node-esm/extraction/index.mjs +0 -269
- package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -470
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +0 -7
- package/dist/types/src/conversation/conversation.d.ts +0 -42
- package/dist/types/src/conversation/conversation.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 -337
- 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/session/format.test.d.ts +0 -2
- package/dist/types/src/session/format.test.d.ts.map +0 -1
- package/dist/types/src/session/session.d.ts +0 -47
- 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.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/types.d.ts +0 -16
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/conversation/conversation.ts +0 -123
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -84
- 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 -62
- package/src/session/__snapshots__/session.test.ts.snap +0 -32
- package/src/session/format.test.ts +0 -60
- package/src/session/session.test.ts +0 -308
- package/src/session/session.ts +0 -201
- package/src/session/version-pin.test.ts +0 -7
- package/src/summarization/summarization.test.ts +0 -197
- package/src/templates/system.tpl +0 -50
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as AiError from '@effect/ai/AiError';
|
|
6
|
+
import * as LanguageModel from '@effect/ai/LanguageModel';
|
|
7
|
+
import * as Array from 'effect/Array';
|
|
8
|
+
import * as Chunk from 'effect/Chunk';
|
|
9
|
+
import * as Effect from 'effect/Effect';
|
|
10
|
+
import { pipe } from 'effect/Function';
|
|
11
|
+
import * as Option from 'effect/Option';
|
|
12
|
+
import * as Stream from 'effect/Stream';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
AiParser,
|
|
16
|
+
AiPreprocessor,
|
|
17
|
+
AiSummarizer,
|
|
18
|
+
type AiToolNotFoundError,
|
|
19
|
+
callTool,
|
|
20
|
+
type OpaqueToolkit,
|
|
21
|
+
type PromptPreprocessingError,
|
|
22
|
+
type ToolExecutionService,
|
|
23
|
+
type ToolResolverService,
|
|
24
|
+
withoutToolCallParising,
|
|
25
|
+
} from '@dxos/ai';
|
|
26
|
+
import { type Blueprint, Trace, Operation, OperationRegistry } from '@dxos/compute';
|
|
27
|
+
import { Database, Obj } from '@dxos/echo';
|
|
28
|
+
import { log } from '@dxos/log';
|
|
29
|
+
import { ContentBlock, Message } from '@dxos/types';
|
|
30
|
+
|
|
31
|
+
import { type AiAssistantError } from '../errors';
|
|
32
|
+
import { CompleteBlock, PartialBlock } from '../tracing';
|
|
33
|
+
import { formatSystemPrompt, formatUserPrompt } from './format';
|
|
34
|
+
import { GenerationObserver } from './observer';
|
|
35
|
+
|
|
36
|
+
export type AiRequestRunError = AiError.AiError | PromptPreprocessingError | AiToolNotFoundError | AiAssistantError;
|
|
37
|
+
|
|
38
|
+
export type AiRequestRunRequirements =
|
|
39
|
+
| LanguageModel.LanguageModel
|
|
40
|
+
| ToolExecutionService
|
|
41
|
+
| ToolResolverService
|
|
42
|
+
| Database.Service
|
|
43
|
+
| Operation.Service
|
|
44
|
+
| OperationRegistry.Service
|
|
45
|
+
| Trace.TraceService;
|
|
46
|
+
|
|
47
|
+
export type AiRequestOptions = {
|
|
48
|
+
/**
|
|
49
|
+
* Summarize before executing the prompt if the existing history exceeds this threshold.
|
|
50
|
+
*/
|
|
51
|
+
summarizationThreshold?: number;
|
|
52
|
+
|
|
53
|
+
// TODO(dmaretskyi): Plan to phase out in favor of TracingService and the return type being a stream.
|
|
54
|
+
observer?: GenerationObserver;
|
|
55
|
+
/**
|
|
56
|
+
* Callback for when a message is received from the user, model, or tool.
|
|
57
|
+
* This is useful for streaming the output to a queue.
|
|
58
|
+
*/
|
|
59
|
+
onOutput?: (message: Message.Message) => Effect.Effect<void, never, never>;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type AiRequestRunProps<R = never> = {
|
|
63
|
+
prompt: string | ContentBlock.Any[];
|
|
64
|
+
// TODO(wittjosiah): Rename to systemPrompt.
|
|
65
|
+
system?: string;
|
|
66
|
+
history?: Message.Message[];
|
|
67
|
+
objects?: Obj.Unknown[];
|
|
68
|
+
blueprints?: readonly Blueprint.Blueprint[];
|
|
69
|
+
toolkit?: OpaqueToolkit.OpaqueToolkit<R>;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export type AiRequestBeginProps = {
|
|
73
|
+
prompt: string | ContentBlock.Any[];
|
|
74
|
+
system?: string;
|
|
75
|
+
history?: Message.Message[];
|
|
76
|
+
objects?: Obj.Unknown[];
|
|
77
|
+
blueprints?: readonly Blueprint.Blueprint[];
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export type AiRequestTurnProps<R = never> = {
|
|
81
|
+
system: string;
|
|
82
|
+
toolkit?: OpaqueToolkit.OpaqueToolkit<R>;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export type AiRequestTurnResult = {
|
|
86
|
+
messages: Message.Message[];
|
|
87
|
+
done: boolean;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Contains message history, tools, current context.
|
|
92
|
+
* Current context means the state of the app, time of day, and other contextual information.
|
|
93
|
+
* It makes requests to the model, its a state machine.
|
|
94
|
+
* It keeps track of the current goal.
|
|
95
|
+
* It manages the context window.
|
|
96
|
+
* Tracks the success criteria of reaching the goal, exposing metrics (stretch).
|
|
97
|
+
* Could be run locally in the app or remotely.
|
|
98
|
+
* Could be personal or shared.
|
|
99
|
+
*/
|
|
100
|
+
export class AiRequest {
|
|
101
|
+
/** Prevents concurrent execution of session. */
|
|
102
|
+
private readonly _semaphore = Effect.runSync(Effect.makeSemaphore(1));
|
|
103
|
+
|
|
104
|
+
private readonly _observer: GenerationObserver;
|
|
105
|
+
private readonly _onOutput: (message: Message.Message) => Effect.Effect<void, never, never>;
|
|
106
|
+
|
|
107
|
+
/** Message history from queue. */
|
|
108
|
+
// TODO(burdon): Evolve into supporting a git-like graph of messages.
|
|
109
|
+
private _history: Message.Message[] = [];
|
|
110
|
+
|
|
111
|
+
/** Pending messages for this session (incl. the current prompt). */
|
|
112
|
+
private _pending: Message.Message[] = [];
|
|
113
|
+
|
|
114
|
+
private _started = 0;
|
|
115
|
+
private _ended = 0;
|
|
116
|
+
private _toolCalls = 0;
|
|
117
|
+
|
|
118
|
+
constructor(private readonly _options: AiRequestOptions = {}) {
|
|
119
|
+
this._observer = _options.observer ?? GenerationObserver.noop();
|
|
120
|
+
this._onOutput = _options.onOutput ?? (() => Effect.void);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
get duration(): number {
|
|
124
|
+
return this._ended - this._started;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
get toolCalls(): number {
|
|
128
|
+
return this._toolCalls;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
get pending(): readonly Message.Message[] {
|
|
132
|
+
return this._pending;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private _submitMessage = (message: Message.Message): Effect.Effect<Message.Message, never, Trace.TraceService> =>
|
|
136
|
+
Effect.gen(this, function* () {
|
|
137
|
+
this._pending.push(message);
|
|
138
|
+
yield* this._observer.onMessage(message);
|
|
139
|
+
for (const block of message.blocks) {
|
|
140
|
+
log('write complete block', {
|
|
141
|
+
messageId: message.id,
|
|
142
|
+
role: message.sender.role!,
|
|
143
|
+
block: JSON.stringify(block),
|
|
144
|
+
});
|
|
145
|
+
yield* Trace.write(CompleteBlock, {
|
|
146
|
+
messageId: message.id,
|
|
147
|
+
role: message.sender.role!,
|
|
148
|
+
block,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
yield* this._onOutput(message);
|
|
152
|
+
return message;
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
getToolCalls = () =>
|
|
156
|
+
pipe(
|
|
157
|
+
[...this._history, ...this._pending],
|
|
158
|
+
Array.reverse,
|
|
159
|
+
Array.takeWhile((_) => _.sender.role === 'assistant'),
|
|
160
|
+
Array.flatMap((_) => _.blocks.filter(ContentBlock.is('toolCall')).map((block) => ({ block, message: _ }))),
|
|
161
|
+
Array.filter((_) => !_.block.providerExecuted),
|
|
162
|
+
Array.reverse,
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Initialize a session: set up history, perform summarization if needed, and submit the user prompt.
|
|
167
|
+
* Must be called before `runTurn()`.
|
|
168
|
+
*/
|
|
169
|
+
begin = ({
|
|
170
|
+
prompt,
|
|
171
|
+
system,
|
|
172
|
+
history = [],
|
|
173
|
+
blueprints = [],
|
|
174
|
+
objects = [],
|
|
175
|
+
}: AiRequestBeginProps): Effect.Effect<void, AiRequestRunError, AiRequestRunRequirements> =>
|
|
176
|
+
Effect.gen(this, function* () {
|
|
177
|
+
this._started = Date.now();
|
|
178
|
+
this._history = [...history];
|
|
179
|
+
this._pending = [];
|
|
180
|
+
|
|
181
|
+
const systemPrompt = yield* formatSystemPrompt({ system, blueprints, objects }).pipe(Effect.orDie);
|
|
182
|
+
|
|
183
|
+
if (this._options.summarizationThreshold !== undefined) {
|
|
184
|
+
const tokenCount = yield* AiPreprocessor.estimateTokens(
|
|
185
|
+
yield* AiPreprocessor.preprocessPrompt([...this._history], {
|
|
186
|
+
system: systemPrompt,
|
|
187
|
+
}),
|
|
188
|
+
);
|
|
189
|
+
if (tokenCount > this._options.summarizationThreshold) {
|
|
190
|
+
const summary = yield* AiSummarizer.summarize([...this._history]);
|
|
191
|
+
yield* this._submitMessage(summary);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
yield* this._submitMessage(yield* formatUserPrompt({ prompt, history }));
|
|
196
|
+
}).pipe(Effect.withSpan('AiRequest.begin'));
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Execute a single turn: one LLM generation followed by tool execution.
|
|
200
|
+
* The toolkit and system prompt can be updated between turns to reflect context changes (e.g. dynamically enabled blueprints).
|
|
201
|
+
*/
|
|
202
|
+
runAgentTurn = <const R = never>({
|
|
203
|
+
system,
|
|
204
|
+
toolkit: opaqueToolkit,
|
|
205
|
+
}: AiRequestTurnProps<R>): Effect.Effect<AiRequestTurnResult, AiRequestRunError, AiRequestRunRequirements | R> =>
|
|
206
|
+
Effect.gen(this, function* () {
|
|
207
|
+
log.info('request', {
|
|
208
|
+
system: { snippet: createSnippet(system), length: system.length },
|
|
209
|
+
pending: this._pending.length,
|
|
210
|
+
history: this._history.length,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
const prompt = yield* AiPreprocessor.preprocessPrompt([...this._history, ...this._pending], {
|
|
214
|
+
system,
|
|
215
|
+
cacheControl: 'ephemeral',
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
const toolkit = opaqueToolkit ? yield* opaqueToolkit.handlers : undefined;
|
|
219
|
+
|
|
220
|
+
const observer = this._observer;
|
|
221
|
+
let currentMessageId: Obj.ID | null = null;
|
|
222
|
+
|
|
223
|
+
const messages = yield* LanguageModel.streamText({
|
|
224
|
+
prompt,
|
|
225
|
+
toolkit,
|
|
226
|
+
disableToolCallResolution: true,
|
|
227
|
+
}).pipe(
|
|
228
|
+
withoutToolCallParising,
|
|
229
|
+
AiParser.parseResponse({
|
|
230
|
+
emitPartial: true,
|
|
231
|
+
onBegin: () => observer.onBegin(),
|
|
232
|
+
onBlock: (block) => observer.onBlock(block),
|
|
233
|
+
onPart: (part) => observer.onPart(part as any),
|
|
234
|
+
onEnd: (summary) => observer.onEnd(summary),
|
|
235
|
+
}),
|
|
236
|
+
Stream.mapEffect(
|
|
237
|
+
(block) =>
|
|
238
|
+
Effect.gen(this, function* () {
|
|
239
|
+
if (block.pending) {
|
|
240
|
+
currentMessageId ??= Obj.ID.random();
|
|
241
|
+
log('emit ephemeral message', { id: currentMessageId, type: block._tag });
|
|
242
|
+
yield* Trace.write(PartialBlock, {
|
|
243
|
+
messageId: currentMessageId,
|
|
244
|
+
role: 'assistant',
|
|
245
|
+
block,
|
|
246
|
+
});
|
|
247
|
+
return Option.none();
|
|
248
|
+
} else {
|
|
249
|
+
currentMessageId ??= Obj.ID.random();
|
|
250
|
+
const id = currentMessageId;
|
|
251
|
+
currentMessageId = null;
|
|
252
|
+
log('emit complete message', { id, type: block._tag });
|
|
253
|
+
const message = Obj.make(Message.Message, {
|
|
254
|
+
id,
|
|
255
|
+
created: new Date().toISOString(),
|
|
256
|
+
sender: { role: 'assistant' },
|
|
257
|
+
blocks: [block],
|
|
258
|
+
});
|
|
259
|
+
return Option.some(yield* this._submitMessage(message));
|
|
260
|
+
}
|
|
261
|
+
}),
|
|
262
|
+
{ concurrency: 1, unordered: false },
|
|
263
|
+
),
|
|
264
|
+
Stream.filterMap((_) => _),
|
|
265
|
+
Stream.runCollect,
|
|
266
|
+
Effect.map(Chunk.toArray),
|
|
267
|
+
);
|
|
268
|
+
log.info('messages', { messages });
|
|
269
|
+
|
|
270
|
+
const toolCalls = this.getToolCalls();
|
|
271
|
+
|
|
272
|
+
if (toolCalls.length === 0) {
|
|
273
|
+
this._ended = Date.now();
|
|
274
|
+
return { messages, done: true };
|
|
275
|
+
} else if (!toolkit) {
|
|
276
|
+
throw new Error('No toolkit provided');
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return { messages, done: false };
|
|
280
|
+
}).pipe(Effect.withSpan('AiRequest.runAgentTurn'));
|
|
281
|
+
|
|
282
|
+
runTools = <const R = never>({
|
|
283
|
+
toolkit: opaqueToolkit,
|
|
284
|
+
}: {
|
|
285
|
+
toolkit?: OpaqueToolkit.OpaqueToolkit<R>;
|
|
286
|
+
}): Effect.Effect<void, AiRequestRunError, AiRequestRunRequirements | R> =>
|
|
287
|
+
Effect.gen(this, function* () {
|
|
288
|
+
const toolkit = opaqueToolkit ? yield* opaqueToolkit.handlers : undefined;
|
|
289
|
+
const toolCalls = this.getToolCalls();
|
|
290
|
+
const toolResults = yield* Effect.forEach(toolCalls, ({ block, message }) => {
|
|
291
|
+
if (!toolkit) {
|
|
292
|
+
throw new Error('No toolkit provided');
|
|
293
|
+
}
|
|
294
|
+
return callTool(toolkit, block);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
yield* this._submitMessage(
|
|
298
|
+
Obj.make(Message.Message, {
|
|
299
|
+
created: new Date().toISOString(),
|
|
300
|
+
sender: { role: 'tool' },
|
|
301
|
+
blocks: toolResults,
|
|
302
|
+
}),
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
this._toolCalls += toolResults.length;
|
|
306
|
+
}).pipe(Effect.withSpan('AiRequest.runTools'));
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Run a full conversation turn loop. Equivalent to calling `begin()` then `runTurn()` in a loop.
|
|
310
|
+
*/
|
|
311
|
+
run = <const R = never>({
|
|
312
|
+
prompt,
|
|
313
|
+
system: systemTemplate,
|
|
314
|
+
history = [],
|
|
315
|
+
objects = [],
|
|
316
|
+
blueprints = [],
|
|
317
|
+
toolkit,
|
|
318
|
+
}: AiRequestRunProps<R>): Effect.Effect<Message.Message[], AiRequestRunError, AiRequestRunRequirements | R> =>
|
|
319
|
+
Effect.gen(this, function* () {
|
|
320
|
+
yield* this.begin({ prompt, system: systemTemplate, history, objects, blueprints });
|
|
321
|
+
|
|
322
|
+
const system = yield* formatSystemPrompt({ system: systemTemplate, blueprints, objects }).pipe(Effect.orDie);
|
|
323
|
+
|
|
324
|
+
do {
|
|
325
|
+
const { done } = yield* this.runAgentTurn({ system, toolkit });
|
|
326
|
+
if (done) {
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
yield* this.runTools({ toolkit });
|
|
330
|
+
} while (true);
|
|
331
|
+
|
|
332
|
+
log('done', { pending: this._pending.length, duration: this.duration, tools: this._toolCalls });
|
|
333
|
+
return this._pending;
|
|
334
|
+
}).pipe(this._semaphore.withPermits(1), Effect.withSpan('AiRequest.run'));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const createSnippet = (text: string, len = 32) =>
|
|
338
|
+
text.length <= len * 2 ? text : [text.slice(0, len), '...', text.slice(-len)].join('');
|
|
339
|
+
|
|
340
|
+
// TODO(dmaretskyi): Extract as a general util.
|
|
341
|
+
const logDump = (message: string, data: unknown) => {
|
|
342
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
343
|
+
const { writeFileSync } = require('node:fs');
|
|
344
|
+
const path = `/tmp/log-data-${Date.now()}.json`;
|
|
345
|
+
writeFileSync(path, JSON.stringify(data, null, 2));
|
|
346
|
+
log.info(message, { path });
|
|
347
|
+
};
|
package/src/session/toolkit.ts
CHANGED
|
@@ -2,41 +2,46 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
5
|
+
import * as Toolkit from '@effect/ai/Toolkit';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Layer from 'effect/Layer';
|
|
7
8
|
|
|
8
|
-
import {
|
|
9
|
-
import { type Blueprint } from '@dxos/
|
|
10
|
-
import {
|
|
9
|
+
import { type AiToolNotFoundError, OpaqueToolkit, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
10
|
+
import { type Blueprint } from '@dxos/compute';
|
|
11
|
+
import { isTruthy } from '@dxos/util';
|
|
11
12
|
|
|
12
|
-
export type
|
|
13
|
-
toolkit?:
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
export type CreateToolkitProps = {
|
|
14
|
+
toolkit?: OpaqueToolkit.Any;
|
|
15
|
+
blueprints?: readonly Blueprint.Blueprint[];
|
|
16
|
+
/**
|
|
17
|
+
* Self-contained with handlers toolkits.
|
|
18
|
+
*/
|
|
19
|
+
opaqueToolkits?: readonly OpaqueToolkit.Any[];
|
|
16
20
|
};
|
|
17
21
|
|
|
18
22
|
/**
|
|
19
23
|
* Build a combined toolkit from the blueprint tools and the provided toolkit.
|
|
20
24
|
*/
|
|
21
|
-
export const createToolkit =
|
|
22
|
-
toolkit:
|
|
23
|
-
toolIds = [],
|
|
25
|
+
export const createToolkit = ({
|
|
26
|
+
toolkit: toolkitProp,
|
|
24
27
|
blueprints = [],
|
|
25
|
-
|
|
28
|
+
opaqueToolkits = [],
|
|
29
|
+
}: CreateToolkitProps): Effect.Effect<
|
|
30
|
+
OpaqueToolkit.OpaqueToolkit,
|
|
31
|
+
AiToolNotFoundError,
|
|
32
|
+
ToolResolverService | ToolExecutionService
|
|
33
|
+
> =>
|
|
26
34
|
Effect.gen(function* () {
|
|
27
|
-
const blueprintToolkit = yield* ToolResolverService.resolveToolkit(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
]);
|
|
35
|
+
const blueprintToolkit = yield* ToolResolverService.resolveToolkit(blueprints.flatMap(({ tools }) => tools));
|
|
36
|
+
const blueprintToolHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));
|
|
37
|
+
const opaqueToolkit = OpaqueToolkit.merge(...opaqueToolkits);
|
|
31
38
|
|
|
32
|
-
const
|
|
33
|
-
|
|
39
|
+
const toolkitDefs = [toolkitProp?.toolkit, blueprintToolkit, opaqueToolkit.toolkit].filter(isTruthy);
|
|
40
|
+
const mergedToolkit = Toolkit.merge(...toolkitDefs);
|
|
41
|
+
const combinedHandlerLayer = Layer.mergeAll(
|
|
42
|
+
Layer.succeedContext(blueprintToolHandler),
|
|
43
|
+
toolkitProp?.layer ?? OpaqueToolkit.empty.layer,
|
|
44
|
+
opaqueToolkit.layer,
|
|
34
45
|
);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return yield* toolkit.pipe(Effect.provide(blueprintToolkitHandler)) as Effect.Effect<
|
|
38
|
-
AiToolkit.ToHandler<any>,
|
|
39
|
-
never,
|
|
40
|
-
AiTool.ToHandler<Tools>
|
|
41
|
-
>;
|
|
42
|
-
});
|
|
46
|
+
return OpaqueToolkit.make(mergedToolkit, combinedHandlerLayer as any) as OpaqueToolkit.OpaqueToolkit;
|
|
47
|
+
}) as Effect.Effect<OpaqueToolkit.OpaqueToolkit, AiToolNotFoundError, ToolResolverService | ToolExecutionService>;
|
|
@@ -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 {
|
|
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>;
|
|
@@ -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),
|
|
@@ -45,16 +45,13 @@ You can reply with a block reference for objects if you have the DXN. For exampl
|
|
|
45
45
|
|
|
46
46
|
Each suggestion must in the form of a proposed action enclosed in a <suggestion> tag.
|
|
47
47
|
Example: <suggestion>Show on a map</suggestion>
|
|
48
|
+
Do not use bullets when listing suggestions.
|
|
48
49
|
|
|
49
50
|
### Multiple choice questions
|
|
50
51
|
|
|
51
52
|
You may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.
|
|
52
53
|
Example: <select><option>Yes</option><option>No</option></select>
|
|
53
54
|
|
|
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
55
|
## Context
|
|
59
56
|
|
|
60
57
|
The current date and time is {{DATETIME}}.
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
{{! System Prompt }}
|
|
2
|
+
|
|
3
|
+
- You are a helpful assistant called Kai.
|
|
4
|
+
- Your name is Kai and you are powered by different language models.
|
|
5
|
+
- You were created by DXOS and operate inside of Composer and on the DXOS EDGE network.
|
|
6
|
+
- In your initial greeting state your name and make some suggestions based on the current context objects.
|
|
7
|
+
- You are an advanced AI assistant capable of creating and managing artifacts from provided data and tools.
|
|
8
|
+
- Your task is to process user commands and questions and decide how best to respond.
|
|
9
|
+
- In general be concise and direct.
|
|
10
|
+
- Follow all instructions carefully.
|
|
11
|
+
|
|
12
|
+
## Planning
|
|
13
|
+
|
|
14
|
+
- Analyze the structure and type of the content in the user's message.
|
|
15
|
+
- Determine if you complete the task using the available blueprint definitions?
|
|
16
|
+
- If you can't complete the task using the available blueprint definitions, query the list of available blueprint definitions using the appropriate tool.
|
|
17
|
+
- Identify which blueprint definitions are relevant to the user's request.
|
|
18
|
+
- Evaluate the potential benefits of creating an artifact vs. normal processing for each identified element.
|
|
19
|
+
- Make a final decision on whether to create an artifact and explain your reasoning.
|
|
20
|
+
- Are the required blueprint definitions already available?
|
|
21
|
+
- If not, select which blueprint definition(s) will be the most relevant and require them using the require_artifact_definitions tool.
|
|
22
|
+
- The require'd artifact tools will be available for use after require.
|
|
23
|
+
- If creating an artifact, outline how you will structure it within the response.
|
|
24
|
+
- Decide if the artifact needs to be shown to the user.
|
|
25
|
+
- Call the show tool to show the artifact to the user.
|
|
26
|
+
- Your reasoning must include: whether to use artifacts or not, to create one or query, whether to show the artifact to the user, and how to structure the response.
|
|
27
|
+
|
|
28
|
+
## Blueprints and Artifacts
|
|
29
|
+
|
|
30
|
+
- Determine if the interaction involves an artifact. Prefer artifacts for tables, lists, spreadsheets, kanbans, games, images, and other structured data.
|
|
31
|
+
- Determine if the user is explicitly talking about creating a new artifact, or wants to use an existing artifact.
|
|
32
|
+
- If it is ambiguous, query for existing artifacts first and then decide.
|
|
33
|
+
- If you decide to create an artifact, call the associated tool to create the artifact.
|
|
34
|
+
- Artifacts are stored in the database. Tools are used to create and query artifacts.
|
|
35
|
+
- If you are unsure about creating an artifact ask the user for clarification.
|
|
36
|
+
- Artifacts are mutable objects that can change over the course of the conversation.
|
|
37
|
+
- Always re-query the artifact using the tool (like query or inspect) to get the latest state of the artifact before answering the user.
|
|
38
|
+
- You must never generate the id of the artifact; only recall the ids that are already in the history.
|
|
39
|
+
- Artifacts are created by requiring the specific artifact using the require_artifact tool and creating it by calling the associated tool.
|
|
40
|
+
- You can add suggested actions at the end of your response.
|
|
41
|
+
- Suggested actions should be very concise and start with a verb and be phrased as a command to an agent -- not a question to the user.
|
|
42
|
+
- Suggested actions must be in the form of a user instruction that you can follow.
|
|
43
|
+
- Suggested actions could include actions that create artifacts.
|
|
44
|
+
- After creating an artifact use a tool to add it to the chat context.
|
|
45
|
+
|
|
46
|
+
## Navigation
|
|
47
|
+
|
|
48
|
+
- When the user asks to open, go to, navigate to, or show a specific document, page, or object, use the navigation tools instead of loading content inline.
|
|
49
|
+
- First, call the Resolve navigation targets tool to find the navigation path for the requested item.
|
|
50
|
+
- Without arguments, it returns available pages that can be navigated to.
|
|
51
|
+
- With a DXN in the query, it resolves that specific object to a navigation path.
|
|
52
|
+
- If you know the object's DXN (e.g. from a database query or context objects), pass it in the query to get the exact navigation path.
|
|
53
|
+
- Then, call the Open tool with the resolved navigation path(s) in the subject array to navigate the user to that item.
|
|
54
|
+
- Do not respond with the document's content when the user asks to open it — open it in the main content area instead.
|
|
55
|
+
- If multiple targets match, present the options to the user and let them choose which to open.
|
|
56
|
+
|
|
57
|
+
## Response format
|
|
58
|
+
|
|
59
|
+
- NEVER output narration text outside `<status>` tags or the final user-facing answer. Any progress update, transition sentence, or "let me…" lead-in MUST be wrapped in `<status>...</status>`. Violating this is an error.
|
|
60
|
+
- When updating the user about the progress of the work you are doing, put the update in a <status> XML tag.
|
|
61
|
+
- Only when you need to show the result to the user or ask a question use a text block without status tags.
|
|
62
|
+
- After a `</status>` tag, the next output must be either another `<status>` tag, a tool call, or the final user-facing answer — never a transition sentence.
|
|
63
|
+
|
|
64
|
+
<example>
|
|
65
|
+
// reasoning...
|
|
66
|
+
<status>Looking for emails from Depot</status>
|
|
67
|
+
// tool call..
|
|
68
|
+
<status>Found 2 emails from Depot.</status>
|
|
69
|
+
<status>Loading email details</status>
|
|
70
|
+
// tool call
|
|
71
|
+
<status>Located mailbox</status>
|
|
72
|
+
I found **2 emails from Depot**:
|
|
73
|
+
</example>
|
|
74
|
+
|
|
75
|
+
Do not do (no status tags):
|
|
76
|
+
|
|
77
|
+
<incorrect_example>
|
|
78
|
+
// reasoning...
|
|
79
|
+
// tool call..
|
|
80
|
+
I found 2 emails from Depot. Let me load their details:
|
|
81
|
+
// tool call
|
|
82
|
+
I found **2 emails from Depot**:
|
|
83
|
+
</incorrect_example>
|
|
84
|
+
|
|
85
|
+
Do not do (transition sentence after a status tag, before tool calls):
|
|
86
|
+
|
|
87
|
+
<incorrect_example>
|
|
88
|
+
<status>Doing X</status>
|
|
89
|
+
Let me start by doing X:
|
|
90
|
+
// tool call
|
|
91
|
+
</incorrect_example>
|
|
92
|
+
|
|
93
|
+
- NEVER write a lead-in sentence before a tool call. Use a `<status>` tag instead.
|
|
94
|
+
- Avoid using plain text to tell the user what you are about to do; instead, use status blocks.
|
|
95
|
+
- WRONG: Let me get...
|
|
96
|
+
- CORRECT: <status>Getting...</status>
|
package/src/templates/system.ts
CHANGED
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
import defaultsDeep from 'lodash.defaultsdeep';
|
|
6
6
|
|
|
7
7
|
import { type ModelCapabilities } from '@dxos/ai';
|
|
8
|
-
import { Template } from '@dxos/
|
|
8
|
+
import { Template } from '@dxos/compute';
|
|
9
9
|
|
|
10
|
-
import FORMAT from './format.tpl?raw';
|
|
11
|
-
import SYSTEM from './system.tpl?raw';
|
|
10
|
+
import FORMAT from './instructions/format.tpl?raw';
|
|
11
|
+
import SYSTEM from './instructions/system.tpl?raw';
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* System template variables.
|
|
@@ -29,13 +29,14 @@ export const createSystemPrompt = (options: Partial<SystemPromptOptions> = {}) =
|
|
|
29
29
|
}),
|
|
30
30
|
);
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
|
|
34
|
+
*/
|
|
35
|
+
const SYSTEM_ID = '01K2J2FZWH39F55CS7B26GP1Q3';
|
|
36
|
+
|
|
32
37
|
export const templates = {
|
|
33
|
-
/** Editable system prompt.*/
|
|
34
38
|
system: Template.make({
|
|
35
|
-
|
|
36
|
-
* NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
|
|
37
|
-
*/
|
|
38
|
-
id: '01K2J2FZWH39F55CS7B26GP1Q3',
|
|
39
|
+
id: SYSTEM_ID,
|
|
39
40
|
source: SYSTEM,
|
|
40
41
|
}),
|
|
41
42
|
};
|
package/src/tracing.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
|
|
7
|
+
import { Trace } from '@dxos/compute';
|
|
8
|
+
import { Obj } from '@dxos/echo';
|
|
9
|
+
import { ContentBlock, Actor } from '@dxos/types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Partial content block emitted.
|
|
13
|
+
*/
|
|
14
|
+
export const PartialBlock = Trace.EventType('assistant.partialBlock', {
|
|
15
|
+
schema: Schema.Struct({
|
|
16
|
+
messageId: Obj.ID,
|
|
17
|
+
role: Actor.Role,
|
|
18
|
+
block: ContentBlock.Any,
|
|
19
|
+
}),
|
|
20
|
+
isEphemeral: true,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Complete content block emitted.
|
|
25
|
+
*/
|
|
26
|
+
export const CompleteBlock = Trace.EventType('assistant.completeBlock', {
|
|
27
|
+
schema: Schema.Struct({
|
|
28
|
+
messageId: Obj.ID,
|
|
29
|
+
role: Actor.Role,
|
|
30
|
+
block: ContentBlock.Any,
|
|
31
|
+
}),
|
|
32
|
+
isEphemeral: false,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export const AgentRequestBegin = Trace.EventType('assistant.agentRequestBegin', {
|
|
36
|
+
schema: Schema.Struct({}),
|
|
37
|
+
isEphemeral: false,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
export const AgentRequestEnd = Trace.EventType('assistant.agentRequestEnd', {
|
|
41
|
+
schema: Schema.Struct({}),
|
|
42
|
+
isEphemeral: false,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Emitted when an MCP server connection fails for a request turn.
|
|
47
|
+
* Ephemeral so that misconfigured/unreachable servers don't pollute the durable feed,
|
|
48
|
+
* but can still be surfaced to the user via the live ephemeral event stream.
|
|
49
|
+
*/
|
|
50
|
+
export const McpServerError = Trace.EventType('assistant.mcpServerError', {
|
|
51
|
+
schema: Schema.Struct({
|
|
52
|
+
url: Schema.String,
|
|
53
|
+
protocol: Schema.Literal('sse', 'http'),
|
|
54
|
+
message: Schema.String,
|
|
55
|
+
}),
|
|
56
|
+
isEphemeral: true,
|
|
57
|
+
});
|