@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
|
@@ -1,308 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { AiTool, AiToolkit } from '@effect/ai';
|
|
6
|
-
import { describe, it } from '@effect/vitest';
|
|
7
|
-
import { Effect, Layer, Schema } from 'effect';
|
|
8
|
-
|
|
9
|
-
import { AiService, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
10
|
-
import { AiServiceTestingPreset } from '@dxos/ai/testing';
|
|
11
|
-
import { Obj, Type } from '@dxos/echo';
|
|
12
|
-
import { TracingService } from '@dxos/functions';
|
|
13
|
-
import { log } from '@dxos/log';
|
|
14
|
-
|
|
15
|
-
import { AiSession } from './session';
|
|
16
|
-
|
|
17
|
-
// Define a calendar event artifact schema.
|
|
18
|
-
const CalendarEventSchema = Schema.Struct({
|
|
19
|
-
title: Schema.String,
|
|
20
|
-
startTime: Schema.String,
|
|
21
|
-
endTime: Schema.String,
|
|
22
|
-
description: Schema.String,
|
|
23
|
-
}).pipe(
|
|
24
|
-
Type.Obj({
|
|
25
|
-
typename: 'example.com/type/CalendarEvent',
|
|
26
|
-
version: '0.1.0',
|
|
27
|
-
}),
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
type CalendarEvent = Schema.Schema.Type<typeof CalendarEventSchema>;
|
|
31
|
-
|
|
32
|
-
class TestToolkit extends AiToolkit.make(
|
|
33
|
-
AiTool.make('Calculator', {
|
|
34
|
-
description: 'Basic calculator tool',
|
|
35
|
-
parameters: {
|
|
36
|
-
input: Schema.String.annotations({
|
|
37
|
-
description: 'The calculation to perform.',
|
|
38
|
-
}),
|
|
39
|
-
},
|
|
40
|
-
success: Schema.Struct({
|
|
41
|
-
result: Schema.Number,
|
|
42
|
-
}),
|
|
43
|
-
failure: Schema.Never,
|
|
44
|
-
}),
|
|
45
|
-
) {}
|
|
46
|
-
|
|
47
|
-
// Tool handlers.
|
|
48
|
-
const toolkitLayer = TestToolkit.toLayer({
|
|
49
|
-
Calculator: ({ input }) =>
|
|
50
|
-
Effect.gen(function* () {
|
|
51
|
-
const result = (() => {
|
|
52
|
-
// Restrict to basic arithmetic operations for safety.
|
|
53
|
-
const sanitizedInput = input.replace(/[^0-9+\-*/().\s]/g, '');
|
|
54
|
-
log.info('calculate', { sanitizedInput });
|
|
55
|
-
|
|
56
|
-
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
57
|
-
return Function(`"use strict"; return (${sanitizedInput})`)();
|
|
58
|
-
})();
|
|
59
|
-
|
|
60
|
-
return { result };
|
|
61
|
-
}),
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
describe.runIf(process.env.DX_RUN_SLOW_TESTS)('AiSession', () => {
|
|
65
|
-
it.effect('no tools', () =>
|
|
66
|
-
Effect.gen(function* () {
|
|
67
|
-
const session = new AiSession({ operationModel: 'configured' });
|
|
68
|
-
const response = yield* session.run({
|
|
69
|
-
prompt: 'Hello world!',
|
|
70
|
-
history: [],
|
|
71
|
-
});
|
|
72
|
-
log.info('response', { response });
|
|
73
|
-
}).pipe(
|
|
74
|
-
Effect.provide(
|
|
75
|
-
Layer.mergeAll(
|
|
76
|
-
toolkitLayer,
|
|
77
|
-
AiService.model('@anthropic/claude-3-5-sonnet-20241022').pipe(
|
|
78
|
-
Layer.provideMerge(ToolResolverService.layerEmpty),
|
|
79
|
-
Layer.provideMerge(ToolExecutionService.layerEmpty),
|
|
80
|
-
Layer.provideMerge(AiServiceTestingPreset('direct')),
|
|
81
|
-
Layer.provideMerge(TracingService.layerNoop),
|
|
82
|
-
),
|
|
83
|
-
),
|
|
84
|
-
),
|
|
85
|
-
),
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
it.effect('calculator', () =>
|
|
89
|
-
Effect.gen(function* () {
|
|
90
|
-
const session = new AiSession({ operationModel: 'configured' });
|
|
91
|
-
const toolkit = yield* TestToolkit;
|
|
92
|
-
const response = yield* session.run({
|
|
93
|
-
toolkit,
|
|
94
|
-
prompt: 'What is 10 + 20?',
|
|
95
|
-
history: [],
|
|
96
|
-
});
|
|
97
|
-
log.info('response', { response });
|
|
98
|
-
}).pipe(
|
|
99
|
-
Effect.provide(
|
|
100
|
-
Layer.mergeAll(
|
|
101
|
-
TracingService.layerNoop,
|
|
102
|
-
AiService.model('@anthropic/claude-3-5-sonnet-20241022').pipe(
|
|
103
|
-
Layer.provideMerge(AiServiceTestingPreset('direct')),
|
|
104
|
-
),
|
|
105
|
-
ToolResolverService.layerEmpty,
|
|
106
|
-
ToolExecutionService.layerEmpty,
|
|
107
|
-
toolkitLayer,
|
|
108
|
-
),
|
|
109
|
-
),
|
|
110
|
-
),
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
// TODO(dmaretskyi): Revive test.
|
|
114
|
-
/*
|
|
115
|
-
it.skip('create calendar itinerary', { timeout: 60_000 }, async () => {
|
|
116
|
-
// overrides: { model: 'llama3.1:8b' },
|
|
117
|
-
const session = new AiSession({ operationModel: 'configured' });
|
|
118
|
-
const objects = new Set<string>();
|
|
119
|
-
|
|
120
|
-
// Define calendar artifact.
|
|
121
|
-
const calendarArtifact = defineArtifact({
|
|
122
|
-
id: 'artifact:dxos.org/plugin/calendar',
|
|
123
|
-
name: 'Calendar',
|
|
124
|
-
instructions: 'Use this to create and query calendar events.',
|
|
125
|
-
schema: CalendarEventSchema,
|
|
126
|
-
tools: [
|
|
127
|
-
createTool('calendar', {
|
|
128
|
-
name: 'query',
|
|
129
|
-
description: 'Query the calendar for events',
|
|
130
|
-
schema: Schema.Struct({}),
|
|
131
|
-
execute: async () => {
|
|
132
|
-
return ToolResult.Success(CALENDAR_EVENTS);
|
|
133
|
-
},
|
|
134
|
-
}),
|
|
135
|
-
],
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
const tableArtifact = defineArtifact({
|
|
139
|
-
id: 'artifact:dxos.org/plugin/table',
|
|
140
|
-
name: 'Table',
|
|
141
|
-
instructions: 'Use this to create and manage tables. Each table has a unique id.',
|
|
142
|
-
schema: Schema.Struct({}),
|
|
143
|
-
tools: [
|
|
144
|
-
createTool('table', {
|
|
145
|
-
name: 'create',
|
|
146
|
-
description: 'Create a table',
|
|
147
|
-
schema: Schema.Struct({
|
|
148
|
-
data: Schema.Array(Schema.Any).annotations({ description: 'Array of data payloads to add as rows' }),
|
|
149
|
-
}),
|
|
150
|
-
execute: async ({ data }) => {
|
|
151
|
-
log('create table', { data });
|
|
152
|
-
const id = DXN.fromLocalObjectId(ObjectId.random()).toString();
|
|
153
|
-
objects.add(id);
|
|
154
|
-
// TODO(dmaretskyi): consider xml for refs instead of @dxn:echo:@:XXXXX
|
|
155
|
-
return ToolResult.Success(`table @${id}`);
|
|
156
|
-
},
|
|
157
|
-
}),
|
|
158
|
-
],
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
const mapArtifact = defineArtifact({
|
|
162
|
-
id: 'artifact:dxos.org/plugin/map',
|
|
163
|
-
name: 'Map',
|
|
164
|
-
instructions:
|
|
165
|
-
'Use this to create and manage maps. Maps source data from tables. Table id is required to create a map.',
|
|
166
|
-
schema: Schema.Struct({}),
|
|
167
|
-
tools: [
|
|
168
|
-
createTool('map', {
|
|
169
|
-
name: 'create',
|
|
170
|
-
description: 'Create a map',
|
|
171
|
-
schema: Schema.Struct({
|
|
172
|
-
source: ArtifactId.annotations({
|
|
173
|
-
description: 'The table that will be used as the source of the map',
|
|
174
|
-
}),
|
|
175
|
-
}),
|
|
176
|
-
execute: async ({ source }) => {
|
|
177
|
-
// TODO(dmaretskyi): Use effect-schema decode instead of manual parsing.
|
|
178
|
-
const sourceId = ArtifactId.toDXN(source);
|
|
179
|
-
if (!objects.has(sourceId.toString())) {
|
|
180
|
-
return ToolResult.Error(`table id=${source} not found`);
|
|
181
|
-
}
|
|
182
|
-
log('create map', { sourceId });
|
|
183
|
-
const id = DXN.fromLocalObjectId(ObjectId.random()).toString();
|
|
184
|
-
objects.add(id);
|
|
185
|
-
// TODO(dmaretskyi): consider xml for refs instead of @dxn:echo:@:XXXXX
|
|
186
|
-
return ToolResult.Success(`map @${id}`);
|
|
187
|
-
},
|
|
188
|
-
}),
|
|
189
|
-
],
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
const scriptArtifact = defineArtifact({
|
|
193
|
-
id: 'artifact:dxos.org/plugin/script',
|
|
194
|
-
name: 'Script',
|
|
195
|
-
instructions: 'Use this to create and manage scripts',
|
|
196
|
-
schema: Schema.Struct({}),
|
|
197
|
-
tools: [],
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
// session.streamEvent.on((event) => {
|
|
201
|
-
// printStreamEvent(event);
|
|
202
|
-
// });
|
|
203
|
-
|
|
204
|
-
const printer = new ConsolePrinter();
|
|
205
|
-
session.message.on((message) => printer.printMessage(message));
|
|
206
|
-
session.userMessage.on((message) => printer.printMessage(message));
|
|
207
|
-
session.block.on((block) => printer.printContentBlock(block));
|
|
208
|
-
|
|
209
|
-
// session.update.on((update) => {
|
|
210
|
-
// log('update', { update });
|
|
211
|
-
// });
|
|
212
|
-
|
|
213
|
-
// Test creating an itinerary
|
|
214
|
-
const response = await session.run({
|
|
215
|
-
client: aiClient,
|
|
216
|
-
tools: [],
|
|
217
|
-
artifacts: [calendarArtifact, tableArtifact, mapArtifact, scriptArtifact],
|
|
218
|
-
requiredArtifactIds: [calendarArtifact.id, tableArtifact.id, mapArtifact.id, scriptArtifact.id],
|
|
219
|
-
history: [],
|
|
220
|
-
generationOptions: {
|
|
221
|
-
model: '@anthropic/claude-3-5-haiku-20241022',
|
|
222
|
-
},
|
|
223
|
-
prompt: 'create a table and map for a travel itinerary based on events in my calendar',
|
|
224
|
-
toolResolver: new ToolRegistry([]),
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
log('result', {
|
|
228
|
-
objects,
|
|
229
|
-
finalMessage: response.at(-1),
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
*/
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
// Travel to rome, florence, livorno, siena, madrid for conferences
|
|
236
|
-
|
|
237
|
-
const _CALENDAR_EVENTS: CalendarEvent[] = [
|
|
238
|
-
Obj.make(CalendarEventSchema, {
|
|
239
|
-
title: 'Exploring Ancient Ruins in Rome',
|
|
240
|
-
startTime: '2024-01-01T10:00:00Z',
|
|
241
|
-
endTime: '2024-01-01T11:00:00Z',
|
|
242
|
-
description: 'Tech conference at the historic Colosseum with networking opportunities',
|
|
243
|
-
}),
|
|
244
|
-
Obj.make(CalendarEventSchema, {
|
|
245
|
-
title: 'Renaissance Tech Summit in Florence',
|
|
246
|
-
startTime: '2024-01-01T11:00:00Z',
|
|
247
|
-
endTime: '2024-01-01T12:00:00Z',
|
|
248
|
-
description: 'Discussing AI innovations surrounded by Renaissance art',
|
|
249
|
-
}),
|
|
250
|
-
Obj.make(CalendarEventSchema, {
|
|
251
|
-
title: 'Travel to Livorno',
|
|
252
|
-
startTime: '2024-01-01T12:00:00Z',
|
|
253
|
-
endTime: '2024-01-01T13:00:00Z',
|
|
254
|
-
description: 'Travel to Livorno',
|
|
255
|
-
}),
|
|
256
|
-
Obj.make(CalendarEventSchema, {
|
|
257
|
-
title: 'Travel to Siena',
|
|
258
|
-
startTime: '2024-01-01T13:00:00Z',
|
|
259
|
-
endTime: '2024-01-01T14:00:00Z',
|
|
260
|
-
description: 'Travel to Siena',
|
|
261
|
-
}),
|
|
262
|
-
Obj.make(CalendarEventSchema, {
|
|
263
|
-
title: 'Travel to Madrid',
|
|
264
|
-
startTime: '2024-01-01T14:00:00Z',
|
|
265
|
-
endTime: '2024-01-01T15:00:00Z',
|
|
266
|
-
description: 'Travel to Madrid',
|
|
267
|
-
}),
|
|
268
|
-
];
|
|
269
|
-
|
|
270
|
-
// const printStreamEvent = (event: GenerationStreamEvent) => {
|
|
271
|
-
// switch (event.type) {
|
|
272
|
-
// case 'message_start': {
|
|
273
|
-
// process.stdout.write(`${event.message.role.toUpperCase()}\n\n`);
|
|
274
|
-
// for (const content of event.message.content) {
|
|
275
|
-
// printContentBlock(content);
|
|
276
|
-
// }
|
|
277
|
-
// break;
|
|
278
|
-
// }
|
|
279
|
-
// case 'content_block_start': {
|
|
280
|
-
// printContentBlock(event.content);
|
|
281
|
-
// break;
|
|
282
|
-
// }
|
|
283
|
-
// case 'content_block_delta': {
|
|
284
|
-
// switch (event.delta.type) {
|
|
285
|
-
// case 'text_delta': {
|
|
286
|
-
// process.stdout.write(event.delta.text);
|
|
287
|
-
// break;
|
|
288
|
-
// }
|
|
289
|
-
// case 'input_json_delta': {
|
|
290
|
-
// process.stdout.write(event.delta.partial_json);
|
|
291
|
-
// break;
|
|
292
|
-
// }
|
|
293
|
-
// }
|
|
294
|
-
// break;
|
|
295
|
-
// }
|
|
296
|
-
// case 'content_block_stop': {
|
|
297
|
-
// process.stdout.write('\n');
|
|
298
|
-
// break;
|
|
299
|
-
// }
|
|
300
|
-
// case 'message_delta': {
|
|
301
|
-
// break;
|
|
302
|
-
// }
|
|
303
|
-
// case 'message_stop': {
|
|
304
|
-
// process.stdout.write('\n\n');
|
|
305
|
-
// break;
|
|
306
|
-
// }
|
|
307
|
-
// }
|
|
308
|
-
// };
|
package/src/session/session.ts
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type AiError, AiLanguageModel, type AiTool, type AiToolkit } from '@effect/ai';
|
|
6
|
-
import { Chunk, Effect, type Schema, Stream } from 'effect';
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
type AiInputPreprocessingError,
|
|
10
|
-
AiParser,
|
|
11
|
-
AiPreprocessor,
|
|
12
|
-
type AiToolNotFoundError,
|
|
13
|
-
type ToolExecutionService,
|
|
14
|
-
type ToolResolverService,
|
|
15
|
-
callTool,
|
|
16
|
-
getToolCalls,
|
|
17
|
-
} from '@dxos/ai';
|
|
18
|
-
import { type Blueprint } from '@dxos/blueprints';
|
|
19
|
-
import { todo } from '@dxos/debug';
|
|
20
|
-
import { Obj } from '@dxos/echo';
|
|
21
|
-
import { TracingService } from '@dxos/functions';
|
|
22
|
-
import { log } from '@dxos/log';
|
|
23
|
-
import { DataType } from '@dxos/schema';
|
|
24
|
-
|
|
25
|
-
import { type AiAssistantError } from '../errors';
|
|
26
|
-
|
|
27
|
-
import { mapAiError } from './error-handling';
|
|
28
|
-
import { formatSystemPrompt, formatUserPrompt } from './format';
|
|
29
|
-
import { GenerationObserver } from './observer';
|
|
30
|
-
|
|
31
|
-
export type AiSessionRunError = AiError.AiError | AiInputPreprocessingError | AiToolNotFoundError | AiAssistantError;
|
|
32
|
-
|
|
33
|
-
export type AiSessionRunRequirements =
|
|
34
|
-
| AiLanguageModel.AiLanguageModel
|
|
35
|
-
| ToolExecutionService
|
|
36
|
-
| ToolResolverService
|
|
37
|
-
| TracingService;
|
|
38
|
-
|
|
39
|
-
export type AiSessionRunParams<Tools extends AiTool.Any> = {
|
|
40
|
-
prompt: string;
|
|
41
|
-
system?: string;
|
|
42
|
-
history?: DataType.Message[];
|
|
43
|
-
objects?: Obj.Any[];
|
|
44
|
-
blueprints?: Blueprint.Blueprint[];
|
|
45
|
-
toolkit?: AiToolkit.ToHandler<Tools>;
|
|
46
|
-
observer?: GenerationObserver;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export type AiSessionOptions = {};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Contains message history, tools, current context.
|
|
53
|
-
* Current context means the state of the app, time of day, and other contextual information.
|
|
54
|
-
* It makes requests to the model, its a state machine.
|
|
55
|
-
* It keeps track of the current goal.
|
|
56
|
-
* It manages the context window.
|
|
57
|
-
* Tracks the success criteria of reaching the goal, exposing metrics (stretch).
|
|
58
|
-
* Could be run locally in the app or remotely.
|
|
59
|
-
* Could be personal or shared.
|
|
60
|
-
*/
|
|
61
|
-
export class AiSession {
|
|
62
|
-
/** Prevents concurrent execution of session. */
|
|
63
|
-
private readonly _semaphore = Effect.runSync(Effect.makeSemaphore(1));
|
|
64
|
-
|
|
65
|
-
/** Message history from queue. */
|
|
66
|
-
// TODO(burdon): Evolve into supporting a git-like graph of messages.
|
|
67
|
-
private _history: DataType.Message[] = [];
|
|
68
|
-
|
|
69
|
-
/** Pending messages for this session (incl. the current prompt). */
|
|
70
|
-
private _pending: DataType.Message[] = [];
|
|
71
|
-
|
|
72
|
-
constructor(private readonly _options: AiSessionOptions = {}) {}
|
|
73
|
-
|
|
74
|
-
run = <Tools extends AiTool.Any>({
|
|
75
|
-
prompt,
|
|
76
|
-
system: systemTemplate,
|
|
77
|
-
history = [],
|
|
78
|
-
objects = [],
|
|
79
|
-
blueprints = [],
|
|
80
|
-
toolkit,
|
|
81
|
-
observer = GenerationObserver.noop(),
|
|
82
|
-
}: AiSessionRunParams<Tools>): Effect.Effect<DataType.Message[], AiSessionRunError, AiSessionRunRequirements> =>
|
|
83
|
-
Effect.gen(this, function* () {
|
|
84
|
-
this._history = [...history];
|
|
85
|
-
this._pending = [];
|
|
86
|
-
const pending = this._pending;
|
|
87
|
-
|
|
88
|
-
const submitMessage = Effect.fnUntraced(function* (message: DataType.Message) {
|
|
89
|
-
pending.push(message);
|
|
90
|
-
yield* observer.onMessage(message);
|
|
91
|
-
yield* TracingService.emitConverationMessage(message);
|
|
92
|
-
return message;
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// Submit the prompt.
|
|
96
|
-
// TODO(burdon): Remove if cancelled?
|
|
97
|
-
const promptMessage = yield* submitMessage(yield* formatUserPrompt({ prompt, history }));
|
|
98
|
-
|
|
99
|
-
// Generate system and prompt messages.
|
|
100
|
-
const system = yield* formatSystemPrompt({ system: systemTemplate, blueprints, objects });
|
|
101
|
-
|
|
102
|
-
// Tool call loop.
|
|
103
|
-
do {
|
|
104
|
-
log('request', {
|
|
105
|
-
prompt: promptMessage,
|
|
106
|
-
system: { snippet: createSnippet(system), length: system.length },
|
|
107
|
-
pending: this._pending.length,
|
|
108
|
-
history: this._history.length,
|
|
109
|
-
objects: objects?.length ?? 0,
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
const prompt = yield* AiPreprocessor.preprocessAiInput([...this._history, ...this._pending]);
|
|
113
|
-
|
|
114
|
-
// Execute the stream request.
|
|
115
|
-
const blocks = yield* AiLanguageModel.streamText({
|
|
116
|
-
prompt,
|
|
117
|
-
system,
|
|
118
|
-
toolkit,
|
|
119
|
-
disableToolCallResolution: true,
|
|
120
|
-
}).pipe(
|
|
121
|
-
Stream.catchTag(
|
|
122
|
-
'AiError',
|
|
123
|
-
Effect.fnUntraced(function* (err) {
|
|
124
|
-
return yield* Effect.fail(yield* mapAiError(err));
|
|
125
|
-
}),
|
|
126
|
-
),
|
|
127
|
-
AiParser.parseResponse({
|
|
128
|
-
onBegin: () => observer.onBegin(),
|
|
129
|
-
onBlock: (block) => observer.onBlock(block),
|
|
130
|
-
onPart: (part) => observer.onPart(part),
|
|
131
|
-
onEnd: (summary) => observer.onEnd(summary),
|
|
132
|
-
}),
|
|
133
|
-
Stream.runCollect,
|
|
134
|
-
Effect.map(Chunk.toArray),
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
// Create the response message.
|
|
138
|
-
const response = yield* submitMessage(
|
|
139
|
-
Obj.make(DataType.Message, {
|
|
140
|
-
created: new Date().toISOString(),
|
|
141
|
-
sender: { role: 'assistant' },
|
|
142
|
-
blocks,
|
|
143
|
-
}),
|
|
144
|
-
);
|
|
145
|
-
|
|
146
|
-
// Parse the response for tool calls.
|
|
147
|
-
const toolCalls = getToolCalls(response);
|
|
148
|
-
if (toolCalls.length === 0) {
|
|
149
|
-
break;
|
|
150
|
-
} else if (!toolkit) {
|
|
151
|
-
throw new Error('No toolkit provided'); // TODO(burdon): Throw user error?
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// Execute the tool calls.
|
|
155
|
-
// TODO(burdon): Retry errors? Write result when each completes individually?
|
|
156
|
-
const toolResults = yield* Effect.forEach(toolCalls, (toolCall) => {
|
|
157
|
-
return callTool(toolkit, toolCall).pipe(
|
|
158
|
-
Effect.provide(
|
|
159
|
-
TracingService.layerSubframe((context) => ({
|
|
160
|
-
...context,
|
|
161
|
-
parentMessage: response.id,
|
|
162
|
-
toolCallId: toolCall.toolCallId,
|
|
163
|
-
})),
|
|
164
|
-
),
|
|
165
|
-
);
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
// Add to queue and continue loop.
|
|
169
|
-
yield* submitMessage(
|
|
170
|
-
Obj.make(DataType.Message, {
|
|
171
|
-
created: new Date().toISOString(),
|
|
172
|
-
sender: { role: 'user' },
|
|
173
|
-
blocks: toolResults,
|
|
174
|
-
}),
|
|
175
|
-
);
|
|
176
|
-
} while (true);
|
|
177
|
-
|
|
178
|
-
log('done', { pending: this._pending.length });
|
|
179
|
-
return this._pending;
|
|
180
|
-
}).pipe(this._semaphore.withPermits(1), Effect.withSpan('AiSession.run'));
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* @deprecated
|
|
184
|
-
*/
|
|
185
|
-
// TODO(burdon): Implement or remove.
|
|
186
|
-
async runStructured<S extends Schema.Schema.AnyNoContext>(
|
|
187
|
-
_schema: S,
|
|
188
|
-
_options: AiSessionRunParams<AiTool.Any>,
|
|
189
|
-
): Promise<Schema.Schema.Type<S>> {
|
|
190
|
-
return todo();
|
|
191
|
-
// const parser = structuredOutputParser(schema);
|
|
192
|
-
// const result = await this.run({
|
|
193
|
-
// ...options,
|
|
194
|
-
// executableTools: [...(options.executableTools ?? []), parser.tool],
|
|
195
|
-
// });
|
|
196
|
-
// return parser.getResult(result);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const createSnippet = (text: string, len = 32) =>
|
|
201
|
-
text.length <= len * 2 ? text : [text.slice(0, len), '...', text.slice(-len)].join('');
|