@dxos/assistant 0.8.3 → 0.8.4-main.1068cf700f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/neutral/chunk-ZPC54VRM.mjs +596 -0
- package/dist/lib/neutral/chunk-ZPC54VRM.mjs.map +7 -0
- package/dist/lib/neutral/extraction/index.mjs +225 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +929 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/testing/index.mjs +38 -0
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +138 -0
- package/dist/types/src/conversation/context.d.ts.map +1 -0
- package/dist/types/src/conversation/context.test.d.ts +2 -0
- package/dist/types/src/conversation/context.test.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.d.ts +66 -0
- package/dist/types/src/conversation/conversation.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.test.d.ts +2 -0
- package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
- package/dist/types/src/conversation/index.d.ts +3 -0
- package/dist/types/src/conversation/index.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 +67 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/execution-graph.d.ts +85 -0
- package/dist/types/src/execution-graph.d.ts.map +1 -0
- package/dist/types/src/execution-graph.test.d.ts +2 -0
- package/dist/types/src/execution-graph.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +1 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +279 -140
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +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 +345 -212
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +3 -3
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/index.d.ts +2 -0
- package/dist/types/src/functions/index.d.ts.map +1 -0
- package/dist/types/src/functions/services.d.ts +19 -0
- package/dist/types/src/functions/services.d.ts.map +1 -0
- 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 +6 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/GenericToolkit.d.ts +107 -0
- package/dist/types/src/session/GenericToolkit.d.ts.map +1 -0
- package/dist/types/src/session/artifact-diff.d.ts +27 -0
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
- package/dist/types/src/session/error-handling.d.ts +4 -0
- package/dist/types/src/session/error-handling.d.ts.map +1 -0
- package/dist/types/src/session/format.d.ts +12 -0
- package/dist/types/src/session/format.d.ts.map +1 -0
- package/dist/types/src/session/format.test.d.ts +2 -0
- package/dist/types/src/session/format.test.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +5 -0
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +23 -0
- package/dist/types/src/session/observer.d.ts.map +1 -0
- package/dist/types/src/session/session.d.ts +41 -246
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +14 -0
- package/dist/types/src/session/toolkit.d.ts.map +1 -0
- package/dist/types/src/session/version-pin.d.ts +5 -5
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/templates/index.d.ts +2 -0
- package/dist/types/src/templates/index.d.ts.map +1 -0
- package/dist/types/src/templates/system.d.ts +18 -0
- package/dist/types/src/templates/system.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +1 -2
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +22 -0
- package/dist/types/src/testing/layer.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts +28 -0
- package/dist/types/src/types/artifact.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +2 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +58 -38
- package/src/conversation/context.test.ts +46 -0
- package/src/conversation/context.ts +330 -0
- package/src/conversation/conversation.test.ts +65 -0
- package/src/conversation/conversation.ts +203 -0
- package/src/conversation/index.ts +6 -0
- package/src/diff.test.ts +74 -0
- package/src/diff.ts +93 -0
- package/src/errors.ts +15 -0
- package/src/execution-graph.test.ts +740 -0
- package/src/execution-graph.ts +509 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +14 -25
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +48 -51
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +23 -22
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +43 -41
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
- package/src/extraction/extraction-ner-function/named-entity-recognition.ts +3 -1
- package/src/extraction/extraction.ts +10 -9
- package/src/extraction/quotes.test.ts +3 -3
- package/src/extraction/quotes.ts +6 -7
- package/src/functions/index.ts +5 -0
- package/src/functions/services.ts +187 -0
- package/src/functions.conversations.json +1 -0
- package/src/functions.test.ts +74 -0
- package/src/index.ts +6 -3
- package/src/session/GenericToolkit.ts +160 -0
- package/src/session/artifact-diff.ts +41 -0
- package/src/session/error-handling.ts +41 -0
- package/src/session/format.test.ts +124 -0
- package/src/session/format.ts +143 -0
- package/src/session/index.ts +6 -0
- package/src/session/observer.ts +61 -0
- package/src/session/session.conversations.json +1 -0
- package/src/session/session.test.ts +106 -173
- package/src/session/session.ts +181 -495
- package/src/session/toolkit.ts +39 -0
- package/src/session/version-pin.ts +12 -12
- package/src/templates/index.ts +5 -0
- package/src/templates/instructions/format.tpl +57 -0
- package/src/templates/instructions/system.tpl +48 -0
- package/src/templates/system.ts +41 -0
- package/src/testing/index.ts +2 -8
- package/src/testing/layer.ts +84 -0
- package/src/types/artifact.ts +89 -0
- package/src/types/index.ts +5 -0
- package/src/typings.d.ts +5 -0
- package/dist/lib/browser/index.mjs +0 -2158
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/node/index.cjs +0 -2178
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
- package/dist/lib/node-esm/index.mjs +0 -2160
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/types/bin/chat.d.ts +0 -2
- package/dist/types/bin/chat.d.ts.map +0 -1
- package/dist/types/src/blueprint/ai.test.d.ts +0 -2
- package/dist/types/src/blueprint/ai.test.d.ts.map +0 -1
- package/dist/types/src/blueprint/blueprint.d.ts +0 -55
- package/dist/types/src/blueprint/blueprint.d.ts.map +0 -1
- package/dist/types/src/blueprint/blueprint.test.d.ts +0 -2
- package/dist/types/src/blueprint/blueprint.test.d.ts.map +0 -1
- package/dist/types/src/blueprint/index.d.ts +0 -4
- package/dist/types/src/blueprint/index.d.ts.map +0 -1
- package/dist/types/src/blueprint/logger.d.ts +0 -25
- package/dist/types/src/blueprint/logger.d.ts.map +0 -1
- package/dist/types/src/blueprint/machine.d.ts +0 -143
- package/dist/types/src/blueprint/machine.d.ts.map +0 -1
- package/dist/types/src/blueprint/test-data.d.ts +0 -3
- package/dist/types/src/blueprint/test-data.d.ts.map +0 -1
- package/dist/types/src/research/exa.d.ts +0 -7
- package/dist/types/src/research/exa.d.ts.map +0 -1
- package/dist/types/src/research/graph.d.ts +0 -41
- package/dist/types/src/research/graph.d.ts.map +0 -1
- package/dist/types/src/research/graph.test.d.ts +0 -2
- package/dist/types/src/research/graph.test.d.ts.map +0 -1
- package/dist/types/src/research/index.d.ts +0 -4
- package/dist/types/src/research/index.d.ts.map +0 -1
- package/dist/types/src/research/research.d.ts +0 -10
- package/dist/types/src/research/research.d.ts.map +0 -1
- package/dist/types/src/research/research.test.d.ts +0 -2
- package/dist/types/src/research/research.test.d.ts.map +0 -1
- package/dist/types/src/session/version-pin.test.d.ts +0 -2
- package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
- package/dist/types/src/summarization/summarization.test.d.ts +0 -2
- package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
- package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
- package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
- package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
- package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
- package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
- package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -3
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/blueprint/ai.test.ts +0 -156
- package/src/blueprint/blueprint.test.ts +0 -151
- package/src/blueprint/blueprint.ts +0 -75
- package/src/blueprint/index.ts +0 -7
- package/src/blueprint/logger.ts +0 -155
- package/src/blueprint/machine.ts +0 -251
- package/src/blueprint/test-data.ts +0 -55
- package/src/research/exa.ts +0 -100
- package/src/research/graph.test.ts +0 -15
- package/src/research/graph.ts +0 -337
- package/src/research/index.ts +0 -7
- package/src/research/instructions-research.tpl +0 -97
- package/src/research/research.test.ts +0 -190
- package/src/research/research.ts +0 -89
- package/src/session/version-pin.test.ts +0 -26
- package/src/summarization/summarization.test.ts +0 -221
- package/src/testing/exa-search-1748337321991.ts +0 -131
- package/src/testing/exa-search-1748337331526.ts +0 -144
- package/src/testing/exa-search-1748337344119.ts +0 -133
- package/src/typedefs.d.ts +0 -8
- package/src/types.ts +0 -8
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as Tool from '@effect/ai/Tool';
|
|
6
|
+
import type * as Toolkit from '@effect/ai/Toolkit';
|
|
7
|
+
import type { Registry } from '@effect-atom/atom-react';
|
|
8
|
+
import * as Context from 'effect/Context';
|
|
9
|
+
import * as Effect from 'effect/Effect';
|
|
10
|
+
import * as Layer from 'effect/Layer';
|
|
11
|
+
|
|
12
|
+
import { type ToolExecutionService, type ToolResolverService } from '@dxos/ai';
|
|
13
|
+
import { Resource } from '@dxos/context';
|
|
14
|
+
import { Obj } from '@dxos/echo';
|
|
15
|
+
import { type Queue } from '@dxos/echo-db';
|
|
16
|
+
import { acquireReleaseResource } from '@dxos/effect';
|
|
17
|
+
import { QueueService } from '@dxos/functions';
|
|
18
|
+
import { invariant } from '@dxos/invariant';
|
|
19
|
+
import { log } from '@dxos/log';
|
|
20
|
+
import { Message } from '@dxos/types';
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
AiSession,
|
|
24
|
+
type AiSessionRunError,
|
|
25
|
+
type AiSessionRunRequirements,
|
|
26
|
+
type GenerationObserver,
|
|
27
|
+
createToolkit,
|
|
28
|
+
} from '../session';
|
|
29
|
+
|
|
30
|
+
import { AiContextBinder, AiContextService, type ContextBinding } from './context';
|
|
31
|
+
|
|
32
|
+
export interface AiConversationRunProps {
|
|
33
|
+
prompt: string;
|
|
34
|
+
system?: string;
|
|
35
|
+
observer?: GenerationObserver;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type AiConversationOptions = {
|
|
39
|
+
queue: Queue<Message.Message | ContextBinding>;
|
|
40
|
+
toolkit?: Toolkit.Any;
|
|
41
|
+
registry?: Registry.Registry;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Durable conversation state (initiated by users and agents) backed by a Queue.
|
|
46
|
+
* Executes tools based on AI responses and supports cancellation of in-progress requests.
|
|
47
|
+
*/
|
|
48
|
+
export class AiConversation extends Resource {
|
|
49
|
+
/**
|
|
50
|
+
* Blueprints and objects bound to the conversation.
|
|
51
|
+
*/
|
|
52
|
+
private readonly _binder: AiContextBinder;
|
|
53
|
+
private readonly _queue: Queue<Message.Message | ContextBinding>;
|
|
54
|
+
private readonly _toolkit?: Toolkit.Any;
|
|
55
|
+
|
|
56
|
+
public constructor(options: AiConversationOptions) {
|
|
57
|
+
super();
|
|
58
|
+
this._queue = options.queue;
|
|
59
|
+
this._toolkit = options.toolkit;
|
|
60
|
+
invariant(this._queue);
|
|
61
|
+
this._binder = new AiContextBinder({ queue: this._queue, registry: options.registry });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
protected override async _open(): Promise<void> {
|
|
65
|
+
await this._binder.open(this._ctx);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public get queue() {
|
|
69
|
+
return this._queue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public get context() {
|
|
73
|
+
return this._binder;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public get toolkit() {
|
|
77
|
+
return this._toolkit;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public async getHistory(): Promise<Message.Message[]> {
|
|
81
|
+
const queueItems = await this._queue.queryObjects(); // TODO(burdon): Update.
|
|
82
|
+
return queueItems.filter(Obj.instanceOf(Message.Message));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
getTools(): Effect.Effect<Record<string, Tool.Any>, never, ToolExecutionService | ToolResolverService> {
|
|
86
|
+
return Effect.gen(this, function* () {
|
|
87
|
+
const blueprints = this.context.getBlueprints();
|
|
88
|
+
const tookit = yield* createToolkit({ toolkit: this._toolkit, blueprints });
|
|
89
|
+
return tookit.tools;
|
|
90
|
+
}).pipe(Effect.orDie);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Creates a new cancelable request effect.
|
|
95
|
+
*/
|
|
96
|
+
public createRequest(
|
|
97
|
+
params: AiConversationRunProps,
|
|
98
|
+
): Effect.Effect<Message.Message[], AiSessionRunError, AiSessionRunRequirements> {
|
|
99
|
+
return Effect.gen(this, function* () {
|
|
100
|
+
const history = yield* Effect.promise(() => this.getHistory());
|
|
101
|
+
|
|
102
|
+
// Create toolkit.
|
|
103
|
+
const blueprints = this.context.getBlueprints();
|
|
104
|
+
const toolkit = yield* createToolkit({
|
|
105
|
+
toolkit: this._toolkit,
|
|
106
|
+
blueprints,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// Context objects.
|
|
110
|
+
const objects = this.context.getObjects();
|
|
111
|
+
|
|
112
|
+
log('run', {
|
|
113
|
+
history: history.length,
|
|
114
|
+
blueprints: blueprints.length,
|
|
115
|
+
tools: Object.keys(toolkit.tools).length,
|
|
116
|
+
objects: objects.length,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Process request.
|
|
120
|
+
const session = new AiSession();
|
|
121
|
+
const messages = yield* session.run({ history, blueprints, toolkit, objects, ...params }).pipe(
|
|
122
|
+
Effect.provide(
|
|
123
|
+
Layer.mergeAll(
|
|
124
|
+
Layer.succeed(AiContextService, {
|
|
125
|
+
binder: this.context,
|
|
126
|
+
}),
|
|
127
|
+
Layer.succeed(AiConversationService, this),
|
|
128
|
+
),
|
|
129
|
+
),
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
log('result', {
|
|
133
|
+
messages: messages.length,
|
|
134
|
+
duration: session.duration,
|
|
135
|
+
toolCalls: session.toolCalls,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// Append to queue.
|
|
139
|
+
yield* Effect.promise(() => this._queue.append(messages));
|
|
140
|
+
return messages;
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Gives access to the ai conversation.
|
|
147
|
+
*/
|
|
148
|
+
export class AiConversationService extends Context.Tag('@dxos/assistant/AiConversationService')<
|
|
149
|
+
AiConversationService,
|
|
150
|
+
AiConversation
|
|
151
|
+
>() {
|
|
152
|
+
/**
|
|
153
|
+
* Create a new conversation layer from options.
|
|
154
|
+
*/
|
|
155
|
+
static layer = (options: AiConversationOptions): Layer.Layer<AiConversationService | AiContextService> =>
|
|
156
|
+
aiContextFromConversation.pipe(
|
|
157
|
+
Layer.provideMerge(
|
|
158
|
+
Layer.scoped(
|
|
159
|
+
AiConversationService,
|
|
160
|
+
Effect.gen(function* () {
|
|
161
|
+
const conversation = yield* acquireReleaseResource(() => new AiConversation(options));
|
|
162
|
+
return conversation;
|
|
163
|
+
}),
|
|
164
|
+
),
|
|
165
|
+
),
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Create a new conversation with a new queue.
|
|
170
|
+
*/
|
|
171
|
+
static layerNewQueue = (
|
|
172
|
+
options?: Omit<AiConversationOptions, 'queue'>,
|
|
173
|
+
): Layer.Layer<AiConversationService | AiContextService, never, QueueService> =>
|
|
174
|
+
Layer.unwrapScoped(
|
|
175
|
+
Effect.gen(function* () {
|
|
176
|
+
return AiConversationService.layer({
|
|
177
|
+
...options,
|
|
178
|
+
queue: yield* QueueService.createQueue<Message.Message | ContextBinding>(),
|
|
179
|
+
});
|
|
180
|
+
}),
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Run a prompt in the current conversation.
|
|
185
|
+
*/
|
|
186
|
+
static run = (
|
|
187
|
+
params: AiConversationRunProps,
|
|
188
|
+
): Effect.Effect<Message.Message[], AiSessionRunError, AiSessionRunRequirements | AiConversationService> =>
|
|
189
|
+
Effect.gen(function* () {
|
|
190
|
+
const conversation = yield* AiConversationService;
|
|
191
|
+
return yield* conversation.createRequest(params);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const aiContextFromConversation = Layer.effect(
|
|
196
|
+
AiContextService,
|
|
197
|
+
Effect.gen(function* () {
|
|
198
|
+
const conversation = yield* AiConversationService;
|
|
199
|
+
return {
|
|
200
|
+
binder: conversation.context,
|
|
201
|
+
};
|
|
202
|
+
}),
|
|
203
|
+
);
|
package/src/diff.test.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { createDocAccessor } from '@dxos/echo-db';
|
|
8
|
+
import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
9
|
+
import { Text } from '@dxos/schema';
|
|
10
|
+
import { trim } from '@dxos/util';
|
|
11
|
+
|
|
12
|
+
import { applyDiffs } from './diff';
|
|
13
|
+
|
|
14
|
+
describe('diff', () => {
|
|
15
|
+
let builder: EchoTestBuilder;
|
|
16
|
+
beforeEach(async () => {
|
|
17
|
+
builder = await new EchoTestBuilder().open();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(async () => {
|
|
21
|
+
await builder.close();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should apply diffs', async () => {
|
|
25
|
+
const { db } = await builder.createDatabase({ types: [Text.Text] });
|
|
26
|
+
|
|
27
|
+
const document = trim`
|
|
28
|
+
# Hello World
|
|
29
|
+
There'z a typo in this sentence.
|
|
30
|
+
And another oon here.
|
|
31
|
+
But not this one.
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
const text = db.add(Text.make(document));
|
|
35
|
+
const accessor = createDocAccessor(text, ['content']);
|
|
36
|
+
const result = applyDiffs(accessor, [
|
|
37
|
+
`- There'z a typo in this sentence.`,
|
|
38
|
+
`+ There is a typo in this sentence.`,
|
|
39
|
+
`- And another oon here.`,
|
|
40
|
+
`+ And another one here.`,
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
expect(result).toBe(trim`
|
|
44
|
+
# Hello World
|
|
45
|
+
There is a typo in this sentence.
|
|
46
|
+
And another one here.
|
|
47
|
+
But not this one.
|
|
48
|
+
`);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should apply diffs that append content at the end', async () => {
|
|
52
|
+
const { db } = await builder.createDatabase({ types: [Text.Text] });
|
|
53
|
+
|
|
54
|
+
const document = '# Shopping list';
|
|
55
|
+
|
|
56
|
+
const text = db.add(Text.make(document));
|
|
57
|
+
const accessor = createDocAccessor(text, ['content']);
|
|
58
|
+
const result = applyDiffs(accessor, ['- # Shopping list', '+ # Shopping list\n- Milk']);
|
|
59
|
+
|
|
60
|
+
expect(result).toBe('# Shopping list\n- Milk');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should append text to an empty document', async () => {
|
|
64
|
+
const builder = new EchoTestBuilder();
|
|
65
|
+
const { db, graph } = await builder.createDatabase();
|
|
66
|
+
await graph.schemaRegistry.register([Text.Text]);
|
|
67
|
+
|
|
68
|
+
const text = db.add(Text.make(''));
|
|
69
|
+
const accessor = createDocAccessor(text, ['content']);
|
|
70
|
+
const result = applyDiffs(accessor, ['+ # Shopping list']);
|
|
71
|
+
|
|
72
|
+
expect(result).toBe('# Shopping list');
|
|
73
|
+
});
|
|
74
|
+
});
|
package/src/diff.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { next as A, type Doc } from '@automerge/automerge';
|
|
6
|
+
|
|
7
|
+
import { DocAccessor, toCursorRange } from '@dxos/echo-db';
|
|
8
|
+
import { log } from '@dxos/log';
|
|
9
|
+
import { isNonNullable } from '@dxos/util';
|
|
10
|
+
|
|
11
|
+
// Relevant info:
|
|
12
|
+
// https://github.com/Minahil-official/GPT-4.1-Promting-Guide?tab=readme-ov-file#-apply-patch-tool-format-v4a-diff
|
|
13
|
+
|
|
14
|
+
// TODO(burdon): Anthropic only.
|
|
15
|
+
// https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/text-editor-tool#example-str-replace-command
|
|
16
|
+
// AI_TOOL_NOT_FOUND: str_replace_based_edit_tool
|
|
17
|
+
// 'str_replace_based_edit_tool',
|
|
18
|
+
|
|
19
|
+
// TOOD(burdon): Support append to end (no "-").
|
|
20
|
+
|
|
21
|
+
export type Diff = {
|
|
22
|
+
match: string;
|
|
23
|
+
replace: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Convert a list of diffs into a list of operations.
|
|
28
|
+
*/
|
|
29
|
+
export const reduceDiffs = (diffs: readonly string[]): Diff[] => {
|
|
30
|
+
return diffs.reduce<Diff[]>((acc, diff) => {
|
|
31
|
+
const current = acc[acc.length - 1];
|
|
32
|
+
const [op, ...rest] = diff.split(' ');
|
|
33
|
+
switch (op) {
|
|
34
|
+
case '-': {
|
|
35
|
+
if (current && !current.match) {
|
|
36
|
+
current.match += rest.join(' ');
|
|
37
|
+
} else {
|
|
38
|
+
acc.push({ match: rest.join(' '), replace: '' });
|
|
39
|
+
}
|
|
40
|
+
return acc;
|
|
41
|
+
}
|
|
42
|
+
case '+': {
|
|
43
|
+
if (current && !current.replace) {
|
|
44
|
+
current.replace += rest.join(' ');
|
|
45
|
+
} else {
|
|
46
|
+
acc.push({ match: '', replace: rest.join(' ') });
|
|
47
|
+
}
|
|
48
|
+
return acc;
|
|
49
|
+
}
|
|
50
|
+
default: {
|
|
51
|
+
throw new Error(`Invalid diff format: each line must start with "- " or "+ ", got: ${JSON.stringify(diff)}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}, []);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const computeDiffsWithCursors = <T>(accessor: DocAccessor<T>, diffs: readonly string[]) => {
|
|
58
|
+
return reduceDiffs(diffs)
|
|
59
|
+
.map((diff) => {
|
|
60
|
+
const text = DocAccessor.getValue<string>(accessor);
|
|
61
|
+
const idx = text.indexOf(diff.match);
|
|
62
|
+
if (idx !== -1) {
|
|
63
|
+
return { cursor: toCursorRange(accessor, idx, idx + diff.match.length), text: diff.replace };
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
.filter(isNonNullable);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const applyDiffs = <T>(
|
|
70
|
+
accessor: DocAccessor<T>,
|
|
71
|
+
diffs: readonly string[],
|
|
72
|
+
{ errorOnNotFound = false }: { errorOnNotFound?: boolean } = {},
|
|
73
|
+
): string => {
|
|
74
|
+
for (const diff of reduceDiffs(diffs)) {
|
|
75
|
+
accessor.handle.change((doc: Doc<T>) => {
|
|
76
|
+
const text = DocAccessor.getValue<string>(accessor);
|
|
77
|
+
const idx = text.indexOf(diff.match);
|
|
78
|
+
if (idx !== -1) {
|
|
79
|
+
// TODO(burdon): Replace smallest substring.
|
|
80
|
+
// NOTE: This only replaces the first match.
|
|
81
|
+
A.splice(doc, accessor.path as A.Prop[], idx, diff.match.length, diff.replace);
|
|
82
|
+
} else {
|
|
83
|
+
if (errorOnNotFound) {
|
|
84
|
+
throw new Error(`Diff not found: ${diff.match}`);
|
|
85
|
+
} else {
|
|
86
|
+
log.warn('diff not found', diff);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return DocAccessor.getValue<string>(accessor);
|
|
93
|
+
};
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { BaseError } from '@dxos/errors';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Generic error from AI model.
|
|
9
|
+
*/
|
|
10
|
+
export class AiModelError extends BaseError.extend('AiModelError', 'AI model error') {}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Generic error for AI agent execution.
|
|
14
|
+
*/
|
|
15
|
+
export class AiAssistantError extends BaseError.extend('AiAssistantError', 'AI assistant error') {}
|