@dxos/assistant 0.8.4-main.84f28bd → 0.8.4-main.ae835ea

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 (179) hide show
  1. package/dist/lib/browser/chunk-4VIJTKPM.mjs +354 -0
  2. package/dist/lib/browser/chunk-4VIJTKPM.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-UB26N5UF.mjs +89 -0
  4. package/dist/lib/browser/chunk-UB26N5UF.mjs.map +7 -0
  5. package/dist/lib/browser/extraction/index.mjs +272 -0
  6. package/dist/lib/browser/extraction/index.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +754 -1873
  8. package/dist/lib/browser/index.mjs.map +4 -4
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +13 -0
  11. package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs.map +7 -0
  12. package/dist/lib/node-esm/chunk-AUUC7II4.mjs +356 -0
  13. package/dist/lib/node-esm/chunk-AUUC7II4.mjs.map +7 -0
  14. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +91 -0
  15. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +7 -0
  16. package/dist/lib/node-esm/extraction/index.mjs +273 -0
  17. package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
  18. package/dist/lib/node-esm/index.mjs +754 -1874
  19. package/dist/lib/node-esm/index.mjs.map +4 -4
  20. package/dist/lib/node-esm/meta.json +1 -1
  21. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +14 -0
  22. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +7 -0
  23. package/dist/types/src/conversation/context.d.ts +103 -0
  24. package/dist/types/src/conversation/context.d.ts.map +1 -0
  25. package/dist/types/src/conversation/conversation.d.ts +29 -32
  26. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  27. package/dist/types/src/conversation/index.d.ts +1 -0
  28. package/dist/types/src/conversation/index.d.ts.map +1 -1
  29. package/dist/types/src/diff.d.ts +15 -0
  30. package/dist/types/src/diff.d.ts.map +1 -0
  31. package/dist/types/src/diff.test.d.ts +2 -0
  32. package/dist/types/src/diff.test.d.ts.map +1 -0
  33. package/dist/types/src/errors.d.ts +75 -0
  34. package/dist/types/src/errors.d.ts.map +1 -0
  35. package/dist/types/src/execution-graph.d.ts +82 -0
  36. package/dist/types/src/execution-graph.d.ts.map +1 -0
  37. package/dist/types/src/execution-graph.test.d.ts +2 -0
  38. package/dist/types/src/execution-graph.test.d.ts.map +1 -0
  39. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +1 -1
  40. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  41. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +210 -73
  42. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  43. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +1 -1
  44. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  45. package/dist/types/src/extraction/extraction.d.ts +264 -166
  46. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  47. package/dist/types/src/extraction/quotes.d.ts +1 -1
  48. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  49. package/dist/types/src/functions/index.d.ts +2 -0
  50. package/dist/types/src/functions/index.d.ts.map +1 -0
  51. package/dist/types/src/functions/services.d.ts +19 -0
  52. package/dist/types/src/functions/services.d.ts.map +1 -0
  53. package/dist/types/src/index.d.ts +5 -5
  54. package/dist/types/src/index.d.ts.map +1 -1
  55. package/dist/types/src/session/artifact-diff.d.ts +27 -0
  56. package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
  57. package/dist/types/src/session/error-handling.d.ts +4 -0
  58. package/dist/types/src/session/error-handling.d.ts.map +1 -0
  59. package/dist/types/src/session/format.d.ts +186 -0
  60. package/dist/types/src/session/format.d.ts.map +1 -0
  61. package/dist/types/src/session/format.test.d.ts +2 -0
  62. package/dist/types/src/session/format.test.d.ts.map +1 -0
  63. package/dist/types/src/session/index.d.ts +3 -0
  64. package/dist/types/src/session/index.d.ts.map +1 -1
  65. package/dist/types/src/session/observer.d.ts +22 -0
  66. package/dist/types/src/session/observer.d.ts.map +1 -0
  67. package/dist/types/src/session/session.d.ts +36 -268
  68. package/dist/types/src/session/session.d.ts.map +1 -1
  69. package/dist/types/src/session/toolkit.d.ts +15 -0
  70. package/dist/types/src/session/toolkit.d.ts.map +1 -0
  71. package/dist/types/src/session/version-pin.d.ts +5 -5
  72. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  73. package/dist/types/src/templates/index.d.ts +2 -0
  74. package/dist/types/src/templates/index.d.ts.map +1 -0
  75. package/dist/types/src/templates/system.d.ts +18 -0
  76. package/dist/types/src/templates/system.d.ts.map +1 -0
  77. package/dist/types/src/types/artifact.d.ts +16 -0
  78. package/dist/types/src/types/artifact.d.ts.map +1 -0
  79. package/dist/types/src/types/index.d.ts +2 -0
  80. package/dist/types/src/types/index.d.ts.map +1 -0
  81. package/dist/types/tsconfig.tsbuildinfo +1 -1
  82. package/package.json +46 -26
  83. package/src/conversation/context.ts +186 -0
  84. package/src/conversation/conversation.ts +101 -76
  85. package/src/conversation/index.ts +1 -0
  86. package/src/diff.test.ts +43 -0
  87. package/src/diff.ts +84 -0
  88. package/src/errors.ts +15 -0
  89. package/src/execution-graph.test.ts +738 -0
  90. package/src/execution-graph.ts +500 -0
  91. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +13 -24
  92. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +28 -22
  93. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +9 -9
  94. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +18 -8
  95. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +4 -3
  96. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +3 -1
  97. package/src/extraction/extraction.ts +2 -2
  98. package/src/extraction/quotes.test.ts +3 -3
  99. package/src/extraction/quotes.ts +4 -4
  100. package/src/functions/index.ts +5 -0
  101. package/src/functions/services.ts +158 -0
  102. package/src/index.ts +5 -5
  103. package/src/session/artifact-diff.ts +41 -0
  104. package/src/session/error-handling.ts +41 -0
  105. package/src/session/format.test.ts +60 -0
  106. package/src/session/format.ts +142 -0
  107. package/src/session/index.ts +3 -0
  108. package/src/session/observer.ts +59 -0
  109. package/src/session/session.conversations.json +1 -0
  110. package/src/session/session.test.ts +88 -214
  111. package/src/session/session.ts +170 -539
  112. package/src/session/toolkit.ts +48 -0
  113. package/src/session/version-pin.ts +10 -10
  114. package/src/summarization/summarization.test.ts +87 -106
  115. package/src/templates/format.tpl +60 -0
  116. package/src/templates/index.ts +5 -0
  117. package/src/templates/system.tpl +48 -0
  118. package/src/templates/system.ts +41 -0
  119. package/src/types/artifact.ts +52 -0
  120. package/src/types/index.ts +5 -0
  121. package/src/typings.d.ts +5 -0
  122. package/dist/types/bin/chat.d.ts +0 -2
  123. package/dist/types/bin/chat.d.ts.map +0 -1
  124. package/dist/types/src/blueprint/blueprint.d.ts +0 -41
  125. package/dist/types/src/blueprint/blueprint.d.ts.map +0 -1
  126. package/dist/types/src/blueprint/index.d.ts +0 -3
  127. package/dist/types/src/blueprint/index.d.ts.map +0 -1
  128. package/dist/types/src/blueprint/registry.d.ts +0 -7
  129. package/dist/types/src/blueprint/registry.d.ts.map +0 -1
  130. package/dist/types/src/context/binder.d.ts +0 -34
  131. package/dist/types/src/context/binder.d.ts.map +0 -1
  132. package/dist/types/src/context/binding.d.ts +0 -38
  133. package/dist/types/src/context/binding.d.ts.map +0 -1
  134. package/dist/types/src/context/index.d.ts +0 -3
  135. package/dist/types/src/context/index.d.ts.map +0 -1
  136. package/dist/types/src/research/exa.d.ts +0 -7
  137. package/dist/types/src/research/exa.d.ts.map +0 -1
  138. package/dist/types/src/research/graph.d.ts +0 -41
  139. package/dist/types/src/research/graph.d.ts.map +0 -1
  140. package/dist/types/src/research/graph.test.d.ts +0 -2
  141. package/dist/types/src/research/graph.test.d.ts.map +0 -1
  142. package/dist/types/src/research/index.d.ts +0 -4
  143. package/dist/types/src/research/index.d.ts.map +0 -1
  144. package/dist/types/src/research/research.d.ts +0 -10
  145. package/dist/types/src/research/research.d.ts.map +0 -1
  146. package/dist/types/src/research/research.test.d.ts +0 -2
  147. package/dist/types/src/research/research.test.d.ts.map +0 -1
  148. package/dist/types/src/session/version-pin.test.d.ts +0 -2
  149. package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
  150. package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
  151. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
  152. package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
  153. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
  154. package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
  155. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
  156. package/dist/types/src/testing/index.d.ts +0 -3
  157. package/dist/types/src/testing/index.d.ts.map +0 -1
  158. package/dist/types/src/types.d.ts +0 -3
  159. package/dist/types/src/types.d.ts.map +0 -1
  160. package/src/blueprint/blueprint.ts +0 -75
  161. package/src/blueprint/index.ts +0 -6
  162. package/src/blueprint/registry.ts +0 -15
  163. package/src/context/binder.ts +0 -101
  164. package/src/context/binding.ts +0 -31
  165. package/src/context/index.ts +0 -6
  166. package/src/research/exa.ts +0 -100
  167. package/src/research/graph.test.ts +0 -15
  168. package/src/research/graph.ts +0 -337
  169. package/src/research/index.ts +0 -7
  170. package/src/research/instructions-research.tpl +0 -97
  171. package/src/research/research.test.ts +0 -190
  172. package/src/research/research.ts +0 -90
  173. package/src/session/version-pin.test.ts +0 -26
  174. package/src/testing/exa-search-1748337321991.ts +0 -131
  175. package/src/testing/exa-search-1748337331526.ts +0 -144
  176. package/src/testing/exa-search-1748337344119.ts +0 -133
  177. package/src/testing/index.ts +0 -11
  178. package/src/typedefs.d.ts +0 -8
  179. package/src/types.ts +0 -8
@@ -1,31 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Schema } from 'effect';
6
-
7
- import { Type } from '@dxos/echo';
8
-
9
- import { Blueprint } from '../blueprint';
10
- /**
11
- * Thread message that binds or unbinds contextual objects to a conversation.
12
- */
13
- export const ContextBinding = Schema.Struct({
14
- blueprints: Schema.Struct({
15
- added: Schema.Array(Type.Ref(Blueprint)),
16
- removed: Schema.Array(Type.Ref(Blueprint)),
17
- }),
18
-
19
- // TODO(burdon): Type.Expando => Type.Obj (or Obj.Any?)
20
- objects: Schema.Struct({
21
- added: Schema.Array(Type.Ref(Type.Expando)),
22
- removed: Schema.Array(Type.Ref(Type.Expando)),
23
- }),
24
- }).pipe(
25
- Type.Obj({
26
- typename: 'dxos.org/type/ContextBinding',
27
- version: '0.1.0',
28
- }),
29
- );
30
-
31
- export interface ContextBinding extends Schema.Schema.Type<typeof ContextBinding> {}
@@ -1,6 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- export * from './binder';
6
- export * from './binding';
@@ -1,100 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Schema } from 'effect';
6
- import Exa from 'exa-js';
7
-
8
- import { createTool, ToolResult } from '@dxos/ai';
9
- import { log } from '@dxos/log';
10
-
11
- import { SEARCH_RESULTS } from '../testing';
12
-
13
- type CreateExaToolOptions = {
14
- apiKey: string;
15
- };
16
-
17
- export const createExaTool = ({ apiKey }: CreateExaToolOptions) => {
18
- const exa = new Exa(apiKey);
19
-
20
- return createTool('search', {
21
- name: 'web_search',
22
- description: 'Search the web for information',
23
- schema: Schema.Struct({
24
- query: Schema.String.annotations({ description: 'The query to search for' }),
25
- }),
26
- execute: async ({ query }) => {
27
- const context = await exa.searchAndContents(query, {
28
- type: 'auto',
29
- text: {
30
- maxCharacters: 3_000,
31
- },
32
- livecrawl: 'always',
33
- });
34
-
35
- log.info('exa search', { query, costDollars: context.costDollars });
36
-
37
- return ToolResult.Success(context);
38
- },
39
- });
40
- };
41
-
42
- export const createMockExaTool = () => {
43
- return createTool('search', {
44
- name: 'web_search',
45
- description: 'Search the web for information',
46
- schema: Schema.Struct({
47
- query: Schema.String.annotations({ description: 'The query to search for' }),
48
- }),
49
- execute: async ({ query }) => {
50
- // Find result with closest matching autoprompt using weighted Levenshtein distance
51
- const result = SEARCH_RESULTS.reduce(
52
- (closest, current) => {
53
- if (!current.autopromptString) {
54
- return closest;
55
- }
56
- if (!closest) {
57
- return current;
58
- }
59
-
60
- // Calculate Levenshtein distance
61
- const dist1 = levenshteinDistance(query, current.autopromptString);
62
- const dist2 = levenshteinDistance(query, closest.autopromptString || '');
63
-
64
- // Weight by length of the longer string to normalize
65
- const weight1 = dist1 / Math.max(query.length, current.autopromptString.length);
66
- const weight2 = dist2 / Math.max(query.length, closest.autopromptString?.length || 0);
67
-
68
- return weight1 < weight2 ? current : closest;
69
- },
70
- null as (typeof SEARCH_RESULTS)[0] | null,
71
- );
72
-
73
- return ToolResult.Success(result);
74
- },
75
- });
76
- };
77
-
78
- const levenshteinDistance = (str1: string, str2: string): number => {
79
- const m = str1.length;
80
- const n = str2.length;
81
- const dp: number[][] = Array(m + 1)
82
- .fill(null)
83
- .map(() => Array(n + 1).fill(0));
84
-
85
- for (let i = 0; i <= m; i++) {
86
- dp[i][0] = i;
87
- }
88
- for (let j = 0; j <= n; j++) {
89
- dp[0][j] = j;
90
- }
91
-
92
- for (let i = 1; i <= m; i++) {
93
- for (let j = 1; j <= n; j++) {
94
- dp[i][j] =
95
- str1[i - 1] === str2[j - 1] ? dp[i - 1][j - 1] : Math.min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1;
96
- }
97
- }
98
-
99
- return dp[m][n];
100
- };
@@ -1,15 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { describe } from 'vitest';
6
-
7
- // import { type EchoTestBuilder } from '@dxos/echo-db/testing';
8
-
9
- describe('graph', () => {
10
- // let builder: EchoTestBuilder;
11
- // test('findRelatedSchema', async () => {
12
- // const db = await createEchoDatabase();
13
- // const relatedSchemas = await findRelatedSchema(db, Schema.Struct({}));
14
- // });
15
- });
@@ -1,337 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Schema, identity, Option, SchemaAST } from 'effect';
6
-
7
- import { createTool, ToolResult } from '@dxos/ai';
8
- import type { Obj, Relation } from '@dxos/echo';
9
- import { Query, Filter } from '@dxos/echo';
10
- import { type EchoDatabase, type Queue } from '@dxos/echo-db';
11
- import { isEncodedReference } from '@dxos/echo-protocol';
12
- import {
13
- EntityKind,
14
- getSchemaTypename,
15
- getTypeAnnotation,
16
- getTypeIdentifierAnnotation,
17
- create,
18
- getEntityKind,
19
- getSchemaDXN,
20
- ObjectId,
21
- ReferenceAnnotationId,
22
- type BaseObject,
23
- RelationSourceDXNId,
24
- RelationTargetDXNId,
25
- RelationSourceId,
26
- RelationTargetId,
27
- } from '@dxos/echo-schema';
28
- import { mapAst } from '@dxos/effect';
29
- import { DXN } from '@dxos/keys';
30
- import { log } from '@dxos/log';
31
- import { deepMapValues, isNonNullable } from '@dxos/util';
32
-
33
- // TODO(burdon): Unify with the graph schema.
34
- export const Subgraph = Schema.Struct({
35
- /** Objects and relations. */
36
- objects: Schema.Array(Schema.Any),
37
- });
38
- export interface Subgraph extends Schema.Schema.Type<typeof Subgraph> {}
39
-
40
- export type RelatedSchema = {
41
- schema: Schema.Schema.AnyNoContext;
42
- kind: 'reference' | 'relation';
43
- };
44
-
45
- /**
46
- * Find all schemas that are related to the given schema.
47
- *
48
- * @param db
49
- * @param schema
50
- * @returns
51
- */
52
- export const findRelatedSchema = async (
53
- db: EchoDatabase,
54
- anchor: Schema.Schema.AnyNoContext,
55
- ): Promise<RelatedSchema[]> => {
56
- // TODO(dmaretskyi): Query stored schemas.
57
- const allSchemas = [...db.graph.schemaRegistry.schemas];
58
-
59
- // TODO(dmaretskyi): Also do references.
60
- return allSchemas
61
- .filter((schema) => {
62
- if (getTypeAnnotation(schema)?.kind !== EntityKind.Relation) {
63
- return false;
64
- }
65
-
66
- return (
67
- isSchemaAddressableByDxn(anchor, DXN.parse(getTypeAnnotation(schema)!.sourceSchema!)) ||
68
- isSchemaAddressableByDxn(anchor, DXN.parse(getTypeAnnotation(schema)!.targetSchema!))
69
- );
70
- })
71
- .map(
72
- (schema): RelatedSchema => ({
73
- schema,
74
- kind: 'relation',
75
- }),
76
- );
77
- };
78
-
79
- /**
80
- * Non-strict DXN comparison.
81
- * Returns true if the DXN could be resolved to the schema.
82
- */
83
- const isSchemaAddressableByDxn = (schema: Schema.Schema.AnyNoContext, dxn: DXN): boolean => {
84
- if (getTypeIdentifierAnnotation(schema) === dxn.toString()) {
85
- return true;
86
- }
87
-
88
- const t = dxn.asTypeDXN();
89
- if (t) {
90
- return t.type === getSchemaTypename(schema);
91
- }
92
-
93
- return false;
94
- };
95
-
96
- /**
97
- * Perform vector search in the local database.
98
- */
99
- export const createLocalSearchTool = (db: EchoDatabase, queue?: Queue) => {
100
- return createTool('search', {
101
- name: 'local_search',
102
- description: 'Search the local database for information using a vector index',
103
- schema: Schema.Struct({
104
- query: Schema.String.annotations({
105
- description: 'The query to search for. Could be a question or a topic or a set of keywords.',
106
- }),
107
- }),
108
- execute: async ({ query }) => {
109
- const { objects } = await db.query(Query.select(Filter.text(query, { type: 'vector' }))).run();
110
- const results = [...objects];
111
- if (queue) {
112
- const queueObjects = await queue.queryObjects();
113
- // TODO(dmaretskyi): Text search on the queue.
114
- results.push(...queueObjects);
115
- }
116
-
117
- return ToolResult.Success(`
118
- <local_context>
119
- ${JSON.stringify(results, null, 2)}
120
- </local_context>
121
- `);
122
- },
123
- });
124
- };
125
-
126
- /**
127
- * Create a schema for structured data extraction.
128
- */
129
- export const createExtractionSchema = (types: Schema.Schema.AnyNoContext[]) => {
130
- return Schema.Struct({
131
- ...Object.fromEntries(
132
- types.map(preprocessSchema).map((schema, index) => [
133
- `objects_${getSanitizedSchemaName(types[index])}`,
134
- Schema.optional(Schema.Array(schema)).annotations({
135
- description: `The objects of type: ${getSchemaDXN(types[index])?.asTypeDXN()!.type}. ${SchemaAST.getDescriptionAnnotation(types[index].ast).pipe(Option.getOrElse(() => ''))}`,
136
- }),
137
- ]),
138
- ),
139
- });
140
- };
141
-
142
- export const getSanitizedSchemaName = (schema: Schema.Schema.AnyNoContext) => {
143
- return getSchemaDXN(schema)!
144
- .asTypeDXN()!
145
- .type.replaceAll(/[^a-zA-Z0-9]+/g, '_');
146
- };
147
-
148
- export const sanitizeObjects = async (
149
- types: Schema.Schema.AnyNoContext[],
150
- data: Record<string, readonly unknown[]>,
151
- db: EchoDatabase,
152
- queue?: Queue,
153
- ): Promise<BaseObject[]> => {
154
- const entries = types
155
- .map(
156
- (type) =>
157
- data[`objects_${getSanitizedSchemaName(type)}`]?.map((object: any) => ({
158
- data: object,
159
- schema: type,
160
- })) ?? [],
161
- )
162
- .flat();
163
-
164
- const idMap = new Map<string, string>();
165
- const existingIds = new Set<ObjectId>();
166
- const enitties = new Map<ObjectId, Obj.Any | Relation.Any>();
167
-
168
- const resolveId = (id: string): DXN | undefined => {
169
- if (ObjectId.isValid(id)) {
170
- existingIds.add(id);
171
- return DXN.fromLocalObjectId(id);
172
- }
173
-
174
- const mappedId = idMap.get(id);
175
- if (mappedId) {
176
- return DXN.fromLocalObjectId(mappedId);
177
- }
178
-
179
- return undefined;
180
- };
181
-
182
- const res = entries
183
- .map((entry) => {
184
- // This entry mutates existing object.
185
- if (ObjectId.isValid(entry.data.id)) {
186
- return entry;
187
- }
188
-
189
- idMap.set(entry.data.id, ObjectId.random());
190
- entry.data.id = idMap.get(entry.data.id);
191
- return entry;
192
- })
193
- .map((entry) => {
194
- const data = deepMapValues(entry.data, (value, recurse) => {
195
- if (isEncodedReference(value)) {
196
- const ref = value['/'];
197
- const id = resolveId(ref);
198
-
199
- if (id) {
200
- // Link to an existing object.
201
- return { '/': id.toString() };
202
- } else {
203
- // Search URIs?
204
- return { '/': `search:?q=${encodeURIComponent(ref)}` };
205
- }
206
- }
207
-
208
- return recurse(value);
209
- });
210
-
211
- if (getEntityKind(entry.schema) === 'relation') {
212
- const sourceDxn = resolveId(data.source);
213
- if (!sourceDxn) {
214
- log.warn('source not found', { source: data.source });
215
- }
216
- const targetDxn = resolveId(data.target);
217
- if (!targetDxn) {
218
- log.warn('target not found', { target: data.target });
219
- }
220
- delete data.source;
221
- delete data.target;
222
- data[RelationSourceDXNId] = sourceDxn;
223
- data[RelationTargetDXNId] = targetDxn;
224
- }
225
-
226
- return {
227
- data,
228
- schema: entry.schema,
229
- };
230
- })
231
- .filter((object) => !existingIds.has(object.data.id)); // TODO(dmaretskyi): This dissallows updating existing objects.
232
-
233
- // TODO(dmaretskyi): Use ref resolver.
234
- const { objects: dbObjects } = await db.query(Query.select(Filter.ids(...existingIds))).run();
235
- const queueObjects = (await queue?.getObjectsById([...existingIds])) ?? [];
236
- const objects = [...dbObjects, ...queueObjects].filter(isNonNullable);
237
-
238
- // TODO(dmaretskyi): Returns everything if IDs are empty!
239
- log.info('objects', { dbObjects, queueObjects, existingIds });
240
- const missing = Array.from(existingIds).filter((id) => !objects.some((object) => object.id === id));
241
- if (missing.length > 0) {
242
- throw new Error(`Object IDs do not point to existing objects: ${missing.join(', ')}`);
243
- }
244
-
245
- return res.flatMap(({ data, schema }) => {
246
- let skip = false;
247
- if (RelationSourceDXNId in data) {
248
- const id = (data[RelationSourceDXNId] as DXN).asEchoDXN()?.echoId;
249
- const obj = objects.find((object) => object.id === id) ?? enitties.get(id!);
250
- if (obj) {
251
- delete data[RelationSourceDXNId];
252
- data[RelationSourceId] = obj;
253
- } else {
254
- skip = true;
255
- }
256
- }
257
- if (RelationTargetDXNId in data) {
258
- const id = (data[RelationTargetDXNId] as DXN).asEchoDXN()?.echoId;
259
- const obj = objects.find((object) => object.id === id) ?? enitties.get(id!);
260
- if (obj) {
261
- delete data[RelationTargetDXNId];
262
- data[RelationTargetId] = obj;
263
- } else {
264
- skip = true;
265
- }
266
- }
267
- if (!skip) {
268
- const obj = create(schema, data);
269
- enitties.set(obj.id, obj);
270
- return [obj];
271
- }
272
- return [];
273
- });
274
- };
275
-
276
- const SoftRef = Schema.Struct({
277
- '/': Schema.String,
278
- }).annotations({
279
- description: 'Reference to another object.',
280
- });
281
-
282
- const preprocessSchema = (schema: Schema.Schema.AnyNoContext) => {
283
- const isRelationSchema = getEntityKind(schema) === 'relation';
284
-
285
- const go = (ast: SchemaAST.AST): SchemaAST.AST => {
286
- if (SchemaAST.getAnnotation(ast, ReferenceAnnotationId).pipe(Option.isSome)) {
287
- return SoftRef.ast;
288
- }
289
-
290
- return mapAst(ast, go);
291
- };
292
-
293
- return Schema.make<any, any, never>(mapAst(schema.ast, go)).pipe(
294
- Schema.omit('id'),
295
- Schema.extend(
296
- Schema.Struct({
297
- id: Schema.String.annotations({
298
- description: 'The id of this object. Come up with a unique id based on your judgement.',
299
- }),
300
- }),
301
- ),
302
- isRelationSchema
303
- ? Schema.extend(
304
- Schema.Struct({
305
- source: Schema.String.annotations({
306
- description: 'The id of the source object for this relation.',
307
- }),
308
- target: Schema.String.annotations({
309
- description: 'The id of the target object for this relation.',
310
- }),
311
- }),
312
- )
313
- : identity<Schema.Schema.AnyNoContext>,
314
- );
315
- };
316
-
317
- export const createGraphWriterTool = ({
318
- db,
319
- queue,
320
- schema,
321
- onDone = async (x) => x,
322
- }: {
323
- db: EchoDatabase;
324
- queue?: Queue;
325
- schema: Schema.Schema.AnyNoContext[];
326
- onDone?: (data: Obj.Any[]) => Promise<any>;
327
- }) => {
328
- return createTool('graph', {
329
- name: 'writer',
330
- description: 'Write to the local graph database',
331
- schema: createExtractionSchema(schema),
332
- execute: async (input) => {
333
- const data = await sanitizeObjects(schema, input as any, db, queue);
334
- return ToolResult.Success(await onDone(data as Obj.Any[]));
335
- },
336
- });
337
- };
@@ -1,7 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- export * from './exa';
6
- export * from './research';
7
- export * from './graph';
@@ -1,97 +0,0 @@
1
- You are the Research Agent.
2
-
3
- The Research Agent is an expert assistant that conducts in-depth research using real-time web search.
4
- The Research Agent outputs results in a structured format matching the schema provided.
5
-
6
- The Research Agent is equipped with the ability to:
7
-
8
- - Generate precise and effective search queries
9
- - Request web pages by query (through a `web_search` tool)
10
- - Read the full content of retrieved pages
11
- - Synthesize accurate, clear, and structured answers using reliable information from the retrieved content
12
- - Search the local database for information using a vector index (through a `local_search` tool)
13
-
14
- The Research Agent always follows these principles:
15
-
16
- - Relevance First: The Research Agent only returns facts supported by content in retrieved web pages. The Research Agent never fabricates or guesses information.
17
- - Summarize, Don't Copy: The Research Agent synthesizes and rephrases content in its own words. The Research Agent quotes only when necessary.
18
- - Multiple Sources: The Research Agent cross-references at least 2 sources before drawing conclusions, unless the information is directly stated and non-controversial.
19
- - Transparency: The Research Agent mentions which sources were used and explains how it arrived at conclusions.
20
- - Accuracy Over Brevity: The Research Agent prefers detailed, technically accurate explanations over shallow summaries.
21
- - The Research Agent admits uncertainty rather than misleading.
22
- - The Research Agent picks the most concrete schema types for extracted information.
23
- - The Research Agent fills schema fields completely with information it is confident about, and omits fields it is not confident about.
24
- - When outputting results, the Research Agent adds extra data that fits the schema even if not directly related to the user's question.
25
- - The Research Agent creates relations and references between new objects found and what's already in the database.
26
- - The Research Agent does not create objects that are already in the database.
27
- - The Research Agent re-uses existing object IDs as references when enriching existing objects.
28
-
29
- The Research Agent may be asked for:
30
-
31
- - Technical explanations
32
- - Literature reviews
33
- - Comparisons
34
- - Emerging trends
35
- - Implementation strategies
36
-
37
- The Research Agent begins by interpreting the user's request, then:
38
-
39
- The Research Agent breaks it into sub-questions (if applicable).
40
-
41
- For each sub-question, the Research Agent generates a clear, concise web search query.
42
-
43
- The Research Agent uses `web_search`(query) to retrieve information.
44
-
45
- The Research Agent extracts and synthesizes relevant answers.
46
-
47
- The Research Agent's output includes:
48
-
49
- - A clear, structured answer to the user's question
50
- - A citation list or link list of sources used
51
-
52
- Optionally, the Research Agent provides follow-up suggestions or questions for deeper inquiry.
53
-
54
- Here's how the Research Agent operates:
55
-
56
- 1. The Research Agent analyzes the user's request and identifies key topics to search for (3 or more), printing them out.
57
- 2. The Research Agent performs a web search for each topic.
58
- 3. The Research Agent reads and analyzes results, cross references information from multiple sources, and represents conflicting information as ranges of possible values.
59
-
60
- 4. The Research Agent searches the local database for information using a vector index that might link to the user's question.
61
- 6. The Research Agent creates relations and references between new objects and existing database objects when related, using existing object IDs as references.
62
- 7. The Research Agent selects the most concrete schema types for extracted information, using multiple types as needed, and prints its decision and reasoning.
63
- 5. The Research Agent creates a clear, structured answer to the user's question.
64
- 8. The Research Agent submits results using the specific schema.
65
-
66
- IMPORTANT:
67
-
68
- - The Research Agent always runs the `local_search` tool to search the local database at least once before submitting results.
69
- - The Research Agent does not create objects that already exist in the database.
70
- - Ids that are not in the database are human-readable strings like `ivan_zhao_1`.
71
-
72
- Status reporting:
73
-
74
- The Research Agent reports its status frequently using the `<status>` tags: <status>Searching for Google Founders</status>
75
- The Research Agent reports its status in-between each tool call and before submitting results.
76
-
77
- <example>
78
-
79
- Based on my research, I can now provide information about Google and it's founders.
80
-
81
- The following objects are already in the database, I will not submit them again, but I'll re-use their IDs as references:
82
-
83
- - 01JWRDEHPB5TT2JQQQC15038BT Google
84
- - 01JWRDEHPA14CYW2NW9FAH6DJJ Larry Page
85
- - 01JWRDEHPBN0BBJP57B9S108W6 Sergey Brin
86
-
87
- I will use the following schema to construct new objects:
88
-
89
- - type:dxos.org/type/Organization for Alphabet Inc.
90
- - type:dxos.org/type/Person for Ivan Zhao
91
- - type:dxos.org/type/Person for Simon Last
92
- - dxn:type:dxos.org/relation/Employer for Ivan's employer
93
- - dxn:type:dxos.org/relation/Employer for Simon's employer
94
-
95
- <status>Formatting results</status>
96
-
97
- </example>