@dxos/assistant 0.8.4-main.f5c0578 → 0.8.4-main.fcfe5033a5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/lib/neutral/chunk-2TBLG3XY.mjs +1793 -0
- package/dist/lib/neutral/chunk-2TBLG3XY.mjs.map +7 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/neutral/extraction/index.mjs +219 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +537 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/testing/index.mjs +111 -0
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +130 -65
- 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 +78 -0
- package/dist/types/src/conversation/session.d.ts.map +1 -0
- package/dist/types/src/conversation/session.test.d.ts.map +1 -0
- package/dist/types/src/diff.d.ts +17 -0
- package/dist/types/src/diff.d.ts.map +1 -0
- package/dist/types/src/diff.test.d.ts +2 -0
- package/dist/types/src/diff.test.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +46 -18
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/execution-graph.d.ts +85 -0
- package/dist/types/src/execution-graph.d.ts.map +1 -0
- package/dist/types/src/execution-graph.test.d.ts +2 -0
- package/dist/types/src/execution-graph.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-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 -277
- 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/functions.test.d.ts +2 -0
- package/dist/types/src/functions.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/service/AgentService.d.ts +75 -0
- package/dist/types/src/service/AgentService.d.ts.map +1 -0
- package/dist/types/src/service/AgentService.test.d.ts +2 -0
- package/dist/types/src/service/AgentService.test.d.ts.map +1 -0
- package/dist/types/src/service/agent-process.d.ts +21 -0
- package/dist/types/src/service/agent-process.d.ts.map +1 -0
- package/dist/types/src/service/index.d.ts +3 -0
- package/dist/types/src/service/index.d.ts.map +1 -0
- 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 +4 -0
- package/dist/types/src/session/error-handling.d.ts.map +1 -0
- package/dist/types/src/session/format.d.ts +9 -161
- package/dist/types/src/session/format.d.ts.map +1 -1
- package/dist/types/src/session/index.d.ts +4 -1
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +23 -0
- package/dist/types/src/session/observer.d.ts.map +1 -0
- package/dist/types/src/session/request.d.ts +106 -0
- package/dist/types/src/session/request.d.ts.map +1 -0
- package/dist/types/src/session/request.test.d.ts +2 -0
- package/dist/types/src/session/request.test.d.ts.map +1 -0
- package/dist/types/src/session/session-ollama.test.d.ts +2 -0
- package/dist/types/src/session/session-ollama.test.d.ts.map +1 -0
- package/dist/types/src/session/toolkit.d.ts +16 -0
- package/dist/types/src/session/toolkit.d.ts.map +1 -0
- package/dist/types/src/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 +7 -12
- package/dist/types/src/templates/system.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +2 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/layer.d.ts +40 -0
- package/dist/types/src/testing/layer.d.ts.map +1 -0
- package/dist/types/src/tracing.d.ts +223 -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 +43 -36
- package/src/conversation/context.test.ts +52 -0
- package/src/conversation/context.ts +338 -68
- package/src/conversation/index.ts +1 -1
- package/src/conversation/session.test.ts +58 -0
- package/src/conversation/session.ts +300 -0
- package/src/diff.test.ts +74 -0
- package/src/diff.ts +93 -0
- package/src/errors.ts +6 -1
- package/src/execution-graph.test.ts +740 -0
- package/src/execution-graph.ts +509 -0
- package/src/extraction/extraction-ner-function/index.ts +0 -1
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
- package/src/extraction/extraction-ner-function/named-entity-recognition.ts +2 -0
- package/src/extraction/extraction.ts +44 -15
- package/src/extraction/index.ts +0 -1
- package/src/extraction/quotes.test.ts +2 -2
- package/src/extraction/quotes.ts +6 -7
- package/src/functions/services.ts +166 -66
- package/src/functions.conversations.json +1 -0
- package/src/functions.test.ts +88 -0
- package/src/index.ts +5 -1
- package/src/service/AgentService.conversations.json +1 -0
- package/src/service/AgentService.test.ts +169 -0
- package/src/service/AgentService.ts +184 -0
- package/src/service/agent-process.ts +364 -0
- package/src/service/index.ts +6 -0
- package/src/session/artifact-diff.ts +8 -4
- package/src/session/error-handling.ts +41 -0
- package/src/session/format.test.ts +113 -41
- package/src/session/format.ts +39 -29
- package/src/session/index.ts +4 -1
- package/src/session/observer.ts +62 -0
- package/src/session/request.conversations.json +1 -0
- package/src/session/request.test.ts +195 -0
- package/src/session/request.ts +344 -0
- package/src/session/session-ollama.test.ts +81 -0
- package/src/session/toolkit.ts +47 -0
- package/src/session/version-pin.ts +7 -7
- package/src/templates/{format.tpl → instructions/format.tpl} +6 -4
- package/src/templates/{system.tpl → instructions/system.tpl} +52 -14
- package/src/templates/system.ts +26 -17
- package/src/testing/index.ts +5 -0
- package/src/testing/layer.ts +236 -0
- package/src/tracing.ts +43 -0
- package/src/{types.ts → types/artifact.ts} +44 -7
- package/src/types/index.ts +5 -0
- package/dist/lib/browser/chunk-6WRGDEMH.mjs +0 -89
- package/dist/lib/browser/chunk-6WRGDEMH.mjs.map +0 -7
- package/dist/lib/browser/chunk-AVKQ32JL.mjs +0 -352
- package/dist/lib/browser/chunk-AVKQ32JL.mjs.map +0 -7
- package/dist/lib/browser/extraction/index.mjs +0 -269
- package/dist/lib/browser/extraction/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -355
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/named-entity-recognition-O46UTCLR.mjs +0 -13
- package/dist/lib/node-esm/chunk-3UU5V4FS.mjs +0 -91
- package/dist/lib/node-esm/chunk-3UU5V4FS.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-MNAVETJA.mjs +0 -354
- package/dist/lib/node-esm/chunk-MNAVETJA.mjs.map +0 -7
- package/dist/lib/node-esm/extraction/index.mjs +0 -270
- package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -356
- 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-W4VMRCH6.mjs +0 -14
- package/dist/lib/node-esm/named-entity-recognition-W4VMRCH6.mjs.map +0 -7
- package/dist/types/src/conversation/conversation.d.ts +0 -49
- 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 -306
- 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/session.d.ts +0 -170
- package/dist/types/src/session/session.d.ts.map +0 -1
- 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 -127
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -84
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -71
- package/src/extraction/extraction-llm-function/index.ts +0 -5
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +0 -116
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +0 -57
- package/src/session/__snapshots__/session.test.ts.snap +0 -32
- package/src/session/session.test.ts +0 -308
- package/src/session/session.ts +0 -303
- package/src/session/version-pin.test.ts +0 -7
- package/src/summarization/summarization.test.ts +0 -197
- /package/dist/lib/{browser/named-entity-recognition-O46UTCLR.mjs.map → neutral/chunk-J5LGTIGS.mjs.map} +0 -0
- /package/dist/types/src/{session → conversation}/session.test.d.ts +0 -0
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
package/src/session/session.ts
DELETED
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type AiError, AiLanguageModel, type AiResponse, type AiTool, AiToolkit } from '@effect/ai';
|
|
6
|
-
import { Chunk, type Context, Effect, Function, Option, Queue, type Schema, Stream } from 'effect';
|
|
7
|
-
|
|
8
|
-
import { type ToolId } from '@dxos/ai';
|
|
9
|
-
import {
|
|
10
|
-
type AiInputPreprocessingError,
|
|
11
|
-
AiParser,
|
|
12
|
-
AiPreprocessor,
|
|
13
|
-
type AiToolNotFoundError,
|
|
14
|
-
type ConsolePrinter,
|
|
15
|
-
ToolExecutionService,
|
|
16
|
-
ToolResolverService,
|
|
17
|
-
callTool,
|
|
18
|
-
getToolCalls,
|
|
19
|
-
} from '@dxos/ai';
|
|
20
|
-
import { type Blueprint } from '@dxos/blueprints';
|
|
21
|
-
import { todo } from '@dxos/debug';
|
|
22
|
-
import { Obj } from '@dxos/echo';
|
|
23
|
-
import { TracingService } from '@dxos/functions';
|
|
24
|
-
import { log } from '@dxos/log';
|
|
25
|
-
import { type ContentBlock, DataType } from '@dxos/schema';
|
|
26
|
-
import { isNotFalsy } from '@dxos/util';
|
|
27
|
-
|
|
28
|
-
import { type AiAssistantError } from '../errors';
|
|
29
|
-
|
|
30
|
-
import { formatSystemPrompt, formatUserPrompt } from './format';
|
|
31
|
-
|
|
32
|
-
export type AiSessionOptions = {};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Live observer of the generation process.
|
|
36
|
-
*/
|
|
37
|
-
export interface GenerationObserver {
|
|
38
|
-
/**
|
|
39
|
-
* Unparsed content block parts from the model.
|
|
40
|
-
*/
|
|
41
|
-
onPart: (part: AiResponse.Part) => Effect.Effect<void>;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Parsed content blocks from the model.
|
|
45
|
-
* NOTE: Use block.pending to determine if the block was completed.
|
|
46
|
-
* For each block this will be called 0..n times with a pending block and then once with the final state of the block.
|
|
47
|
-
*
|
|
48
|
-
* Example:
|
|
49
|
-
* 1. { pending: true, text: "Hello"}
|
|
50
|
-
* 2. { pending: true, text: "Hello, I am a"}
|
|
51
|
-
* 3. { pending: false, text: "Hello, I am a helpful assistant!"}
|
|
52
|
-
*/
|
|
53
|
-
onBlock: (block: ContentBlock.Any) => Effect.Effect<void>;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Complete messages fired during the session, both from the model and from the user.
|
|
57
|
-
* This message will contain all message blocks emitted through the `onBlock` callback.
|
|
58
|
-
*/
|
|
59
|
-
onMessage: (message: DataType.Message) => Effect.Effect<void>;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export const GenerationObserver = Object.freeze({
|
|
63
|
-
make: ({
|
|
64
|
-
onPart = Function.constant(Effect.void),
|
|
65
|
-
onBlock = Function.constant(Effect.void),
|
|
66
|
-
onMessage = Function.constant(Effect.void),
|
|
67
|
-
}: Partial<GenerationObserver> = {}): GenerationObserver => ({
|
|
68
|
-
onPart,
|
|
69
|
-
onBlock,
|
|
70
|
-
onMessage,
|
|
71
|
-
}),
|
|
72
|
-
|
|
73
|
-
noop: () => GenerationObserver.make(),
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Debug printer to be used in unit-tests and browser devtools.
|
|
77
|
-
*/
|
|
78
|
-
fromPrinter: (printer: ConsolePrinter) =>
|
|
79
|
-
GenerationObserver.make({
|
|
80
|
-
onBlock: (block) =>
|
|
81
|
-
Effect.sync(() => {
|
|
82
|
-
if (block.pending) {
|
|
83
|
-
return; // Only prints full blocks (better for unit-tests and browser devtools).
|
|
84
|
-
}
|
|
85
|
-
printer.printContentBlock(block);
|
|
86
|
-
}),
|
|
87
|
-
onMessage: (message) =>
|
|
88
|
-
Effect.sync(() => {
|
|
89
|
-
if (message.sender.role === 'assistant') {
|
|
90
|
-
return; // Skip assistant messages since they are printed in the `onBlock` callback.
|
|
91
|
-
}
|
|
92
|
-
printer.printMessage(message);
|
|
93
|
-
}),
|
|
94
|
-
}),
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
export type SessionRunParams<Tools extends AiTool.Any> = {
|
|
98
|
-
prompt: string;
|
|
99
|
-
system?: string;
|
|
100
|
-
history?: DataType.Message[];
|
|
101
|
-
objects?: Obj.Any[]; // TODO(burdon): Meta only (typename and id -- write to binder).
|
|
102
|
-
blueprints?: Blueprint.Blueprint[];
|
|
103
|
-
toolIds?: ToolId[];
|
|
104
|
-
toolkit?: AiToolkit.AiToolkit<Tools>;
|
|
105
|
-
observer?: GenerationObserver;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Contains message history, tools, current context.
|
|
110
|
-
* Current context means the state of the app, time of day, and other contextual information.
|
|
111
|
-
* It makes requests to the model, its a state machine.
|
|
112
|
-
* It keeps track of the current goal.
|
|
113
|
-
* It manages the context window.
|
|
114
|
-
* Tracks the success criteria of reaching the goal, exposing metrics (stretch).
|
|
115
|
-
* Could be run locally in the app or remotely.
|
|
116
|
-
* Could be personal or shared.
|
|
117
|
-
*/
|
|
118
|
-
export class AiSession {
|
|
119
|
-
/** Complete messages fired during the session, both from the model and from the user. */
|
|
120
|
-
public readonly messageQueue = Effect.runSync(Queue.unbounded<DataType.Message>());
|
|
121
|
-
|
|
122
|
-
// TODO(dmaretskyi): Remove the queues and convert everything to observer.
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Blocks streaming from the model during the session.
|
|
126
|
-
* @deprecated Use `observer.onBlock` instead.
|
|
127
|
-
*/
|
|
128
|
-
public readonly blockQueue = Effect.runSync(Queue.unbounded<Option.Option<ContentBlock.Any>>());
|
|
129
|
-
|
|
130
|
-
/** Unparsed events from the underlying generation stream. */
|
|
131
|
-
public readonly eventQueue = Effect.runSync(Queue.unbounded<AiResponse.Part>());
|
|
132
|
-
|
|
133
|
-
/** Prevents concurrent execution of session. */
|
|
134
|
-
private readonly _semaphore = Effect.runSync(Effect.makeSemaphore(1));
|
|
135
|
-
|
|
136
|
-
/** Pending messages (incl. the current user request). */
|
|
137
|
-
private _pending: DataType.Message[] = [];
|
|
138
|
-
|
|
139
|
-
/** Prior history from queue. */
|
|
140
|
-
private _history: DataType.Message[] = [];
|
|
141
|
-
|
|
142
|
-
constructor(private readonly _options: AiSessionOptions = {}) {}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Runs the AI model loop interacting with tools and artifacts.
|
|
146
|
-
* @returns The messages generated by the session, including the user's prompt.
|
|
147
|
-
*/
|
|
148
|
-
// TODO(dmaretskyi): Toolkit context doesn't get added to the effect type.
|
|
149
|
-
run = <Tools extends AiTool.Any>(
|
|
150
|
-
params: SessionRunParams<Tools>,
|
|
151
|
-
): Effect.Effect<
|
|
152
|
-
DataType.Message[],
|
|
153
|
-
AiAssistantError | AiInputPreprocessingError | AiToolNotFoundError | AiError.AiError,
|
|
154
|
-
| AiLanguageModel.AiLanguageModel
|
|
155
|
-
| ToolResolverService
|
|
156
|
-
| ToolExecutionService
|
|
157
|
-
| TracingService
|
|
158
|
-
| AiTool.ToHandler<Tools>
|
|
159
|
-
> =>
|
|
160
|
-
Effect.gen(this, function* () {
|
|
161
|
-
const observer = params.observer ?? GenerationObserver.noop();
|
|
162
|
-
|
|
163
|
-
// Create toolkit.
|
|
164
|
-
const toolkit: AiToolkit.ToHandler<Tools> = yield* createToolkit(params);
|
|
165
|
-
|
|
166
|
-
// Generate system prompt.
|
|
167
|
-
// TODO(budon): Dynamically resolve template variables.
|
|
168
|
-
const system = yield* formatSystemPrompt(params);
|
|
169
|
-
console.log(system);
|
|
170
|
-
|
|
171
|
-
// Generate user prompt.
|
|
172
|
-
const promptMessages = yield* formatUserPrompt(params);
|
|
173
|
-
yield* this.messageQueue.offer(promptMessages);
|
|
174
|
-
yield* observer.onMessage(promptMessages);
|
|
175
|
-
yield* TracingService.emitConverationMessage(promptMessages);
|
|
176
|
-
|
|
177
|
-
this._history = [...(params.history ?? [])];
|
|
178
|
-
this._pending = [promptMessages];
|
|
179
|
-
|
|
180
|
-
// Potential tool-use loop.
|
|
181
|
-
do {
|
|
182
|
-
log.info('request', {
|
|
183
|
-
prompt: promptMessages,
|
|
184
|
-
system: { snippet: [system.slice(0, 32), '...', system.slice(-32)].join(''), length: system.length },
|
|
185
|
-
pending: this._pending.length,
|
|
186
|
-
history: this._history.length,
|
|
187
|
-
objects: params.objects?.length ?? 0,
|
|
188
|
-
blueprints: params.blueprints?.length ?? 0,
|
|
189
|
-
toolkit: Object.values(toolkit.tools).map((tool: AiTool.Any) => tool.name),
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
// Generate the prompt and make request.
|
|
193
|
-
const prompt = yield* AiPreprocessor.preprocessAiInput([...this._history, ...this._pending]);
|
|
194
|
-
const blocks = yield* AiLanguageModel.streamText({
|
|
195
|
-
prompt,
|
|
196
|
-
system,
|
|
197
|
-
toolkit,
|
|
198
|
-
// TODO(burdon): Despite this flag, the model still calls tools.
|
|
199
|
-
// Flag is only used in generateText (not streamText); patch and submit bug.
|
|
200
|
-
// https://github.com/Effect-TS/effect/blob/main/packages/ai/ai/src/AiLanguageModel.ts#L401
|
|
201
|
-
disableToolCallResolution: true,
|
|
202
|
-
}).pipe(
|
|
203
|
-
AiParser.parseResponse({
|
|
204
|
-
onBlock: (block) =>
|
|
205
|
-
Effect.all([this.blockQueue.offer(Option.some(block)), observer.onBlock(block)], { discard: true }),
|
|
206
|
-
onPart: (part) => Effect.all([this.eventQueue.offer(part), observer.onPart(part)], { discard: true }),
|
|
207
|
-
}),
|
|
208
|
-
Stream.runCollect,
|
|
209
|
-
Effect.map(Chunk.toArray),
|
|
210
|
-
);
|
|
211
|
-
|
|
212
|
-
// Signal to stream consumers that message blocks are complete.
|
|
213
|
-
// Allows for coordination between the block and message queues
|
|
214
|
-
// to prevent the streaming blocks from being rendered twice when the message is produced.
|
|
215
|
-
// TODO(wittjosiah): The block queue should probably be drained at this point in the case that there is no consumer.
|
|
216
|
-
yield* this.blockQueue.offer(Option.none());
|
|
217
|
-
|
|
218
|
-
// Create response message.
|
|
219
|
-
const response = Obj.make(DataType.Message, {
|
|
220
|
-
created: new Date().toISOString(),
|
|
221
|
-
sender: { role: 'assistant' },
|
|
222
|
-
blocks,
|
|
223
|
-
});
|
|
224
|
-
this._pending.push(response);
|
|
225
|
-
yield* this.messageQueue.offer(response);
|
|
226
|
-
yield* observer.onMessage(response);
|
|
227
|
-
yield* TracingService.emitConverationMessage(response);
|
|
228
|
-
|
|
229
|
-
// Parse response for tool calls.
|
|
230
|
-
const toolCalls = getToolCalls(response);
|
|
231
|
-
if (toolCalls.length === 0) {
|
|
232
|
-
break;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// TODO(burdon): Report errors to user; with proposed actions.
|
|
236
|
-
const toolResults = yield* Effect.forEach(toolCalls, (toolCall) =>
|
|
237
|
-
callTool(toolkit, toolCall).pipe(
|
|
238
|
-
Effect.provide(
|
|
239
|
-
TracingService.layerSubframe((context) => ({
|
|
240
|
-
...context,
|
|
241
|
-
parentMessage: response.id,
|
|
242
|
-
toolCallId: toolCall.toolCallId,
|
|
243
|
-
})),
|
|
244
|
-
),
|
|
245
|
-
),
|
|
246
|
-
);
|
|
247
|
-
const toolResultsMessage = Obj.make(DataType.Message, {
|
|
248
|
-
created: new Date().toISOString(),
|
|
249
|
-
sender: { role: 'user' },
|
|
250
|
-
blocks: toolResults,
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
this._pending.push(toolResultsMessage);
|
|
254
|
-
yield* this.messageQueue.offer(toolResultsMessage);
|
|
255
|
-
yield* observer.onMessage(toolResultsMessage);
|
|
256
|
-
yield* TracingService.emitConverationMessage(toolResultsMessage);
|
|
257
|
-
} while (true);
|
|
258
|
-
|
|
259
|
-
// Signals to stream consumers that the session has completed and no more messages are coming.
|
|
260
|
-
yield* Queue.shutdown(this.messageQueue);
|
|
261
|
-
yield* Queue.shutdown(this.blockQueue);
|
|
262
|
-
yield* Queue.shutdown(this.eventQueue);
|
|
263
|
-
|
|
264
|
-
log('done', { pending: this._pending.length });
|
|
265
|
-
return this._pending;
|
|
266
|
-
}).pipe(this._semaphore.withPermits(1), Effect.withSpan('AiSession.run'));
|
|
267
|
-
|
|
268
|
-
// TODO(burdon): Implement.
|
|
269
|
-
async runStructured<S extends Schema.Schema.AnyNoContext>(
|
|
270
|
-
_schema: S,
|
|
271
|
-
_options: SessionRunParams<AiTool.Any>,
|
|
272
|
-
): Promise<Schema.Schema.Type<S>> {
|
|
273
|
-
return todo();
|
|
274
|
-
// const parser = structuredOutputParser(schema);
|
|
275
|
-
// const result = await this.run({
|
|
276
|
-
// ...options,
|
|
277
|
-
// executableTools: [...(options.executableTools ?? []), parser.tool],
|
|
278
|
-
// });
|
|
279
|
-
// return parser.getResult(result);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Build a combined toolkit from the blueprint tools and the provided toolkit.
|
|
285
|
-
*/
|
|
286
|
-
const createToolkit = <Tools extends AiTool.Any>({
|
|
287
|
-
toolkit,
|
|
288
|
-
blueprints = [],
|
|
289
|
-
toolIds = [],
|
|
290
|
-
}: Pick<SessionRunParams<Tools>, 'toolkit' | 'blueprints' | 'toolIds'>) =>
|
|
291
|
-
Effect.gen(function* () {
|
|
292
|
-
const blueprintToolkit = yield* ToolResolverService.resolveToolkit([
|
|
293
|
-
...blueprints.flatMap(({ tools }) => tools),
|
|
294
|
-
...toolIds,
|
|
295
|
-
]);
|
|
296
|
-
const blueprintToolkitHandler: Context.Context<AiTool.ToHandler<AiTool.Any>> = yield* blueprintToolkit.toContext(
|
|
297
|
-
ToolExecutionService.handlersFor(blueprintToolkit),
|
|
298
|
-
);
|
|
299
|
-
|
|
300
|
-
return yield* AiToolkit.merge(...[toolkit, blueprintToolkit].filter(isNotFalsy)).pipe(
|
|
301
|
-
Effect.provide(blueprintToolkitHandler),
|
|
302
|
-
) as Effect.Effect<AiToolkit.ToHandler<any>, never, AiTool.ToHandler<Tools>>;
|
|
303
|
-
});
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
// ISSUE(burdon): defineFunction
|
|
6
|
-
// @ts-nocheck
|
|
7
|
-
|
|
8
|
-
import { Schema, pipe } from 'effect';
|
|
9
|
-
import { beforeAll, describe, test } from 'vitest';
|
|
10
|
-
|
|
11
|
-
import { AiService, MixedStreamParser } from '@dxos/ai';
|
|
12
|
-
import { Obj } from '@dxos/echo';
|
|
13
|
-
import { type EchoDatabase } from '@dxos/echo-db';
|
|
14
|
-
import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
15
|
-
import { FunctionExecutor, ServiceContainer, defineFunction } from '@dxos/functions';
|
|
16
|
-
import { type ContentBlock, DataType } from '@dxos/schema';
|
|
17
|
-
import { createTestData } from '@dxos/schema/testing';
|
|
18
|
-
import { trim } from '@dxos/util';
|
|
19
|
-
|
|
20
|
-
const summarizationFn = defineFunction({
|
|
21
|
-
description: 'Summarize a document',
|
|
22
|
-
inputSchema: Schema.Struct({
|
|
23
|
-
document: Schema.optional(DataType.Text),
|
|
24
|
-
transcript: Schema.Array(DataType.Message),
|
|
25
|
-
}),
|
|
26
|
-
outputSchema: DataType.Text,
|
|
27
|
-
handler: async ({ data: { document, transcript }, context }) => {
|
|
28
|
-
const ai = context.getService(AiService.AiService);
|
|
29
|
-
const result = await new MixedStreamParser().parse(
|
|
30
|
-
await ai.client.execStream({
|
|
31
|
-
model: '@anthropic/claude-3-5-haiku-20241022',
|
|
32
|
-
// TODO(burdon): Factor out and test prompts.
|
|
33
|
-
systemPrompt: trim`
|
|
34
|
-
Your job is to summarize a meeting transcript.
|
|
35
|
-
Transcript Summarizer updates an existing summary within <summary> tags with a new transcript that is placed in <transcript> tags.
|
|
36
|
-
Transcript Summarizer updates only the relevant parts of the summary.
|
|
37
|
-
If there's no summary, create a new one.
|
|
38
|
-
Transcript Summarizer outputs only the summary texts and nothing else.
|
|
39
|
-
Transcript Summarizer keeps the original summary structure.
|
|
40
|
-
Transcript Summarizer keeps the unrelated parts of the summary untouched.
|
|
41
|
-
The Transcript Summarizer errs on the side of being too verbose.
|
|
42
|
-
The Transcript Summarizer outputs the entire update summary.
|
|
43
|
-
The summary is formatted as hierarchical markdown bullet points with no headings.
|
|
44
|
-
The hierarchical structure is deeply nested.
|
|
45
|
-
The Transcript Summarizer formats the bullets with "-" symbol as prefix and two spaces indentation.
|
|
46
|
-
The Transcript Summarizer knows that the content of transcript will be updated solely by its output.
|
|
47
|
-
The Transcript Summarizer outputs only the summary text.
|
|
48
|
-
`,
|
|
49
|
-
history: [
|
|
50
|
-
Obj.make(DataType.Message, {
|
|
51
|
-
created: new Date().toISOString(),
|
|
52
|
-
sender: { role: 'user' },
|
|
53
|
-
blocks: [
|
|
54
|
-
{
|
|
55
|
-
_tag: 'text',
|
|
56
|
-
text: trim`
|
|
57
|
-
<transcript>
|
|
58
|
-
${JSON.stringify(transcript, null, 2)}
|
|
59
|
-
</transcript>
|
|
60
|
-
`,
|
|
61
|
-
},
|
|
62
|
-
...(document != null && document.content.length > 0
|
|
63
|
-
? [
|
|
64
|
-
{
|
|
65
|
-
_tag: 'text',
|
|
66
|
-
text: trim`
|
|
67
|
-
<summary>
|
|
68
|
-
${document.content}
|
|
69
|
-
</summary>
|
|
70
|
-
`,
|
|
71
|
-
} satisfies ContentBlock.Text,
|
|
72
|
-
]
|
|
73
|
-
: []),
|
|
74
|
-
],
|
|
75
|
-
}),
|
|
76
|
-
],
|
|
77
|
-
}),
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
return DataType.makeText(pipe(result[0]?.blocks[0], (c) => (c?._tag === 'text' ? c.text : '')));
|
|
81
|
-
},
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
const refinementFn = defineFunction({
|
|
85
|
-
description: 'Refine a summary',
|
|
86
|
-
inputSchema: Schema.Struct({
|
|
87
|
-
summaries: Schema.Array(DataType.Text),
|
|
88
|
-
}),
|
|
89
|
-
outputSchema: Schema.Struct({
|
|
90
|
-
summary: DataType.Text,
|
|
91
|
-
}),
|
|
92
|
-
handler: async ({ data: { summaries }, context }) => {
|
|
93
|
-
const ai = context.getService(AiService.AiService);
|
|
94
|
-
const result = await new MixedStreamParser().parse(
|
|
95
|
-
await ai.client.execStream({
|
|
96
|
-
model: '@anthropic/claude-3-5-haiku-20241022',
|
|
97
|
-
// TODO(burdon): Factor out and test prompts.
|
|
98
|
-
systemPrompt: trim`
|
|
99
|
-
You are a Transcript Summary Refiner.
|
|
100
|
-
Transcript Summary Refiner works on a history of evolving summaries that were generated based on the transcript.
|
|
101
|
-
The summaries are presented starting from the oldest to the newest.
|
|
102
|
-
Each iteration of the summary contains a new part of the transcript.
|
|
103
|
-
The Transcript Summary Refiner refines the summary based on the history of summaries.
|
|
104
|
-
The Transcript Summary Refiner identifies any potential data lost between the iterations and adds its back to the output.
|
|
105
|
-
The Transcript Summary Refiner updates the summary to be more structured.
|
|
106
|
-
The Transcript Summary Refiner errs on the side of being too verbose.
|
|
107
|
-
The Transcript Summary Refiner errs on keeping the original summary structure.
|
|
108
|
-
The Transcript Summary Refiner outputs the entire update summary.
|
|
109
|
-
The Transcript Summary is formatted as hierarchical markdown bullet points with no headings.
|
|
110
|
-
The hierarchical structure is deeply nested.
|
|
111
|
-
The Transcript Summary Refiner formats the bullets with "-" symbol as prefix and two spaces indentation.
|
|
112
|
-
The Transcript Summary Refiner knows that the content of transcript will be updated solely by its output.
|
|
113
|
-
The Transcript Summary Refiner outputs only the summary text.
|
|
114
|
-
`,
|
|
115
|
-
history: [
|
|
116
|
-
Obj.make(DataType.Message, {
|
|
117
|
-
created: new Date().toISOString(),
|
|
118
|
-
sender: { role: 'user' },
|
|
119
|
-
blocks: summaries.map(
|
|
120
|
-
(summary) =>
|
|
121
|
-
({
|
|
122
|
-
_tag: 'text',
|
|
123
|
-
text: trim`
|
|
124
|
-
<summary>
|
|
125
|
-
${summary.content}
|
|
126
|
-
</summary>
|
|
127
|
-
`,
|
|
128
|
-
}) satisfies ContentBlock.Text,
|
|
129
|
-
),
|
|
130
|
-
}),
|
|
131
|
-
],
|
|
132
|
-
}),
|
|
133
|
-
);
|
|
134
|
-
return {
|
|
135
|
-
summary: DataType.makeText(pipe(result[0]?.blocks[0], (c) => (c?._tag === 'text' ? c.text : ''))),
|
|
136
|
-
};
|
|
137
|
-
},
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
describe.skip('Summarization', () => {
|
|
141
|
-
let builder: EchoTestBuilder;
|
|
142
|
-
let db: EchoDatabase;
|
|
143
|
-
let executor: FunctionExecutor;
|
|
144
|
-
|
|
145
|
-
// TODO(burdon): Rewrite test.
|
|
146
|
-
beforeAll(async () => {
|
|
147
|
-
// TODO(dmaretskyi): Helper to scaffold this from a config.
|
|
148
|
-
builder = await new EchoTestBuilder().open();
|
|
149
|
-
const { db: db1 } = await builder.createDatabase({ indexing: { vector: true } });
|
|
150
|
-
db = db1;
|
|
151
|
-
executor = new FunctionExecutor(
|
|
152
|
-
new ServiceContainer().setServices({
|
|
153
|
-
ai: todo(),
|
|
154
|
-
database: {
|
|
155
|
-
db,
|
|
156
|
-
},
|
|
157
|
-
}),
|
|
158
|
-
);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
test('keeps transcript outline', { timeout: 1000_000 }, async () => {
|
|
162
|
-
const { transcriptMessages } = createTestData();
|
|
163
|
-
|
|
164
|
-
const summary = DataType.makeText();
|
|
165
|
-
|
|
166
|
-
const summaries = [];
|
|
167
|
-
|
|
168
|
-
for (let i = 0; i < transcriptMessages.length; i++) {
|
|
169
|
-
const blocks = transcriptMessages.slice(Math.max(0, i - 2), i + 1);
|
|
170
|
-
const result = await executor.invoke(summarizationFn, {
|
|
171
|
-
document: summary,
|
|
172
|
-
transcript: blocks,
|
|
173
|
-
});
|
|
174
|
-
summary.content = result.content;
|
|
175
|
-
summaries.push(DataType.makeText(result.content));
|
|
176
|
-
|
|
177
|
-
console.log(blocks.at(-1));
|
|
178
|
-
console.log();
|
|
179
|
-
console.log(summary.content);
|
|
180
|
-
console.log();
|
|
181
|
-
|
|
182
|
-
if (i % 3 === 0 && i > 0) {
|
|
183
|
-
const history: any = summaries.slice(-5);
|
|
184
|
-
const result = await executor.invoke(refinementFn, {
|
|
185
|
-
summaries: history,
|
|
186
|
-
});
|
|
187
|
-
summary.content = result.summary.content;
|
|
188
|
-
summaries.push(DataType.makeText(result.summary.content));
|
|
189
|
-
|
|
190
|
-
console.log('REFINED');
|
|
191
|
-
console.log();
|
|
192
|
-
console.log(summary.content);
|
|
193
|
-
console.log();
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
});
|
|
File without changes
|
|
File without changes
|
/package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl}
RENAMED
|
File without changes
|