@dxos/assistant-toolkit 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 (200) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +3 -0
  3. package/dist/lib/browser/index.mjs +2481 -0
  4. package/dist/lib/browser/index.mjs.map +7 -0
  5. package/dist/lib/browser/meta.json +1 -0
  6. package/dist/lib/node-esm/index.mjs +2483 -0
  7. package/dist/lib/node-esm/index.mjs.map +7 -0
  8. package/dist/lib/node-esm/meta.json +1 -0
  9. package/dist/types/src/blueprints/design/design-blueprint.d.ts +4 -0
  10. package/dist/types/src/blueprints/design/design-blueprint.d.ts.map +1 -0
  11. package/dist/types/src/blueprints/design/design-blueprint.test.d.ts +2 -0
  12. package/dist/types/src/blueprints/design/design-blueprint.test.d.ts.map +1 -0
  13. package/dist/types/src/blueprints/design/index.d.ts +3 -0
  14. package/dist/types/src/blueprints/design/index.d.ts.map +1 -0
  15. package/dist/types/src/blueprints/discord/discord-blueprint.d.ts +18 -0
  16. package/dist/types/src/blueprints/discord/discord-blueprint.d.ts.map +1 -0
  17. package/dist/types/src/blueprints/discord/index.d.ts +3 -0
  18. package/dist/types/src/blueprints/discord/index.d.ts.map +1 -0
  19. package/dist/types/src/blueprints/index.d.ts +7 -0
  20. package/dist/types/src/blueprints/index.d.ts.map +1 -0
  21. package/dist/types/src/blueprints/linear/index.d.ts +3 -0
  22. package/dist/types/src/blueprints/linear/index.d.ts.map +1 -0
  23. package/dist/types/src/blueprints/linear/linear-blueprint.d.ts +18 -0
  24. package/dist/types/src/blueprints/linear/linear-blueprint.d.ts.map +1 -0
  25. package/dist/types/src/blueprints/planning/index.d.ts +3 -0
  26. package/dist/types/src/blueprints/planning/index.d.ts.map +1 -0
  27. package/dist/types/src/blueprints/planning/planning-blueprint.d.ts +4 -0
  28. package/dist/types/src/blueprints/planning/planning-blueprint.d.ts.map +1 -0
  29. package/dist/types/src/blueprints/planning/planning-blueprint.test.d.ts +2 -0
  30. package/dist/types/src/blueprints/planning/planning-blueprint.test.d.ts.map +1 -0
  31. package/dist/types/src/blueprints/research/index.d.ts +3 -0
  32. package/dist/types/src/blueprints/research/index.d.ts.map +1 -0
  33. package/dist/types/src/blueprints/research/research-blueprint.d.ts +4 -0
  34. package/dist/types/src/blueprints/research/research-blueprint.d.ts.map +1 -0
  35. package/dist/types/src/blueprints/research/research-blueprint.test.d.ts +2 -0
  36. package/dist/types/src/blueprints/research/research-blueprint.test.d.ts.map +1 -0
  37. package/dist/types/src/blueprints/testing.d.ts +12 -0
  38. package/dist/types/src/blueprints/testing.d.ts.map +1 -0
  39. package/dist/types/src/blueprints/websearch/index.d.ts +4 -0
  40. package/dist/types/src/blueprints/websearch/index.d.ts.map +1 -0
  41. package/dist/types/src/blueprints/websearch/websearch-blueprint.d.ts +4 -0
  42. package/dist/types/src/blueprints/websearch/websearch-blueprint.d.ts.map +1 -0
  43. package/dist/types/src/blueprints/websearch/websearch-toolkit.d.ts +26 -0
  44. package/dist/types/src/blueprints/websearch/websearch-toolkit.d.ts.map +1 -0
  45. package/dist/types/src/experimental/feed.test.d.ts +2 -0
  46. package/dist/types/src/experimental/feed.test.d.ts.map +1 -0
  47. package/dist/types/src/functions/agent/index.d.ts +5 -0
  48. package/dist/types/src/functions/agent/index.d.ts.map +1 -0
  49. package/dist/types/src/functions/agent/prompt.d.ts +11 -0
  50. package/dist/types/src/functions/agent/prompt.d.ts.map +1 -0
  51. package/dist/types/src/functions/discord/fetch-messages.d.ts +11 -0
  52. package/dist/types/src/functions/discord/fetch-messages.d.ts.map +1 -0
  53. package/dist/types/src/functions/discord/fetch-messages.test.d.ts +2 -0
  54. package/dist/types/src/functions/discord/fetch-messages.test.d.ts.map +1 -0
  55. package/dist/types/src/functions/discord/index.d.ts +12 -0
  56. package/dist/types/src/functions/discord/index.d.ts.map +1 -0
  57. package/dist/types/src/functions/document/index.d.ts +12 -0
  58. package/dist/types/src/functions/document/index.d.ts.map +1 -0
  59. package/dist/types/src/functions/document/read.d.ts +7 -0
  60. package/dist/types/src/functions/document/read.d.ts.map +1 -0
  61. package/dist/types/src/functions/document/update.d.ts +6 -0
  62. package/dist/types/src/functions/document/update.d.ts.map +1 -0
  63. package/dist/types/src/functions/entity-extraction/entity-extraction.d.ts +173 -0
  64. package/dist/types/src/functions/entity-extraction/entity-extraction.d.ts.map +1 -0
  65. package/dist/types/src/functions/entity-extraction/entity-extraction.test.d.ts +2 -0
  66. package/dist/types/src/functions/entity-extraction/entity-extraction.test.d.ts.map +1 -0
  67. package/dist/types/src/functions/entity-extraction/index.d.ts +174 -0
  68. package/dist/types/src/functions/entity-extraction/index.d.ts.map +1 -0
  69. package/dist/types/src/functions/exa/exa.d.ts +5 -0
  70. package/dist/types/src/functions/exa/exa.d.ts.map +1 -0
  71. package/dist/types/src/functions/exa/index.d.ts +3 -0
  72. package/dist/types/src/functions/exa/index.d.ts.map +1 -0
  73. package/dist/types/src/functions/exa/mock.d.ts +5 -0
  74. package/dist/types/src/functions/exa/mock.d.ts.map +1 -0
  75. package/dist/types/src/functions/github/fetch-prs.d.ts +6 -0
  76. package/dist/types/src/functions/github/fetch-prs.d.ts.map +1 -0
  77. package/dist/types/src/functions/index.d.ts +8 -0
  78. package/dist/types/src/functions/index.d.ts.map +1 -0
  79. package/dist/types/src/functions/linear/index.d.ts +9 -0
  80. package/dist/types/src/functions/linear/index.d.ts.map +1 -0
  81. package/dist/types/src/functions/linear/linear.test.d.ts +2 -0
  82. package/dist/types/src/functions/linear/linear.test.d.ts.map +1 -0
  83. package/dist/types/src/functions/linear/sync-issues.d.ts +12 -0
  84. package/dist/types/src/functions/linear/sync-issues.d.ts.map +1 -0
  85. package/dist/types/src/functions/research/create-document.d.ts +7 -0
  86. package/dist/types/src/functions/research/create-document.d.ts.map +1 -0
  87. package/dist/types/src/functions/research/graph.d.ts +64 -0
  88. package/dist/types/src/functions/research/graph.d.ts.map +1 -0
  89. package/dist/types/src/functions/research/graph.test.d.ts +2 -0
  90. package/dist/types/src/functions/research/graph.test.d.ts.map +1 -0
  91. package/dist/types/src/functions/research/index.d.ts +19 -0
  92. package/dist/types/src/functions/research/index.d.ts.map +1 -0
  93. package/dist/types/src/functions/research/research-graph.d.ts +18 -0
  94. package/dist/types/src/functions/research/research-graph.d.ts.map +1 -0
  95. package/dist/types/src/functions/research/research.d.ts +13 -0
  96. package/dist/types/src/functions/research/research.d.ts.map +1 -0
  97. package/dist/types/src/functions/research/research.test.d.ts +2 -0
  98. package/dist/types/src/functions/research/research.test.d.ts.map +1 -0
  99. package/dist/types/src/functions/research/types.d.ts +384 -0
  100. package/dist/types/src/functions/research/types.d.ts.map +1 -0
  101. package/dist/types/src/functions/tasks/index.d.ts +15 -0
  102. package/dist/types/src/functions/tasks/index.d.ts.map +1 -0
  103. package/dist/types/src/functions/tasks/read.d.ts +7 -0
  104. package/dist/types/src/functions/tasks/read.d.ts.map +1 -0
  105. package/dist/types/src/functions/tasks/task-list.d.ts +74 -0
  106. package/dist/types/src/functions/tasks/task-list.d.ts.map +1 -0
  107. package/dist/types/src/functions/tasks/task-list.test.d.ts +2 -0
  108. package/dist/types/src/functions/tasks/task-list.test.d.ts.map +1 -0
  109. package/dist/types/src/functions/tasks/update.d.ts +9 -0
  110. package/dist/types/src/functions/tasks/update.d.ts.map +1 -0
  111. package/dist/types/src/index.d.ts +5 -0
  112. package/dist/types/src/index.d.ts.map +1 -0
  113. package/dist/types/src/plugins.d.ts +19 -0
  114. package/dist/types/src/plugins.d.ts.map +1 -0
  115. package/dist/types/src/sync/index.d.ts +2 -0
  116. package/dist/types/src/sync/index.d.ts.map +1 -0
  117. package/dist/types/src/sync/sync.d.ts +15 -0
  118. package/dist/types/src/sync/sync.d.ts.map +1 -0
  119. package/dist/types/src/testing/data/exa-search-1748337321991.d.ts +38 -0
  120. package/dist/types/src/testing/data/exa-search-1748337321991.d.ts.map +1 -0
  121. package/dist/types/src/testing/data/exa-search-1748337331526.d.ts +37 -0
  122. package/dist/types/src/testing/data/exa-search-1748337331526.d.ts.map +1 -0
  123. package/dist/types/src/testing/data/exa-search-1748337344119.d.ts +58 -0
  124. package/dist/types/src/testing/data/exa-search-1748337344119.d.ts.map +1 -0
  125. package/dist/types/src/testing/data/index.d.ts +3 -0
  126. package/dist/types/src/testing/data/index.d.ts.map +1 -0
  127. package/dist/types/src/testing/index.d.ts +2 -0
  128. package/dist/types/src/testing/index.d.ts.map +1 -0
  129. package/dist/types/src/util/graphql.d.ts +22 -0
  130. package/dist/types/src/util/graphql.d.ts.map +1 -0
  131. package/dist/types/src/util/index.d.ts +2 -0
  132. package/dist/types/src/util/index.d.ts.map +1 -0
  133. package/dist/types/tsconfig.tsbuildinfo +1 -0
  134. package/package.json +67 -0
  135. package/src/blueprints/design/design-blueprint.test.ts +108 -0
  136. package/src/blueprints/design/design-blueprint.ts +33 -0
  137. package/src/blueprints/design/index.ts +7 -0
  138. package/src/blueprints/discord/discord-blueprint.ts +34 -0
  139. package/src/blueprints/discord/index.ts +7 -0
  140. package/src/blueprints/index.ts +10 -0
  141. package/src/blueprints/linear/index.ts +7 -0
  142. package/src/blueprints/linear/linear-blueprint.ts +35 -0
  143. package/src/blueprints/planning/index.ts +7 -0
  144. package/src/blueprints/planning/planning-blueprint.test.ts +129 -0
  145. package/src/blueprints/planning/planning-blueprint.ts +98 -0
  146. package/src/blueprints/research/index.ts +7 -0
  147. package/src/blueprints/research/research-blueprint.test.ts +7 -0
  148. package/src/blueprints/research/research-blueprint.ts +45 -0
  149. package/src/blueprints/testing.ts +34 -0
  150. package/src/blueprints/websearch/index.ts +8 -0
  151. package/src/blueprints/websearch/websearch-blueprint.ts +20 -0
  152. package/src/blueprints/websearch/websearch-toolkit.ts +8 -0
  153. package/src/experimental/feed.test.ts +108 -0
  154. package/src/functions/agent/index.ts +11 -0
  155. package/src/functions/agent/prompt.ts +101 -0
  156. package/src/functions/discord/fetch-messages.test.ts +59 -0
  157. package/src/functions/discord/fetch-messages.ts +251 -0
  158. package/src/functions/discord/index.ts +9 -0
  159. package/src/functions/document/index.ts +11 -0
  160. package/src/functions/document/read.ts +29 -0
  161. package/src/functions/document/update.ts +30 -0
  162. package/src/functions/entity-extraction/entity-extraction.conversations.json +1 -0
  163. package/src/functions/entity-extraction/entity-extraction.test.ts +100 -0
  164. package/src/functions/entity-extraction/entity-extraction.ts +163 -0
  165. package/src/functions/entity-extraction/index.ts +9 -0
  166. package/src/functions/exa/exa.ts +37 -0
  167. package/src/functions/exa/index.ts +6 -0
  168. package/src/functions/exa/mock.ts +71 -0
  169. package/src/functions/github/fetch-prs.ts +30 -0
  170. package/src/functions/index.ts +11 -0
  171. package/src/functions/linear/index.ts +9 -0
  172. package/src/functions/linear/linear.test.ts +86 -0
  173. package/src/functions/linear/sync-issues.ts +189 -0
  174. package/src/functions/research/create-document.ts +69 -0
  175. package/src/functions/research/graph.test.ts +69 -0
  176. package/src/functions/research/graph.ts +388 -0
  177. package/src/functions/research/index.ts +15 -0
  178. package/src/functions/research/instructions-research.tpl +98 -0
  179. package/src/functions/research/research-graph.ts +47 -0
  180. package/src/functions/research/research.conversations.json +10714 -0
  181. package/src/functions/research/research.test.ts +240 -0
  182. package/src/functions/research/research.ts +155 -0
  183. package/src/functions/research/types.ts +24 -0
  184. package/src/functions/tasks/index.ts +11 -0
  185. package/src/functions/tasks/read.ts +34 -0
  186. package/src/functions/tasks/task-list.test.ts +99 -0
  187. package/src/functions/tasks/task-list.ts +165 -0
  188. package/src/functions/tasks/update.ts +52 -0
  189. package/src/index.ts +8 -0
  190. package/src/plugins.tsx +68 -0
  191. package/src/sync/index.ts +5 -0
  192. package/src/sync/sync.ts +87 -0
  193. package/src/testing/data/exa-search-1748337321991.ts +131 -0
  194. package/src/testing/data/exa-search-1748337331526.ts +144 -0
  195. package/src/testing/data/exa-search-1748337344119.ts +133 -0
  196. package/src/testing/data/index.ts +11 -0
  197. package/src/testing/index.ts +5 -0
  198. package/src/typedefs.d.ts +8 -0
  199. package/src/util/graphql.ts +31 -0
  200. package/src/util/index.ts +5 -0
@@ -0,0 +1,69 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as AnthropicClient from '@effect/ai-anthropic/AnthropicClient';
6
+ import * as FetchHttpClient from '@effect/platform/FetchHttpClient';
7
+ import { describe, it } from '@effect/vitest';
8
+ import * as Config from 'effect/Config';
9
+ import * as Effect from 'effect/Effect';
10
+ import * as Function from 'effect/Function';
11
+ import * as Layer from 'effect/Layer';
12
+
13
+ import { AiService, ToolExecutionService, ToolResolverService } from '@dxos/ai';
14
+ import * as AiServiceRouter from '@dxos/ai/AiServiceRouter';
15
+ import { tapHttpErrors } from '@dxos/ai/testing';
16
+ import { AiSession } from '@dxos/assistant';
17
+ import { TestHelpers } from '@dxos/effect';
18
+ import { DatabaseService, TracingService } from '@dxos/functions';
19
+ import { log } from '@dxos/log';
20
+ import { DataType } from '@dxos/schema';
21
+
22
+ import { makeGraphWriterHandler, makeGraphWriterToolkit } from './graph';
23
+
24
+ // import { type EchoTestBuilder } from '@dxos/echo-db/testing';
25
+
26
+ const TestLayer = Function.pipe(
27
+ AiService.model('@anthropic/claude-3-5-sonnet-20241022'),
28
+ Layer.provideMerge(DatabaseService.notAvailable),
29
+ Layer.provideMerge(ToolResolverService.layerEmpty),
30
+ Layer.provideMerge(ToolExecutionService.layerEmpty),
31
+ Layer.provide(AiServiceRouter.AiServiceRouter),
32
+ Layer.provide(
33
+ AnthropicClient.layerConfig({
34
+ apiKey: Config.redacted('ANTHROPIC_API_KEY'),
35
+ transformClient: tapHttpErrors,
36
+ }),
37
+ ),
38
+ Layer.provide(FetchHttpClient.layer),
39
+ Layer.provideMerge(TracingService.layerNoop),
40
+ );
41
+
42
+ describe('graph', () => {
43
+ // let builder: EchoTestBuilder;
44
+ // test('findRelatedSchema', async () => {
45
+ // const db = await createEchoDatabase();
46
+ // const relatedSchemas = await findRelatedSchema(db, Schema.Struct({}));
47
+ // });
48
+
49
+ const Toolkit = makeGraphWriterToolkit({ schema: [DataType.Project] });
50
+ const ToolkitLayer = makeGraphWriterHandler(Toolkit);
51
+
52
+ it.effect.skip(
53
+ 'calculator',
54
+ Effect.fn(
55
+ function* (_) {
56
+ const session = new AiSession();
57
+ const toolkit = yield* Toolkit;
58
+ const response = yield* session.run({
59
+ toolkit,
60
+ prompt: 'What is 10 + 20?',
61
+ });
62
+
63
+ log.info('response', { response });
64
+ },
65
+ Effect.provide(Layer.mergeAll(TestLayer, ToolkitLayer)),
66
+ TestHelpers.runIf(process.env.ANTHROPIC_API_KEY),
67
+ ),
68
+ );
69
+ });
@@ -0,0 +1,388 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Tool from '@effect/ai/Tool';
6
+ import * as Toolkit from '@effect/ai/Toolkit';
7
+ import * as Context from 'effect/Context';
8
+ import * as Effect from 'effect/Effect';
9
+ import * as Function from 'effect/Function';
10
+ import * as Option from 'effect/Option';
11
+ import * as Schema from 'effect/Schema';
12
+ import * as SchemaAST from 'effect/SchemaAST';
13
+
14
+ import { Obj, type Relation } from '@dxos/echo';
15
+ import { Filter, Query } from '@dxos/echo';
16
+ import {
17
+ EntityKind,
18
+ ObjectId,
19
+ ReferenceAnnotationId,
20
+ RelationSourceDXNId,
21
+ RelationSourceId,
22
+ RelationTargetDXNId,
23
+ RelationTargetId,
24
+ create,
25
+ getEntityKind,
26
+ getSchemaDXN,
27
+ getSchemaTypename,
28
+ getTypeAnnotation,
29
+ getTypeIdentifierAnnotation,
30
+ } from '@dxos/echo/internal';
31
+ import { type EchoDatabase, type Queue } from '@dxos/echo-db';
32
+ import { isEncodedReference } from '@dxos/echo-protocol';
33
+ import { mapAst } from '@dxos/effect';
34
+ import { ContextQueueService, DatabaseService } from '@dxos/functions';
35
+ import { DXN } from '@dxos/keys';
36
+ import { log } from '@dxos/log';
37
+ import { deepMapValues, isNonNullable, trim } from '@dxos/util';
38
+
39
+ // TODO(burdon): Unify with the graph schema.
40
+ export const Subgraph = Schema.Struct({
41
+ /** Objects and relations. */
42
+ objects: Schema.Array(Schema.Any),
43
+ });
44
+
45
+ export interface Subgraph extends Schema.Schema.Type<typeof Subgraph> {}
46
+
47
+ export type RelatedSchema = {
48
+ schema: Schema.Schema.AnyNoContext;
49
+ kind: 'reference' | 'relation';
50
+ };
51
+
52
+ /**
53
+ * Find all schemas that are related to the given schema.
54
+ *
55
+ * @param db
56
+ * @param schema
57
+ * @returns
58
+ */
59
+ export const findRelatedSchema = async (
60
+ db: EchoDatabase,
61
+ anchor: Schema.Schema.AnyNoContext,
62
+ ): Promise<RelatedSchema[]> => {
63
+ // TODO(dmaretskyi): Query stored schemas.
64
+ const allSchemas = [...db.graph.schemaRegistry.schemas];
65
+
66
+ // TODO(dmaretskyi): Also do references.
67
+ return allSchemas
68
+ .filter((schema) => {
69
+ if (getTypeAnnotation(schema)?.kind !== EntityKind.Relation) {
70
+ return false;
71
+ }
72
+
73
+ return (
74
+ isSchemaAddressableByDxn(anchor, DXN.parse(getTypeAnnotation(schema)!.sourceSchema!)) ||
75
+ isSchemaAddressableByDxn(anchor, DXN.parse(getTypeAnnotation(schema)!.targetSchema!))
76
+ );
77
+ })
78
+ .map(
79
+ (schema): RelatedSchema => ({
80
+ schema,
81
+ kind: 'relation',
82
+ }),
83
+ );
84
+ };
85
+
86
+ /**
87
+ * Non-strict DXN comparison.
88
+ * Returns true if the DXN could be resolved to the schema.
89
+ */
90
+ const isSchemaAddressableByDxn = (schema: Schema.Schema.AnyNoContext, dxn: DXN): boolean => {
91
+ if (getTypeIdentifierAnnotation(schema) === dxn.toString()) {
92
+ return true;
93
+ }
94
+
95
+ const t = dxn.asTypeDXN();
96
+ if (t) {
97
+ return t.type === getSchemaTypename(schema);
98
+ }
99
+
100
+ return false;
101
+ };
102
+
103
+ /**
104
+ * Perform vector search in the local database.
105
+ */
106
+ // TODO(dmaretskyi): Rename `GraphReadToolkit`.
107
+ export const LocalSearchToolkit = Toolkit.make(
108
+ Tool.make('search_local_search', {
109
+ description: 'Search the local database for information using a vector index',
110
+ parameters: {
111
+ query: Schema.String.annotations({
112
+ description: 'The query to search for. Could be a question or a topic or a set of keywords.',
113
+ }),
114
+ },
115
+ success: Schema.Unknown,
116
+ failure: Schema.Never,
117
+ dependencies: [DatabaseService],
118
+ }),
119
+ );
120
+
121
+ export const LocalSearchHandler = LocalSearchToolkit.toLayer({
122
+ search_local_search: Effect.fn(function* ({ query }) {
123
+ const { objects } = yield* DatabaseService.runQuery(Query.select(Filter.text(query, { type: 'vector' })));
124
+ const results = [...objects];
125
+
126
+ const option = yield* Effect.serviceOption(ContextQueueService);
127
+ if (Option.isSome(option)) {
128
+ const queueObjects = yield* Effect.promise(() => option.value.queue.queryObjects());
129
+ // TODO(dmaretskyi): Text search on the queue.
130
+ results.push(...queueObjects);
131
+ }
132
+
133
+ return trim`
134
+ <local_context>
135
+ ${JSON.stringify(results, null, 2)}
136
+ </local_context>
137
+ `;
138
+ }),
139
+ });
140
+
141
+ /**
142
+ * Attached as an annotation to the writer tool.
143
+ */
144
+ class GraphWriterSchema extends Context.Tag('@dxos/assistant/GraphWriterSchema')<
145
+ GraphWriterSchema,
146
+ {
147
+ schema: Schema.Schema.AnyNoContext[];
148
+ }
149
+ >() {}
150
+
151
+ /**
152
+ * Forms typed objects that can be written to the graph database.
153
+ */
154
+ export const makeGraphWriterToolkit = ({ schema }: { schema: Schema.Schema.AnyNoContext[] }) => {
155
+ return Toolkit.make(
156
+ Tool.make('graph_writer', {
157
+ description: 'Write to the local graph database',
158
+ parameters: createExtractionSchema(schema).fields,
159
+ success: Schema.Unknown,
160
+ failure: Schema.Never,
161
+ dependencies: [DatabaseService, ContextQueueService],
162
+ }).annotateContext(Context.make(GraphWriterSchema, { schema })),
163
+ );
164
+ };
165
+
166
+ export const makeGraphWriterHandler = (
167
+ toolkit: ReturnType<typeof makeGraphWriterToolkit>,
168
+ {
169
+ onAppend,
170
+ }: {
171
+ onAppend?: (object: DXN[]) => void;
172
+ } = {},
173
+ ) => {
174
+ const { schema } = Context.get(
175
+ toolkit.tools.graph_writer.annotations as Context.Context<GraphWriterSchema>,
176
+ GraphWriterSchema,
177
+ );
178
+
179
+ return toolkit.toLayer({
180
+ graph_writer: Effect.fn(function* (input) {
181
+ const { db } = yield* DatabaseService;
182
+ const { queue } = yield* ContextQueueService;
183
+ const data = yield* Effect.promise(() => sanitizeObjects(schema, input as any, db, queue));
184
+ yield* Effect.promise(() => queue.append(data as Obj.Any[]));
185
+
186
+ const dxns = data.map((obj) => Obj.getDXN(obj));
187
+ onAppend?.(dxns);
188
+ return dxns;
189
+ }),
190
+ });
191
+ };
192
+
193
+ /**
194
+ * Create a schema for structured data extraction.
195
+ */
196
+ export const createExtractionSchema = (types: Schema.Schema.AnyNoContext[]) => {
197
+ return Schema.Struct({
198
+ ...Object.fromEntries(
199
+ types.map(preprocessSchema).map((schema, index) => [
200
+ `objects_${getSanitizedSchemaName(types[index])}`,
201
+ Schema.optional(Schema.Array(schema)).annotations({
202
+ description: `The objects of type: ${getSchemaDXN(types[index])?.asTypeDXN()!.type}. ${SchemaAST.getDescriptionAnnotation(types[index].ast).pipe(Option.getOrElse(() => ''))}`,
203
+ }),
204
+ ]),
205
+ ),
206
+ });
207
+ };
208
+
209
+ export const getSanitizedSchemaName = (schema: Schema.Schema.AnyNoContext) => {
210
+ return getSchemaDXN(schema)!
211
+ .asTypeDXN()!
212
+ .type.replaceAll(/[^a-zA-Z0-9]+/g, '_');
213
+ };
214
+
215
+ export const sanitizeObjects = async (
216
+ types: Schema.Schema.AnyNoContext[],
217
+ data: Record<string, readonly unknown[]>,
218
+ db: EchoDatabase,
219
+ queue?: Queue,
220
+ ): Promise<Obj.Any[]> => {
221
+ const entries = types
222
+ .map(
223
+ (type) =>
224
+ data[`objects_${getSanitizedSchemaName(type)}`]?.map((object: any) => ({
225
+ data: object,
226
+ schema: type,
227
+ })) ?? [],
228
+ )
229
+ .flat();
230
+
231
+ const idMap = new Map<string, string>();
232
+ const existingIds = new Set<ObjectId>();
233
+ const enitties = new Map<ObjectId, Obj.Any | Relation.Any>();
234
+
235
+ const resolveId = (id: string): DXN | undefined => {
236
+ if (ObjectId.isValid(id)) {
237
+ existingIds.add(id);
238
+ return DXN.fromLocalObjectId(id);
239
+ }
240
+
241
+ const mappedId = idMap.get(id);
242
+ if (mappedId) {
243
+ return DXN.fromLocalObjectId(mappedId);
244
+ }
245
+
246
+ return undefined;
247
+ };
248
+
249
+ const res = entries
250
+ .map((entry) => {
251
+ // This entry mutates existing object.
252
+ if (ObjectId.isValid(entry.data.id)) {
253
+ return entry;
254
+ }
255
+
256
+ idMap.set(entry.data.id, ObjectId.random());
257
+ entry.data.id = idMap.get(entry.data.id);
258
+ return entry;
259
+ })
260
+ .map((entry) => {
261
+ const data = deepMapValues(entry.data, (value, recurse) => {
262
+ if (isEncodedReference(value)) {
263
+ const ref = value['/'];
264
+ const id = resolveId(ref);
265
+
266
+ if (id) {
267
+ // Link to an existing object.
268
+ return { '/': id.toString() };
269
+ } else {
270
+ // Search URIs?
271
+ return { '/': `search:?q=${encodeURIComponent(ref)}` };
272
+ }
273
+ }
274
+
275
+ return recurse(value);
276
+ });
277
+
278
+ if (getEntityKind(entry.schema) === 'relation') {
279
+ const sourceDxn = resolveId(data.source);
280
+ if (!sourceDxn) {
281
+ log.warn('source not found', { source: data.source });
282
+ }
283
+ const targetDxn = resolveId(data.target);
284
+ if (!targetDxn) {
285
+ log.warn('target not found', { target: data.target });
286
+ }
287
+ delete data.source;
288
+ delete data.target;
289
+ data[RelationSourceDXNId] = sourceDxn;
290
+ data[RelationTargetDXNId] = targetDxn;
291
+ }
292
+
293
+ return {
294
+ data,
295
+ schema: entry.schema,
296
+ };
297
+ })
298
+ .filter((object) => !existingIds.has(object.data.id)); // TODO(dmaretskyi): This dissallows updating existing objects.
299
+
300
+ // TODO(dmaretskyi): Use ref resolver.
301
+ const { objects: dbObjects } = await db.query(Query.select(Filter.ids(...existingIds))).run();
302
+ const queueObjects = (await queue?.getObjectsById([...existingIds])) ?? [];
303
+ const objects = [...dbObjects, ...queueObjects].filter(isNonNullable);
304
+
305
+ // TODO(dmaretskyi): Returns everything if IDs are empty!
306
+ log.info('objects', { dbObjects, queueObjects, existingIds });
307
+ const missing = Array.from(existingIds).filter((id) => !objects.some((object) => object.id === id));
308
+ if (missing.length > 0) {
309
+ throw new Error(`Object IDs do not point to existing objects: ${missing.join(', ')}`);
310
+ }
311
+
312
+ return res.flatMap(({ data, schema }) => {
313
+ let skip = false;
314
+ if (RelationSourceDXNId in data) {
315
+ const id = (data[RelationSourceDXNId] as DXN).asEchoDXN()?.echoId;
316
+ const obj = objects.find((object) => object.id === id) ?? enitties.get(id!);
317
+ if (obj) {
318
+ delete data[RelationSourceDXNId];
319
+ data[RelationSourceId] = obj;
320
+ } else {
321
+ skip = true;
322
+ }
323
+ }
324
+ if (RelationTargetDXNId in data) {
325
+ const id = (data[RelationTargetDXNId] as DXN).asEchoDXN()?.echoId;
326
+ const obj = objects.find((object) => object.id === id) ?? enitties.get(id!);
327
+ if (obj) {
328
+ delete data[RelationTargetDXNId];
329
+ data[RelationTargetId] = obj;
330
+ } else {
331
+ skip = true;
332
+ }
333
+ }
334
+ if (!skip) {
335
+ const obj = create(schema, data);
336
+ enitties.set(obj.id, obj);
337
+ return [obj];
338
+ }
339
+ return [];
340
+ });
341
+ };
342
+
343
+ const SoftRef = Schema.Struct({
344
+ '/': Schema.String,
345
+ }).annotations({
346
+ description: 'Reference to another object.',
347
+ });
348
+
349
+ const preprocessSchema = (schema: Schema.Schema.AnyNoContext) => {
350
+ const isRelationSchema = getEntityKind(schema) === 'relation';
351
+
352
+ const go = (ast: SchemaAST.AST, visited = new Set<SchemaAST.AST>()): SchemaAST.AST => {
353
+ if (visited.has(ast)) {
354
+ // Already visited this node, prevent infinite recursion.
355
+ return ast;
356
+ }
357
+ visited.add(ast);
358
+
359
+ if (SchemaAST.getAnnotation(ast, ReferenceAnnotationId).pipe(Option.isSome)) {
360
+ return SoftRef.ast;
361
+ }
362
+
363
+ return mapAst(ast, (child) => go(child, visited));
364
+ };
365
+
366
+ return Schema.make<any, any, never>(mapAst(schema.ast, (ast) => go(ast))).pipe(
367
+ Schema.omit('id'),
368
+ Schema.extend(
369
+ Schema.Struct({
370
+ id: Schema.String.annotations({
371
+ description: 'The id of this object. Come up with a unique id based on your judgement.',
372
+ }),
373
+ }),
374
+ ),
375
+ isRelationSchema
376
+ ? Schema.extend(
377
+ Schema.Struct({
378
+ source: Schema.String.annotations({
379
+ description: 'The id of the source object for this relation.',
380
+ }),
381
+ target: Schema.String.annotations({
382
+ description: 'The id of the target object for this relation.',
383
+ }),
384
+ }),
385
+ )
386
+ : Function.identity<Schema.Schema.AnyNoContext>,
387
+ );
388
+ };
@@ -0,0 +1,15 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { default as create$ } from './create-document';
6
+ import { default as research$ } from './research';
7
+
8
+ export * from './graph';
9
+ export * from './research-graph';
10
+ export * from './types';
11
+
12
+ export namespace Research {
13
+ export const create = create$;
14
+ export const research = research$;
15
+ }
@@ -0,0 +1,98 @@
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
+ - The Research Agent ALWAYS calls the `graph_writer` at the end to save the data. This conversation will be deleted, so only the data written to the graph will be preserved.
29
+
30
+ The Research Agent may be asked for:
31
+
32
+ - Technical explanations
33
+ - Literature reviews
34
+ - Comparisons
35
+ - Emerging trends
36
+ - Implementation strategies
37
+
38
+ The Research Agent begins by interpreting the user's request, then:
39
+
40
+ The Research Agent breaks it into sub-questions (if applicable).
41
+
42
+ For each sub-question, the Research Agent generates a clear, concise web search query.
43
+
44
+ The Research Agent uses `web_search`(query) to retrieve information.
45
+
46
+ The Research Agent extracts and synthesizes relevant answers.
47
+
48
+ The Research Agent's output includes:
49
+
50
+ - A clear, structured answer to the user's question
51
+ - A citation list or link list of sources used
52
+
53
+ Optionally, the Research Agent provides follow-up suggestions or questions for deeper inquiry.
54
+
55
+ Here's how the Research Agent operates:
56
+
57
+ 1. The Research Agent analyzes the user's request and identifies key topics to search for (3 or more), printing them out.
58
+ 2. The Research Agent performs a web search for each topic.
59
+ 3. The Research Agent reads and analyzes results, cross references information from multiple sources, and represents conflicting information as ranges of possible values.
60
+
61
+ 4. The Research Agent searches the local database for information using a vector index that might link to the user's question.
62
+ 6. The Research Agent creates relations and references between new objects and existing database objects when related, using existing object IDs as references.
63
+ 7. The Research Agent selects the most concrete schema types for extracted information, using multiple types as needed, and prints its decision and reasoning.
64
+ 5. The Research Agent creates a clear, structured answer to the user's question.
65
+ 8. The Research Agent submits results using the specific schema.
66
+
67
+ IMPORTANT:
68
+
69
+ - The Research Agent always runs the `local_search` tool to search the local database at least once before submitting results.
70
+ - The Research Agent does not create objects that already exist in the database.
71
+ - Ids that are not in the database are human-readable strings like `ivan_zhao_1`.
72
+
73
+ Status reporting:
74
+
75
+ The Research Agent reports its status frequently using the `<status>` tags: <status>Searching for Google Founders</status>
76
+ The Research Agent reports its status in-between each tool call and before submitting results.
77
+
78
+ <example>
79
+
80
+ Based on my research, I can now provide information about Google and it's founders.
81
+
82
+ The following objects are already in the database, I will not submit them again, but I'll re-use their IDs as references:
83
+
84
+ - 01JWRDEHPB5TT2JQQQC15038BT Google
85
+ - 01JWRDEHPA14CYW2NW9FAH6DJJ Larry Page
86
+ - 01JWRDEHPBN0BBJP57B9S108W6 Sergey Brin
87
+
88
+ I will use the following schema to construct new objects:
89
+
90
+ - type:dxos.org/type/Organization for Alphabet Inc.
91
+ - type:dxos.org/type/Person for Ivan Zhao
92
+ - type:dxos.org/type/Person for Simon Last
93
+ - dxn:type:dxos.org/relation/Employer for Ivan's employer
94
+ - dxn:type:dxos.org/relation/Employer for Simon's employer
95
+
96
+ <status>Formatting results</status>
97
+
98
+ </example>
@@ -0,0 +1,47 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Effect from 'effect/Effect';
6
+ import * as Layer from 'effect/Layer';
7
+ import * as Schema from 'effect/Schema';
8
+
9
+ import { Obj, Query, Ref, Type } from '@dxos/echo';
10
+ import { Queue } from '@dxos/echo-db';
11
+ import { ContextQueueService, DatabaseService, QueueService } from '@dxos/functions';
12
+
13
+ /**
14
+ * Container for a set of ephemeral research results.
15
+ */
16
+ export const ResearchGraph = Schema.Struct({
17
+ queue: Type.Ref(Queue),
18
+ }).pipe(
19
+ Type.Obj({
20
+ typename: 'dxos.org/type/ResearchGraph',
21
+ version: '0.1.0',
22
+ }),
23
+ );
24
+
25
+ export interface ResearchGraph extends Schema.Schema.Type<typeof ResearchGraph> {}
26
+
27
+ export const queryResearchGraph: () => Effect.Effect<ResearchGraph | undefined, never, DatabaseService> = Effect.fn(
28
+ 'queryResearchGraph',
29
+ )(function* () {
30
+ const { objects } = yield* DatabaseService.runQuery(Query.type(ResearchGraph));
31
+ return objects.at(0);
32
+ });
33
+
34
+ export const createResearchGraph: () => Effect.Effect<ResearchGraph, never, DatabaseService | QueueService> = Effect.fn(
35
+ 'createResearchGraph',
36
+ )(function* () {
37
+ const queue = yield* QueueService.createQueue();
38
+ return yield* DatabaseService.add(Obj.make(ResearchGraph, { queue: Ref.fromDXN(queue.dxn) }));
39
+ });
40
+
41
+ export const contextQueueLayerFromResearchGraph = Layer.unwrapEffect(
42
+ Effect.gen(function* () {
43
+ const researchGraph = (yield* queryResearchGraph()) ?? (yield* createResearchGraph());
44
+ const researchQueue = yield* DatabaseService.load(researchGraph.queue);
45
+ return ContextQueueService.layer(researchQueue);
46
+ }),
47
+ );