@dxos/assistant 0.8.4-main.b97322e → 0.8.4-main.bc2380dfbc

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.
Files changed (196) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +2 -1
  3. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  4. package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
  5. package/dist/lib/neutral/extraction/index.mjs +204 -0
  6. package/dist/lib/neutral/extraction/index.mjs.map +7 -0
  7. package/dist/lib/neutral/index.mjs +1755 -0
  8. package/dist/lib/neutral/index.mjs.map +7 -0
  9. package/dist/lib/neutral/meta.json +1 -0
  10. package/dist/types/src/extraction/extraction-ner-function/index.d.ts +0 -1
  11. package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -1
  12. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  13. package/dist/types/src/extraction/extraction.d.ts +1066 -273
  14. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  15. package/dist/types/src/extraction/index.d.ts +0 -1
  16. package/dist/types/src/extraction/index.d.ts.map +1 -1
  17. package/dist/types/src/extraction/quotes.d.ts +3 -3
  18. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  19. package/dist/types/src/functions/services.d.ts +15 -4
  20. package/dist/types/src/functions/services.d.ts.map +1 -1
  21. package/dist/types/src/index.d.ts +2 -5
  22. package/dist/types/src/index.d.ts.map +1 -1
  23. package/dist/types/src/request/AiRequest.d.ts +104 -0
  24. package/dist/types/src/request/AiRequest.d.ts.map +1 -0
  25. package/dist/types/src/request/artifact-diff.d.ts +27 -0
  26. package/dist/types/src/request/artifact-diff.d.ts.map +1 -0
  27. package/dist/types/src/request/format.d.ts +15 -0
  28. package/dist/types/src/request/format.d.ts.map +1 -0
  29. package/dist/types/src/request/index.d.ts +4 -0
  30. package/dist/types/src/request/index.d.ts.map +1 -0
  31. package/dist/types/src/request/observer.d.ts +23 -0
  32. package/dist/types/src/request/observer.d.ts.map +1 -0
  33. package/dist/types/src/{session → request}/version-pin.d.ts +4 -4
  34. package/dist/types/src/request/version-pin.d.ts.map +1 -0
  35. package/dist/types/src/session/AiContext.d.ts +154 -0
  36. package/dist/types/src/session/AiContext.d.ts.map +1 -0
  37. package/dist/types/src/session/AiContext.test.d.ts +2 -0
  38. package/dist/types/src/session/AiContext.test.d.ts.map +1 -0
  39. package/dist/types/src/session/AiSession.d.ts +73 -0
  40. package/dist/types/src/session/AiSession.d.ts.map +1 -0
  41. package/dist/types/src/session/index.d.ts +3 -2
  42. package/dist/types/src/session/index.d.ts.map +1 -1
  43. package/dist/types/src/session/toolkit.d.ts +16 -0
  44. package/dist/types/src/session/toolkit.d.ts.map +1 -0
  45. package/dist/types/src/templates/index.d.ts +1 -1
  46. package/dist/types/src/templates/system.d.ts +17 -0
  47. package/dist/types/src/templates/system.d.ts.map +1 -0
  48. package/dist/types/src/util/artifact.d.ts +27 -0
  49. package/dist/types/src/util/artifact.d.ts.map +1 -0
  50. package/dist/types/src/util/diff.d.ts +17 -0
  51. package/dist/types/src/util/diff.d.ts.map +1 -0
  52. package/dist/types/src/util/diff.test.d.ts +2 -0
  53. package/dist/types/src/util/diff.test.d.ts.map +1 -0
  54. package/dist/types/src/util/error-handling.d.ts +4 -0
  55. package/dist/types/src/util/error-handling.d.ts.map +1 -0
  56. package/dist/types/src/util/errors.d.ts +67 -0
  57. package/dist/types/src/util/errors.d.ts.map +1 -0
  58. package/dist/types/src/util/execution-graph.d.ts +85 -0
  59. package/dist/types/src/util/execution-graph.d.ts.map +1 -0
  60. package/dist/types/src/util/execution-graph.test.d.ts +2 -0
  61. package/dist/types/src/util/execution-graph.test.d.ts.map +1 -0
  62. package/dist/types/src/util/index.d.ts +7 -0
  63. package/dist/types/src/util/index.d.ts.map +1 -0
  64. package/dist/types/src/util/tracing.d.ts +233 -0
  65. package/dist/types/src/util/tracing.d.ts.map +1 -0
  66. package/dist/types/tsconfig.tsbuildinfo +1 -1
  67. package/package.json +42 -36
  68. package/src/extraction/extraction-ner-function/index.ts +0 -1
  69. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
  70. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +2 -0
  71. package/src/extraction/extraction.ts +44 -15
  72. package/src/extraction/index.ts +0 -1
  73. package/src/extraction/quotes.test.ts +3 -3
  74. package/src/extraction/quotes.ts +6 -7
  75. package/src/functions/services.ts +173 -98
  76. package/src/index.ts +2 -5
  77. package/src/request/AiRequest.ts +348 -0
  78. package/src/request/artifact-diff.ts +41 -0
  79. package/src/request/format.ts +147 -0
  80. package/src/request/index.ts +8 -0
  81. package/src/request/observer.ts +62 -0
  82. package/src/{session → request}/version-pin.ts +8 -8
  83. package/src/session/AiContext.test.ts +53 -0
  84. package/src/session/AiContext.ts +403 -0
  85. package/src/session/AiSession.ts +324 -0
  86. package/src/session/index.ts +4 -2
  87. package/src/session/toolkit.ts +47 -0
  88. package/src/templates/index.ts +1 -1
  89. package/src/templates/instructions/format.tpl +57 -0
  90. package/src/templates/instructions/system.tpl +96 -0
  91. package/src/templates/system.ts +42 -0
  92. package/src/typings.d.ts +5 -0
  93. package/src/util/artifact.ts +88 -0
  94. package/src/util/diff.test.ts +74 -0
  95. package/src/util/diff.ts +93 -0
  96. package/src/util/error-handling.ts +42 -0
  97. package/src/util/errors.ts +15 -0
  98. package/src/util/execution-graph.test.ts +740 -0
  99. package/src/util/execution-graph.ts +519 -0
  100. package/src/util/index.ts +10 -0
  101. package/src/util/tracing.ts +57 -0
  102. package/dist/lib/browser/index.mjs +0 -1831
  103. package/dist/lib/browser/index.mjs.map +0 -7
  104. package/dist/lib/browser/meta.json +0 -1
  105. package/dist/lib/node-esm/index.mjs +0 -1833
  106. package/dist/lib/node-esm/index.mjs.map +0 -7
  107. package/dist/lib/node-esm/meta.json +0 -1
  108. package/dist/types/bin/chat.d.ts +0 -2
  109. package/dist/types/bin/chat.d.ts.map +0 -1
  110. package/dist/types/src/context/binder.d.ts +0 -34
  111. package/dist/types/src/context/binder.d.ts.map +0 -1
  112. package/dist/types/src/context/binding.d.ts +0 -56
  113. package/dist/types/src/context/binding.d.ts.map +0 -1
  114. package/dist/types/src/context/index.d.ts +0 -3
  115. package/dist/types/src/context/index.d.ts.map +0 -1
  116. package/dist/types/src/conversation/conversation.d.ts +0 -38
  117. package/dist/types/src/conversation/conversation.d.ts.map +0 -1
  118. package/dist/types/src/conversation/index.d.ts +0 -2
  119. package/dist/types/src/conversation/index.d.ts.map +0 -1
  120. package/dist/types/src/errors.d.ts +0 -38
  121. package/dist/types/src/errors.d.ts.map +0 -1
  122. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +0 -4
  123. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +0 -1
  124. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +0 -2
  125. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +0 -1
  126. package/dist/types/src/extraction/extraction-llm-function/index.d.ts +0 -2
  127. package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +0 -1
  128. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +0 -302
  129. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +0 -1
  130. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +0 -2
  131. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +0 -1
  132. package/dist/types/src/research/exa.d.ts +0 -12
  133. package/dist/types/src/research/exa.d.ts.map +0 -1
  134. package/dist/types/src/research/graph.d.ts +0 -51
  135. package/dist/types/src/research/graph.d.ts.map +0 -1
  136. package/dist/types/src/research/graph.test.d.ts +0 -2
  137. package/dist/types/src/research/graph.test.d.ts.map +0 -1
  138. package/dist/types/src/research/index.d.ts +0 -4
  139. package/dist/types/src/research/index.d.ts.map +0 -1
  140. package/dist/types/src/research/research.d.ts +0 -10
  141. package/dist/types/src/research/research.d.ts.map +0 -1
  142. package/dist/types/src/research/research.test.d.ts +0 -2
  143. package/dist/types/src/research/research.test.d.ts.map +0 -1
  144. package/dist/types/src/session/session.d.ts +0 -260
  145. package/dist/types/src/session/session.d.ts.map +0 -1
  146. package/dist/types/src/session/session.test.d.ts +0 -2
  147. package/dist/types/src/session/session.test.d.ts.map +0 -1
  148. package/dist/types/src/session/version-pin.d.ts.map +0 -1
  149. package/dist/types/src/session/version-pin.test.d.ts +0 -2
  150. package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
  151. package/dist/types/src/summarization/summarization.test.d.ts +0 -2
  152. package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
  153. package/dist/types/src/templates/prompt.d.ts +0 -30
  154. package/dist/types/src/templates/prompt.d.ts.map +0 -1
  155. package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
  156. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
  157. package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
  158. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
  159. package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
  160. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
  161. package/dist/types/src/testing/index.d.ts +0 -3
  162. package/dist/types/src/testing/index.d.ts.map +0 -1
  163. package/dist/types/src/types.d.ts +0 -13
  164. package/dist/types/src/types.d.ts.map +0 -1
  165. package/src/context/binder.ts +0 -103
  166. package/src/context/binding.ts +0 -32
  167. package/src/context/index.ts +0 -6
  168. package/src/conversation/conversation.ts +0 -96
  169. package/src/conversation/index.ts +0 -5
  170. package/src/errors.ts +0 -10
  171. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -83
  172. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -70
  173. package/src/extraction/extraction-llm-function/index.ts +0 -5
  174. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +0 -115
  175. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +0 -57
  176. package/src/research/exa.ts +0 -97
  177. package/src/research/graph.test.ts +0 -64
  178. package/src/research/graph.ts +0 -369
  179. package/src/research/index.ts +0 -7
  180. package/src/research/instructions-research.tpl +0 -98
  181. package/src/research/research.test.ts +0 -190
  182. package/src/research/research.ts +0 -87
  183. package/src/session/__snapshots__/session.test.ts.snap +0 -32
  184. package/src/session/session.test.ts +0 -310
  185. package/src/session/session.ts +0 -336
  186. package/src/session/version-pin.test.ts +0 -7
  187. package/src/summarization/summarization.test.ts +0 -208
  188. package/src/templates/assistant.tpl +0 -111
  189. package/src/templates/prompt.ts +0 -45
  190. package/src/testing/exa-search-1748337321991.ts +0 -131
  191. package/src/testing/exa-search-1748337331526.ts +0 -144
  192. package/src/testing/exa-search-1748337344119.ts +0 -133
  193. package/src/testing/index.ts +0 -11
  194. package/src/typedefs.d.ts +0 -8
  195. package/src/types.ts +0 -47
  196. /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
@@ -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({ content: 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({ content: 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({ content: '' }));
69
+ const accessor = createDocAccessor(text, ['content']);
70
+ const result = applyDiffs(accessor, ['+ # Shopping list']);
71
+
72
+ expect(result).toBe('# Shopping list');
73
+ });
74
+ });
@@ -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
+ };
@@ -0,0 +1,42 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as AiError from '@effect/ai/AiError';
6
+ import * as HttpClientError from '@effect/platform/HttpClientError';
7
+ import * as Effect from 'effect/Effect';
8
+ import * as Schema from 'effect/Schema';
9
+
10
+ import { AiModelError } from './errors';
11
+
12
+ const AnthropicErrorResponse = Schema.Struct({
13
+ type: Schema.Literal('error'),
14
+ error: Schema.Struct({
15
+ type: Schema.String,
16
+ message: Schema.String,
17
+ }),
18
+ });
19
+
20
+ // TODO(dmaretskyi): Needs rework.
21
+ export const mapAiError = (err: AiError.AiError): Effect.Effect<AiError.AiError> =>
22
+ Effect.gen(function* () {
23
+ const cause = err.cause;
24
+ if (HttpClientError.isHttpClientError(cause) && cause.reason === 'StatusCode') {
25
+ const body = yield* cause.response.json.pipe(
26
+ Effect.flatMap(Schema.decodeUnknown(AnthropicErrorResponse, { exact: false })),
27
+ );
28
+
29
+ const parsedCause = new AiModelError({
30
+ message: body.error.message,
31
+ context: { model: 'anthropic', type: body.error.type },
32
+ });
33
+
34
+ return AiError.UnknownError.make({
35
+ description: body.error.message,
36
+ module: err.module,
37
+ method: err.method,
38
+ cause: parsedCause,
39
+ });
40
+ }
41
+ return err;
42
+ }).pipe(Effect.catchAll(() => Effect.succeed(err)));
@@ -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') {}