@dxos/assistant 0.8.4-main.fd6878d → 0.8.4-staging.60fe92afc8

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 (183) 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/extraction/index.mjs +208 -0
  5. package/dist/lib/neutral/extraction/index.mjs.map +7 -0
  6. package/dist/lib/neutral/index.mjs +1886 -0
  7. package/dist/lib/neutral/index.mjs.map +7 -0
  8. package/dist/lib/neutral/meta.json +1 -0
  9. package/dist/types/src/extraction/extraction-ner-function/index.d.ts +0 -1
  10. package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -1
  11. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  12. package/dist/types/src/extraction/extraction.d.ts +1650 -281
  13. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  14. package/dist/types/src/extraction/index.d.ts +0 -1
  15. package/dist/types/src/extraction/index.d.ts.map +1 -1
  16. package/dist/types/src/extraction/quotes.d.ts +3 -3
  17. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  18. package/dist/types/src/functions/services.d.ts +16 -5
  19. package/dist/types/src/functions/services.d.ts.map +1 -1
  20. package/dist/types/src/index.d.ts +2 -3
  21. package/dist/types/src/index.d.ts.map +1 -1
  22. package/dist/types/src/request/AiRequest.d.ts +308 -0
  23. package/dist/types/src/request/AiRequest.d.ts.map +1 -0
  24. package/dist/types/src/{session → request}/artifact-diff.d.ts +5 -5
  25. package/dist/types/src/request/artifact-diff.d.ts.map +1 -0
  26. package/dist/types/src/request/format.d.ts +15 -0
  27. package/dist/types/src/request/format.d.ts.map +1 -0
  28. package/dist/types/src/request/index.d.ts +4 -0
  29. package/dist/types/src/request/index.d.ts.map +1 -0
  30. package/dist/types/src/request/observer.d.ts +23 -0
  31. package/dist/types/src/request/observer.d.ts.map +1 -0
  32. package/dist/types/src/{session → request}/version-pin.d.ts +6 -6
  33. package/dist/types/src/request/version-pin.d.ts.map +1 -0
  34. package/dist/types/src/session/AiContext.d.ts +178 -0
  35. package/dist/types/src/session/AiContext.d.ts.map +1 -0
  36. package/dist/types/src/session/AiContext.test.d.ts +2 -0
  37. package/dist/types/src/session/AiContext.test.d.ts.map +1 -0
  38. package/dist/types/src/session/AiSession.d.ts +79 -0
  39. package/dist/types/src/session/AiSession.d.ts.map +1 -0
  40. package/dist/types/src/session/AiSession.test.d.ts +2 -0
  41. package/dist/types/src/session/AiSession.test.d.ts.map +1 -0
  42. package/dist/types/src/session/SessionLink.d.ts +17 -0
  43. package/dist/types/src/session/SessionLink.d.ts.map +1 -0
  44. package/dist/types/src/session/SessionLoader.d.ts +15 -0
  45. package/dist/types/src/session/SessionLoader.d.ts.map +1 -0
  46. package/dist/types/src/session/SessionLoader.test.d.ts +2 -0
  47. package/dist/types/src/session/SessionLoader.test.d.ts.map +1 -0
  48. package/dist/types/src/session/index.d.ts +5 -3
  49. package/dist/types/src/session/index.d.ts.map +1 -1
  50. package/dist/types/src/session/toolkit.d.ts +16 -0
  51. package/dist/types/src/session/toolkit.d.ts.map +1 -0
  52. package/dist/types/src/templates/system.d.ts +19 -14
  53. package/dist/types/src/templates/system.d.ts.map +1 -1
  54. package/dist/types/src/util/artifact.d.ts +18 -0
  55. package/dist/types/src/util/artifact.d.ts.map +1 -0
  56. package/dist/types/src/util/diff.d.ts +17 -0
  57. package/dist/types/src/util/diff.d.ts.map +1 -0
  58. package/dist/types/src/util/diff.test.d.ts +2 -0
  59. package/dist/types/src/util/diff.test.d.ts.map +1 -0
  60. package/dist/types/src/util/error-handling.d.ts +4 -0
  61. package/dist/types/src/util/error-handling.d.ts.map +1 -0
  62. package/dist/types/src/util/errors.d.ts +67 -0
  63. package/dist/types/src/util/errors.d.ts.map +1 -0
  64. package/dist/types/src/util/execution-graph.d.ts +85 -0
  65. package/dist/types/src/util/execution-graph.d.ts.map +1 -0
  66. package/dist/types/src/util/execution-graph.test.d.ts +2 -0
  67. package/dist/types/src/util/execution-graph.test.d.ts.map +1 -0
  68. package/dist/types/src/util/index.d.ts +7 -0
  69. package/dist/types/src/util/index.d.ts.map +1 -0
  70. package/dist/types/src/util/tracing.d.ts +296 -0
  71. package/dist/types/src/util/tracing.d.ts.map +1 -0
  72. package/dist/types/tsconfig.tsbuildinfo +1 -1
  73. package/package.json +37 -47
  74. package/src/extraction/extraction-ner-function/index.ts +0 -1
  75. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
  76. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +2 -0
  77. package/src/extraction/extraction.ts +49 -19
  78. package/src/extraction/index.ts +0 -1
  79. package/src/extraction/quotes.test.ts +2 -2
  80. package/src/extraction/quotes.ts +7 -9
  81. package/src/functions/services.ts +180 -66
  82. package/src/index.ts +2 -3
  83. package/src/request/AiRequest.ts +387 -0
  84. package/src/{session → request}/artifact-diff.ts +10 -6
  85. package/src/{session → request}/format.ts +45 -37
  86. package/src/request/index.ts +8 -0
  87. package/src/request/observer.ts +62 -0
  88. package/src/{session → request}/version-pin.ts +8 -8
  89. package/src/session/AiContext.test.ts +49 -0
  90. package/src/session/AiContext.ts +420 -0
  91. package/src/session/AiSession.test.ts +111 -0
  92. package/src/session/AiSession.ts +328 -0
  93. package/src/session/SessionLink.ts +23 -0
  94. package/src/session/SessionLoader.test.ts +115 -0
  95. package/src/session/SessionLoader.ts +55 -0
  96. package/src/session/index.ts +6 -3
  97. package/src/session/toolkit.ts +47 -0
  98. package/src/templates/{format.tpl → instructions/format.tpl} +6 -4
  99. package/src/templates/instructions/system.tpl +96 -0
  100. package/src/templates/system.ts +26 -16
  101. package/src/util/artifact.ts +96 -0
  102. package/src/util/diff.test.ts +74 -0
  103. package/src/util/diff.ts +93 -0
  104. package/src/util/error-handling.ts +42 -0
  105. package/src/util/errors.ts +15 -0
  106. package/src/util/execution-graph.test.ts +740 -0
  107. package/src/util/execution-graph.ts +520 -0
  108. package/src/util/index.ts +10 -0
  109. package/src/util/tracing.ts +60 -0
  110. package/dist/lib/browser/chunk-6WRGDEMH.mjs +0 -89
  111. package/dist/lib/browser/chunk-6WRGDEMH.mjs.map +0 -7
  112. package/dist/lib/browser/chunk-NZGGCEF3.mjs +0 -341
  113. package/dist/lib/browser/chunk-NZGGCEF3.mjs.map +0 -7
  114. package/dist/lib/browser/extraction/index.mjs +0 -269
  115. package/dist/lib/browser/extraction/index.mjs.map +0 -7
  116. package/dist/lib/browser/index.mjs +0 -354
  117. package/dist/lib/browser/index.mjs.map +0 -7
  118. package/dist/lib/browser/meta.json +0 -1
  119. package/dist/lib/browser/named-entity-recognition-O46UTCLR.mjs +0 -13
  120. package/dist/lib/node-esm/chunk-3UU5V4FS.mjs +0 -91
  121. package/dist/lib/node-esm/chunk-3UU5V4FS.mjs.map +0 -7
  122. package/dist/lib/node-esm/chunk-XOOPGSGW.mjs +0 -343
  123. package/dist/lib/node-esm/chunk-XOOPGSGW.mjs.map +0 -7
  124. package/dist/lib/node-esm/extraction/index.mjs +0 -270
  125. package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
  126. package/dist/lib/node-esm/index.mjs +0 -355
  127. package/dist/lib/node-esm/index.mjs.map +0 -7
  128. package/dist/lib/node-esm/meta.json +0 -1
  129. package/dist/lib/node-esm/named-entity-recognition-W4VMRCH6.mjs +0 -14
  130. package/dist/lib/node-esm/named-entity-recognition-W4VMRCH6.mjs.map +0 -7
  131. package/dist/types/src/conversation/context.d.ts +0 -90
  132. package/dist/types/src/conversation/context.d.ts.map +0 -1
  133. package/dist/types/src/conversation/conversation.d.ts +0 -50
  134. package/dist/types/src/conversation/conversation.d.ts.map +0 -1
  135. package/dist/types/src/conversation/index.d.ts +0 -3
  136. package/dist/types/src/conversation/index.d.ts.map +0 -1
  137. package/dist/types/src/errors.d.ts +0 -39
  138. package/dist/types/src/errors.d.ts.map +0 -1
  139. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +0 -4
  140. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +0 -1
  141. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +0 -2
  142. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +0 -1
  143. package/dist/types/src/extraction/extraction-llm-function/index.d.ts +0 -2
  144. package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +0 -1
  145. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +0 -306
  146. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +0 -1
  147. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +0 -2
  148. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +0 -1
  149. package/dist/types/src/session/artifact-diff.d.ts.map +0 -1
  150. package/dist/types/src/session/format.d.ts +0 -168
  151. package/dist/types/src/session/format.d.ts.map +0 -1
  152. package/dist/types/src/session/format.test.d.ts +0 -2
  153. package/dist/types/src/session/format.test.d.ts.map +0 -1
  154. package/dist/types/src/session/session.d.ts +0 -167
  155. package/dist/types/src/session/session.d.ts.map +0 -1
  156. package/dist/types/src/session/session.test.d.ts +0 -2
  157. package/dist/types/src/session/session.test.d.ts.map +0 -1
  158. package/dist/types/src/session/version-pin.d.ts.map +0 -1
  159. package/dist/types/src/session/version-pin.test.d.ts +0 -2
  160. package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
  161. package/dist/types/src/summarization/summarization.test.d.ts +0 -2
  162. package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
  163. package/dist/types/src/types.d.ts +0 -16
  164. package/dist/types/src/types.d.ts.map +0 -1
  165. package/src/conversation/context.ts +0 -133
  166. package/src/conversation/conversation.ts +0 -125
  167. package/src/conversation/index.ts +0 -6
  168. package/src/errors.ts +0 -10
  169. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -84
  170. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -71
  171. package/src/extraction/extraction-llm-function/index.ts +0 -5
  172. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +0 -116
  173. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +0 -57
  174. package/src/session/__snapshots__/session.test.ts.snap +0 -32
  175. package/src/session/format.test.ts +0 -60
  176. package/src/session/session.test.ts +0 -305
  177. package/src/session/session.ts +0 -279
  178. package/src/session/version-pin.test.ts +0 -7
  179. package/src/summarization/summarization.test.ts +0 -197
  180. package/src/templates/system.tpl +0 -47
  181. package/src/types.ts +0 -52
  182. /package/dist/lib/{browser/named-entity-recognition-O46UTCLR.mjs.map → neutral/chunk-J5LGTIGS.mjs.map} +0 -0
  183. /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
@@ -1,269 +0,0 @@
1
- import {
2
- AiSession
3
- } from "../chunk-NZGGCEF3.mjs";
4
- import {
5
- combineNerTokens,
6
- createTokenGroups,
7
- extractFullEntities,
8
- getNer
9
- } from "../chunk-6WRGDEMH.mjs";
10
-
11
- // src/extraction/extraction.ts
12
- import { Schema } from "effect";
13
- import { asyncTimeout } from "@dxos/async";
14
- import { Expando } from "@dxos/echo-schema";
15
- import { log } from "@dxos/log";
16
- import { DataType } from "@dxos/schema";
17
- var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/extraction/extraction.ts";
18
- var ExtractionInput = Schema.Struct({
19
- message: DataType.Message,
20
- objects: Schema.optional(Schema.Array(Expando)),
21
- options: Schema.optional(Schema.Struct({
22
- timeout: Schema.optional(Schema.Number),
23
- fallbackToRaw: Schema.optional(Schema.Boolean)
24
- }))
25
- });
26
- var ExtractionOutput = Schema.Struct({
27
- message: DataType.Message,
28
- timeElapsed: Schema.Number
29
- });
30
- var processTranscriptMessage = async (params) => {
31
- try {
32
- if (params.options?.timeout && params.options.timeout > 0) {
33
- return await asyncTimeout(params.executor.invoke(params.function, params.input), params.options.timeout);
34
- } else {
35
- return await params.executor.invoke(params.function, params.input);
36
- }
37
- } catch (error) {
38
- if (params.options?.fallbackToRaw) {
39
- log.warn("failed to process message, falling back to raw text", {
40
- error
41
- }, {
42
- F: __dxlog_file,
43
- L: 66,
44
- S: void 0,
45
- C: (f, a) => f(...a)
46
- });
47
- return {
48
- message: params.input.message,
49
- timeElapsed: 0
50
- };
51
- } else {
52
- throw error;
53
- }
54
- }
55
- };
56
-
57
- // src/extraction/extraction-llm-function/extraction-llm-function.ts
58
- import { AiService, ToolRegistry } from "@dxos/ai";
59
- import { Obj } from "@dxos/echo";
60
- import { create } from "@dxos/echo-schema";
61
- import { defineFunction } from "@dxos/functions";
62
- import { DataType as DataType2 } from "@dxos/schema";
63
-
64
- // src/extraction/quotes.ts
65
- import { Schema as Schema2 } from "effect";
66
- import { Filter, Query } from "@dxos/echo";
67
- import { ObjectId } from "@dxos/echo-schema";
68
- import { DXN } from "@dxos/keys";
69
- import { log as log2 } from "@dxos/log";
70
- var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/extraction/quotes.ts";
71
- var ReferencedQuotes = Schema2.Struct({
72
- references: Schema2.Array(Schema2.Struct({
73
- id: Schema2.String,
74
- quote: Schema2.String
75
- })).pipe(Schema2.mutable).annotations({
76
- // TODO(burdon): Does this description make sense?
77
- description: `
78
- The references to the context objects that are mentioned in the transcript.
79
- Quote should match the original transcript text exactly, while id is the id of the context object.
80
- `
81
- })
82
- });
83
- var findQuotes = async (quotes, db) => {
84
- const references = await Promise.all(quotes.map(async (quote) => {
85
- const { objects } = await db.query(Query.select(Filter.text(quote, {
86
- type: "vector"
87
- }))).run();
88
- log2.info("found quotes in vector index", {
89
- objects,
90
- quote
91
- }, {
92
- F: __dxlog_file2,
93
- L: 35,
94
- S: void 0,
95
- C: (f, a) => f(...a)
96
- });
97
- return objects.length > 0 ? [
98
- {
99
- id: objects[0].id.toString(),
100
- quote
101
- }
102
- ] : [];
103
- }));
104
- return {
105
- references: references.flat()
106
- };
107
- };
108
- var insertReferences = (text, quotes) => {
109
- for (const quote of quotes.references) {
110
- if (!ObjectId.isValid(quote.id)) {
111
- continue;
112
- }
113
- const regex = new RegExp(quote.quote.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "gi");
114
- text = text.replace(regex, `[${quote.quote}][${DXN.fromLocalObjectId(quote.id).toString()}]`);
115
- }
116
- return text;
117
- };
118
-
119
- // raw-loader:./instructions.tpl?raw
120
- var instructions_default = 'You are an AI assistant specialized in processing and enhancing transcripts with contextual information.\nYour task is to analyze a given transcript and extract references to the context objects.\n\nThe context and transcript are provided in the user message in `context` and `transcript` XML tags.\n\nHere are your instructions:\n\n1. Read the transcript and context information provided above.\n\n2. Process each segment of the transcript separately.\n\n3. For each segment:\n a. Analyze the content and identify relevant information from the context.\n b. Extract a number of references to the context objects that are mentioned in the segment.\n c. Output the references in the format `{ "quote": "earnings report", "id": "01JT0JP9AX0XKGZX4MV4B69VT6" }`.\n d. If there are no references, just output an empty array.\n\n4. Maintain the exact order and structure of the original transcript segments.\n\nOutput Format:\nYou have been provided with a tool that specifies output format\n\nRemember:\n- Only quote the text that is in the transcript.\n- Analyze each segment carefully before processing it.\n- Only reference objects that appear in the context and exactly by their id, If the object is not in the context, do not insert a reference. \n\nNow, process the transcript and context provided below according to these instructions.';
121
-
122
- // src/extraction/extraction-llm-function/extraction-llm-function.ts
123
- var extractionAnthropicFn = defineFunction({
124
- description: "Extract entities from the transcript message and add them to the message.",
125
- inputSchema: ExtractionInput,
126
- outputSchema: ExtractionOutput,
127
- handler: async ({ data: { message, objects }, context }) => {
128
- const startTime = performance.now();
129
- const ai = context.getService(AiService.AiService);
130
- const session = new AiSession({
131
- operationModel: "configured"
132
- });
133
- const result = await session.runStructured(ReferencedQuotes, {
134
- generationOptions: {
135
- model: "@anthropic/claude-3-5-haiku-20241022"
136
- },
137
- client: ai.client,
138
- systemPrompt: instructions_default,
139
- history: [
140
- Obj.make(DataType2.Message, {
141
- created: (/* @__PURE__ */ new Date()).toISOString(),
142
- sender: {
143
- role: "user"
144
- },
145
- blocks: [
146
- {
147
- _tag: "text",
148
- text: `<context>${JSON.stringify(objects)}</context>`
149
- },
150
- {
151
- _tag: "text",
152
- text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`
153
- }
154
- ]
155
- })
156
- ],
157
- artifacts: [],
158
- prompt: "",
159
- tools: [],
160
- toolResolver: new ToolRegistry([])
161
- });
162
- return {
163
- message: create(DataType2.Message, {
164
- ...message,
165
- blocks: message.blocks.map((block, i) => block._tag !== "transcript" ? block : {
166
- ...block,
167
- text: insertReferences(block.text, result)
168
- })
169
- }),
170
- timeElapsed: performance.now() - startTime
171
- };
172
- }
173
- });
174
-
175
- // src/extraction/extraction-ner-function/extraction-ner-function.ts
176
- import { asyncTimeout as asyncTimeout2 } from "@dxos/async";
177
- import { create as create2 } from "@dxos/echo-schema";
178
- import { DatabaseService, defineFunction as defineFunction2 } from "@dxos/functions";
179
- import { invariant } from "@dxos/invariant";
180
- import { log as log3 } from "@dxos/log";
181
- import { DataType as DataType3 } from "@dxos/schema";
182
- var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/extraction/extraction-ner-function/extraction-ner-function.ts";
183
- var extractionNerFn = defineFunction2({
184
- description: "Extracts named entities from a text",
185
- inputSchema: ExtractionInput,
186
- outputSchema: ExtractionOutput,
187
- handler: async ({ data: { message, options }, context }) => {
188
- log3.info("input", {
189
- message,
190
- options
191
- }, {
192
- F: __dxlog_file3,
193
- L: 24,
194
- S: void 0,
195
- C: (f, a) => f(...a)
196
- });
197
- const startTime = performance.now();
198
- const { db } = context.getService(DatabaseService);
199
- const { extractFullEntities: extractFullEntities2 } = await import("../named-entity-recognition-O46UTCLR.mjs");
200
- const entitiesPromise = Promise.all(message.blocks.map(async (block) => {
201
- invariant(block._tag === "transcript" || block._tag === "text", "Block must have text", {
202
- F: __dxlog_file3,
203
- L: 31,
204
- S: void 0,
205
- A: [
206
- "block._tag === 'transcript' || block._tag === 'text'",
207
- "'Block must have text'"
208
- ]
209
- });
210
- return extractFullEntities2(block.text);
211
- })).then((entities2) => entities2.flat());
212
- const entities = options?.timeout ? await asyncTimeout2(entitiesPromise, options.timeout) : await entitiesPromise;
213
- log3.info("entities", {
214
- entities
215
- }, {
216
- F: __dxlog_file3,
217
- L: 36,
218
- S: void 0,
219
- C: (f, a) => f(...a)
220
- });
221
- const quoteReferences = await findQuotes(entities.map((entity) => entity.word), db);
222
- const blocksWithReferences = message.blocks.map((block, i) => {
223
- invariant(block._tag === "transcript" || block._tag === "text", "Block must have text", {
224
- F: __dxlog_file3,
225
- L: 42,
226
- S: void 0,
227
- A: [
228
- "block._tag === 'transcript' || block._tag === 'text'",
229
- "'Block must have text'"
230
- ]
231
- });
232
- return {
233
- ...block,
234
- text: insertReferences(block._tag, quoteReferences)
235
- };
236
- });
237
- const messageWithReferences = create2(DataType3.Message, {
238
- ...message,
239
- blocks: blocksWithReferences
240
- });
241
- log3.info("output", {
242
- messageWithReferences
243
- }, {
244
- F: __dxlog_file3,
245
- L: 50,
246
- S: void 0,
247
- C: (f, a) => f(...a)
248
- });
249
- return {
250
- message: messageWithReferences,
251
- timeElapsed: performance.now() - startTime
252
- };
253
- }
254
- });
255
- export {
256
- ExtractionInput,
257
- ExtractionOutput,
258
- ReferencedQuotes,
259
- combineNerTokens,
260
- createTokenGroups,
261
- extractFullEntities,
262
- extractionAnthropicFn,
263
- extractionNerFn,
264
- findQuotes,
265
- getNer,
266
- insertReferences,
267
- processTranscriptMessage
268
- };
269
- //# sourceMappingURL=index.mjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../../src/extraction/extraction.ts", "../../../../src/extraction/extraction-llm-function/extraction-llm-function.ts", "../../../../src/extraction/quotes.ts", "raw-loader:./instructions.tpl?raw", "../../../../src/extraction/extraction-ner-function/extraction-ner-function.ts"],
4
- "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport { Schema } from 'effect';\n\nimport { asyncTimeout } from '@dxos/async';\nimport { Expando } from '@dxos/echo-schema';\nimport { type FunctionDefinition, type FunctionExecutor } from '@dxos/functions';\nimport { log } from '@dxos/log';\nimport { DataType } from '@dxos/schema';\n\nexport const ExtractionInput = Schema.Struct({\n message: DataType.Message,\n objects: Schema.optional(Schema.Array(Expando)),\n options: Schema.optional(\n Schema.Struct({\n timeout: Schema.optional(Schema.Number),\n fallbackToRaw: Schema.optional(Schema.Boolean),\n }),\n ),\n});\nexport interface ExtractionInput extends Schema.Schema.Type<typeof ExtractionInput> {}\n\nexport const ExtractionOutput = Schema.Struct({\n message: DataType.Message,\n timeElapsed: Schema.Number,\n});\nexport interface ExtractionOutput extends Schema.Schema.Type<typeof ExtractionOutput> {}\n\nexport type ProcessTranscriptMessageParams = {\n input: ExtractionInput;\n function: FunctionDefinition<ExtractionInput, ExtractionOutput>;\n executor: FunctionExecutor;\n\n options?: {\n /**\n * Timeout for the entity extraction.\n */\n timeout?: number;\n\n /**\n * Fallback to raw text if the entity extraction fails.\n * Otherwise the function will throw an error.\n * @default false\n */\n fallbackToRaw?: boolean;\n };\n};\n\nexport type ExtractionFunction = FunctionDefinition<ExtractionInput, ExtractionOutput>;\n\n/**\n * Extract entities from the transcript message and add them to the message.\n */\n// TODO(dmaretskyi): Move context to a vector search index.\nexport const processTranscriptMessage = async (params: ProcessTranscriptMessageParams): Promise<ExtractionOutput> => {\n try {\n if (params.options?.timeout && params.options.timeout > 0) {\n return await asyncTimeout(params.executor.invoke(params.function, params.input), params.options.timeout);\n } else {\n return await params.executor.invoke(params.function, params.input);\n }\n } catch (error) {\n if (params.options?.fallbackToRaw) {\n log.warn('failed to process message, falling back to raw text', { error });\n return {\n message: params.input.message,\n timeElapsed: 0,\n };\n } else {\n throw error;\n }\n }\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\n// ISSUE(burdon): defineFunction\n// @ts-nocheck\n\nimport { AiService, ToolRegistry } from '@dxos/ai';\nimport { Obj } from '@dxos/echo';\nimport { create } from '@dxos/echo-schema';\nimport { type FunctionDefinition, defineFunction } from '@dxos/functions';\nimport { type ContentBlock, DataType } from '@dxos/schema';\n\nimport { AiSession } from '../../session';\nimport { ExtractionInput, ExtractionOutput } from '../extraction';\nimport { ReferencedQuotes, insertReferences } from '../quotes';\n\nimport PROMPT from './instructions.tpl?raw';\n\nexport const extractionAnthropicFn: FunctionDefinition<ExtractionInput, ExtractionOutput> = defineFunction({\n description: 'Extract entities from the transcript message and add them to the message.',\n inputSchema: ExtractionInput,\n outputSchema: ExtractionOutput,\n handler: async ({ data: { message, objects }, context }) => {\n const startTime = performance.now();\n const ai = context.getService(AiService.AiService);\n const session = new AiSession({ operationModel: 'configured' });\n const result = await session.runStructured(ReferencedQuotes, {\n generationOptions: {\n model: '@anthropic/claude-3-5-haiku-20241022',\n },\n client: ai.client,\n systemPrompt: PROMPT,\n history: [\n Obj.make(DataType.Message, {\n created: new Date().toISOString(),\n sender: { role: 'user' },\n blocks: [\n {\n _tag: 'text',\n text: `<context>${JSON.stringify(objects)}</context>`,\n } satisfies ContentBlock.Text,\n {\n _tag: 'text',\n text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`,\n } satisfies ContentBlock.Text,\n ],\n }),\n ],\n artifacts: [],\n prompt: '',\n tools: [],\n toolResolver: new ToolRegistry([]),\n } as any); // TODO(burdon): Rewrite test.\n\n return {\n message: create(DataType.Message, {\n ...message,\n blocks: message.blocks.map((block, i) =>\n block._tag !== 'transcript'\n ? block\n : {\n ...block,\n text: insertReferences(block.text, result),\n },\n ),\n }),\n timeElapsed: performance.now() - startTime,\n };\n },\n});\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { Schema } from 'effect';\n\nimport { Filter, Query } from '@dxos/echo';\nimport { type EchoDatabase } from '@dxos/echo-db';\nimport { ObjectId } from '@dxos/echo-schema';\nimport { DXN } from '@dxos/keys';\nimport { log } from '@dxos/log';\n\nexport const ReferencedQuotes = Schema.Struct({\n references: Schema.Array(\n Schema.Struct({\n id: Schema.String,\n quote: Schema.String, // TODO(burdon): Quote?\n }),\n )\n .pipe(Schema.mutable)\n .annotations({\n // TODO(burdon): Does this description make sense?\n description: `\n The references to the context objects that are mentioned in the transcript. \n Quote should match the original transcript text exactly, while id is the id of the context object.\n `,\n }),\n});\nexport type ReferencedQuotes = Schema.Schema.Type<typeof ReferencedQuotes>;\n\nexport const findQuotes = async (quotes: string[], db: EchoDatabase): Promise<ReferencedQuotes> => {\n const references = await Promise.all(\n quotes.map(async (quote) => {\n const { objects } = await db.query(Query.select(Filter.text(quote, { type: 'vector' }))).run();\n log.info('found quotes in vector index', { objects, quote });\n return objects.length > 0 ? [{ id: objects[0].id.toString(), quote }] : [];\n }),\n );\n return { references: references.flat() };\n};\n\n/**\n * Finds and replaces all quotes with DXNs references.\n */\n// TODO(dmaretskyi): Lookup and verifiy ids from provided context.\nexport const insertReferences = (text: string, quotes: ReferencedQuotes) => {\n for (const quote of quotes.references) {\n if (!ObjectId.isValid(quote.id)) {\n continue;\n }\n\n // Use a case-insensitive regular expression to replace the quote.\n const regex = new RegExp(quote.quote.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&'), 'gi');\n text = text.replace(regex, `[${quote.quote}][${DXN.fromLocalObjectId(quote.id).toString()}]`);\n }\n\n return text;\n};\n", "You are an AI assistant specialized in processing and enhancing transcripts with contextual information.\nYour task is to analyze a given transcript and extract references to the context objects.\n\nThe context and transcript are provided in the user message in `context` and `transcript` XML tags.\n\nHere are your instructions:\n\n1. Read the transcript and context information provided above.\n\n2. Process each segment of the transcript separately.\n\n3. For each segment:\n a. Analyze the content and identify relevant information from the context.\n b. Extract a number of references to the context objects that are mentioned in the segment.\n c. Output the references in the format `{ \"quote\": \"earnings report\", \"id\": \"01JT0JP9AX0XKGZX4MV4B69VT6\" }`.\n d. If there are no references, just output an empty array.\n\n4. Maintain the exact order and structure of the original transcript segments.\n\nOutput Format:\nYou have been provided with a tool that specifies output format\n\nRemember:\n- Only quote the text that is in the transcript.\n- Analyze each segment carefully before processing it.\n- Only reference objects that appear in the context and exactly by their id, If the object is not in the context, do not insert a reference. \n\nNow, process the transcript and context provided below according to these instructions.", "//\n// Copyright 2025 DXOS.org\n//\n\n// ISSUE(burdon): defineFunction\n// @ts-nocheck\n\nimport { asyncTimeout } from '@dxos/async';\nimport { create } from '@dxos/echo-schema';\nimport { DatabaseService, defineFunction } from '@dxos/functions';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\nimport { DataType } from '@dxos/schema';\n\nimport { ExtractionInput, ExtractionOutput } from '../extraction';\nimport { findQuotes, insertReferences } from '../quotes';\n\n// TODO(mykola): Make it use vector index to query objects and not pass objects as input.\nexport const extractionNerFn = defineFunction({\n description: 'Extracts named entities from a text',\n inputSchema: ExtractionInput,\n outputSchema: ExtractionOutput,\n handler: async ({ data: { message, options }, context }) => {\n log.info('input', { message, options });\n const startTime = performance.now();\n const { db } = context.getService(DatabaseService);\n const { extractFullEntities } = await import('./named-entity-recognition');\n\n const entitiesPromise = Promise.all(\n message.blocks.map(async (block) => {\n invariant(block._tag === 'transcript' || block._tag === 'text', 'Block must have text');\n return extractFullEntities(block.text);\n }),\n ).then((entities) => entities.flat());\n const entities = options?.timeout ? await asyncTimeout(entitiesPromise, options.timeout) : await entitiesPromise;\n log.info('entities', { entities });\n const quoteReferences = await findQuotes(\n entities.map((entity) => entity.word),\n db,\n );\n const blocksWithReferences = message.blocks.map((block, i) => {\n invariant(block._tag === 'transcript' || block._tag === 'text', 'Block must have text');\n return { ...block, text: insertReferences(block._tag, quoteReferences) };\n });\n const messageWithReferences = create(DataType.Message, {\n ...message,\n blocks: blocksWithReferences,\n });\n\n log.info('output', { messageWithReferences });\n\n return {\n message: messageWithReferences,\n timeElapsed: performance.now() - startTime,\n };\n },\n});\n"],
5
- "mappings": ";;;;;;;;;;;AAIA,SAASA,cAAc;AAEvB,SAASC,oBAAoB;AAC7B,SAASC,eAAe;AAExB,SAASC,WAAW;AACpB,SAASC,gBAAgB;;AA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;AACrC,UAAMI,YAAYC,YAAYC,IAAG;AACjC,UAAM,EAAEC,GAAE,IAAKN,QAAQO,WAAWC,eAAAA;AAClC,UAAM,EAAEC,qBAAAA,qBAAmB,IAAK,MAAM,OAAO,0CAAA;AAE7C,UAAMC,kBAAkBC,QAAQC,IAC9Bd,QAAQe,OAAOC,IAAI,OAAOC,UAAAA;AACxBC,gBAAUD,MAAME,SAAS,gBAAgBF,MAAME,SAAS,QAAQ,wBAAA;;;;;;;;;AAChE,aAAOR,qBAAoBM,MAAMG,IAAI;IACvC,CAAA,CAAA,EACAC,KAAK,CAACC,cAAaA,UAASC,KAAI,CAAA;AAClC,UAAMD,WAAWrB,SAASuB,UAAU,MAAMC,cAAab,iBAAiBX,QAAQuB,OAAO,IAAI,MAAMZ;AACjGT,IAAAA,KAAIC,KAAK,YAAY;MAAEkB;IAAS,GAAA;;;;;;AAChC,UAAMI,kBAAkB,MAAMC,WAC5BL,SAASN,IAAI,CAACY,WAAWA,OAAOC,IAAI,GACpCrB,EAAAA;AAEF,UAAMsB,uBAAuB9B,QAAQe,OAAOC,IAAI,CAACC,OAAOc,MAAAA;AACtDb,gBAAUD,MAAME,SAAS,gBAAgBF,MAAME,SAAS,QAAQ,wBAAA;;;;;;;;;AAChE,aAAO;QAAE,GAAGF;QAAOG,MAAMY,iBAAiBf,MAAME,MAAMO,eAAAA;MAAiB;IACzE,CAAA;AACA,UAAMO,wBAAwBC,QAAOC,UAASC,SAAS;MACrD,GAAGpC;MACHe,QAAQe;IACV,CAAA;AAEA3B,IAAAA,KAAIC,KAAK,UAAU;MAAE6B;IAAsB,GAAA;;;;;;AAE3C,WAAO;MACLjC,SAASiC;MACTI,aAAa/B,YAAYC,IAAG,IAAKF;IACnC;EACF;AACF,CAAA;",
6
- "names": ["Schema", "asyncTimeout", "Expando", "log", "DataType", "ExtractionInput", "Struct", "message", "Message", "objects", "optional", "Array", "options", "timeout", "Number", "fallbackToRaw", "Boolean", "ExtractionOutput", "timeElapsed", "processTranscriptMessage", "params", "executor", "invoke", "function", "input", "error", "warn", "AiService", "ToolRegistry", "Obj", "create", "defineFunction", "DataType", "Schema", "Filter", "Query", "ObjectId", "DXN", "log", "ReferencedQuotes", "Struct", "references", "Array", "id", "String", "quote", "pipe", "mutable", "annotations", "description", "findQuotes", "quotes", "db", "Promise", "all", "map", "objects", "query", "select", "text", "type", "run", "info", "length", "toString", "flat", "insertReferences", "isValid", "regex", "RegExp", "replace", "fromLocalObjectId", "extractionAnthropicFn", "defineFunction", "description", "inputSchema", "ExtractionInput", "outputSchema", "ExtractionOutput", "handler", "data", "message", "objects", "context", "startTime", "performance", "now", "ai", "getService", "AiService", "session", "AiSession", "operationModel", "result", "runStructured", "ReferencedQuotes", "generationOptions", "model", "client", "systemPrompt", "PROMPT", "history", "Obj", "make", "DataType", "Message", "created", "Date", "toISOString", "sender", "role", "blocks", "_tag", "text", "JSON", "stringify", "artifacts", "prompt", "tools", "toolResolver", "ToolRegistry", "create", "map", "block", "i", "insertReferences", "timeElapsed", "asyncTimeout", "create", "DatabaseService", "defineFunction", "invariant", "log", "DataType", "extractionNerFn", "defineFunction", "description", "inputSchema", "ExtractionInput", "outputSchema", "ExtractionOutput", "handler", "data", "message", "options", "context", "log", "info", "startTime", "performance", "now", "db", "getService", "DatabaseService", "extractFullEntities", "entitiesPromise", "Promise", "all", "blocks", "map", "block", "invariant", "_tag", "text", "then", "entities", "flat", "timeout", "asyncTimeout", "quoteReferences", "findQuotes", "entity", "word", "blocksWithReferences", "i", "insertReferences", "messageWithReferences", "create", "DataType", "Message", "timeElapsed"]
7
- }
@@ -1,354 +0,0 @@
1
- import {
2
- AiAssistantError,
3
- AiSession,
4
- ArtifactDiffResolver,
5
- GenerationObserver,
6
- VersionPin
7
- } from "./chunk-NZGGCEF3.mjs";
8
-
9
- // src/conversation/context.ts
10
- import { computed } from "@preact/signals-core";
11
- import { Schema } from "effect";
12
- import { Array, pipe } from "effect";
13
- import { Blueprint } from "@dxos/blueprints";
14
- import { Obj, Type } from "@dxos/echo";
15
- import { ComplexSet } from "@dxos/util";
16
- var ContextBinding = Schema.Struct({
17
- blueprints: Schema.Struct({
18
- added: Schema.Array(Type.Ref(Blueprint.Blueprint)),
19
- removed: Schema.Array(Type.Ref(Blueprint.Blueprint))
20
- }),
21
- // TODO(burdon): Type.Expando => Type.Obj (or Obj.Any?)
22
- objects: Schema.Struct({
23
- added: Schema.Array(Type.Ref(Type.Expando)),
24
- removed: Schema.Array(Type.Ref(Type.Expando))
25
- })
26
- }).pipe(Type.Obj({
27
- typename: "dxos.org/type/ContextBinding",
28
- version: "0.1.0"
29
- }));
30
- var Bindings = class {
31
- blueprints = new ComplexSet((ref) => ref.dxn.toString());
32
- objects = new ComplexSet((ref) => ref.dxn.toString());
33
- toJSON() {
34
- return {
35
- blueprints: Array.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
36
- objects: Array.fromIterable(this.objects).map((ref) => ref.dxn.toString())
37
- };
38
- }
39
- };
40
- var AiContextBinder = class {
41
- _queue;
42
- /**
43
- * Reactive query of all bindings.
44
- */
45
- // TODO(burdon): Cache value?
46
- bindings;
47
- blueprints;
48
- objects;
49
- constructor(_queue) {
50
- this._queue = _queue;
51
- this.bindings = computed(() => this._reduce(this._queue.objects));
52
- this.blueprints = computed(() => [
53
- ...this.bindings.value.blueprints
54
- ]);
55
- this.objects = computed(() => [
56
- ...this.bindings.value.objects
57
- ]);
58
- }
59
- /**
60
- * Asynchronous query of all bindings.
61
- */
62
- async query() {
63
- const queueItems = await this._queue.queryObjects();
64
- return this._reduce(queueItems);
65
- }
66
- // TODO(burdon): Pass in Blueprint obj (from registry?) and create reference.
67
- async bind(props) {
68
- if (!props.blueprints?.length && !props.objects?.length) {
69
- return;
70
- }
71
- await this._queue.append([
72
- Obj.make(ContextBinding, {
73
- blueprints: {
74
- added: props.blueprints ?? [],
75
- removed: []
76
- },
77
- objects: {
78
- added: props.objects ?? [],
79
- removed: []
80
- }
81
- })
82
- ]);
83
- }
84
- async unbind(props) {
85
- if (!props.blueprints?.length && !props.objects?.length) {
86
- return;
87
- }
88
- await this._queue.append([
89
- Obj.make(ContextBinding, {
90
- blueprints: {
91
- added: [],
92
- removed: props.blueprints ?? []
93
- },
94
- objects: {
95
- added: [],
96
- removed: props.objects ?? []
97
- }
98
- })
99
- ]);
100
- }
101
- _reduce(items) {
102
- return pipe(items, Array.filter(Obj.instanceOf(ContextBinding)), Array.reduce(new Bindings(), (context, item) => {
103
- item.blueprints.removed.forEach((item2) => context.blueprints.delete(item2));
104
- item.blueprints.added.forEach((item2) => context.blueprints.add(item2));
105
- item.objects.removed.forEach((item2) => context.objects.delete(item2));
106
- item.objects.added.forEach((item2) => context.objects.add(item2));
107
- return context;
108
- }));
109
- }
110
- };
111
-
112
- // src/conversation/conversation.ts
113
- import { Effect } from "effect";
114
- import { Event } from "@dxos/async";
115
- import { Obj as Obj2 } from "@dxos/echo";
116
- import { DatabaseService } from "@dxos/functions";
117
- import { log } from "@dxos/log";
118
- import { DataType } from "@dxos/schema";
119
- var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
120
- var AiConversation = class {
121
- _queue;
122
- /**
123
- * Blueprints bound to the conversation.
124
- */
125
- _context;
126
- /**
127
- * Fired when the execution loop begins.
128
- * This is called before the first message is sent.
129
- *
130
- * @deprecated Pass in a session instead.
131
- */
132
- // TODO(burdon): Is this still deprecated?
133
- onBegin = new Event();
134
- constructor(options) {
135
- this._queue = options.queue;
136
- this._context = new AiContextBinder(this._queue);
137
- }
138
- get context() {
139
- return this._context;
140
- }
141
- async getHistory() {
142
- const queueItems = await this._queue.queryObjects();
143
- return queueItems.filter(Obj2.instanceOf(DataType.Message));
144
- }
145
- /**
146
- * Executes a prompt.
147
- * Each invocation creates a new `AiSession`, which handles potential tool calls.
148
- */
149
- run = ({
150
- // TODO(burdon): Decide whether to pass in or to fully encapsulate.
151
- session = new AiSession(),
152
- ...params
153
- }) => Effect.gen(this, function* () {
154
- const history = yield* Effect.promise(() => this.getHistory());
155
- const context = yield* Effect.promise(() => this.context.query());
156
- const blueprints = yield* Effect.forEach(context.blueprints.values(), DatabaseService.load);
157
- const objects = yield* Effect.forEach(context.objects.values(), DatabaseService.load);
158
- log.info("run", {
159
- prompt: params.prompt,
160
- system: params.system,
161
- history: history.length,
162
- objects: objects.length,
163
- blueprints: blueprints.length,
164
- toolkit: params.toolkit
165
- }, {
166
- F: __dxlog_file,
167
- L: 99,
168
- S: this,
169
- C: (f, a) => f(...a)
170
- });
171
- const start = Date.now();
172
- this.onBegin.emit(session);
173
- const messages = yield* session.run({
174
- prompt: params.prompt,
175
- system: params.system,
176
- history,
177
- objects,
178
- blueprints,
179
- toolkit: params.toolkit,
180
- observer: params.observer
181
- });
182
- log.info("result", {
183
- messages,
184
- duration: Date.now() - start
185
- }, {
186
- F: __dxlog_file,
187
- L: 121,
188
- S: this,
189
- C: (f, a) => f(...a)
190
- });
191
- yield* Effect.promise(() => this._queue.append(messages));
192
- return messages;
193
- }).pipe(Effect.withSpan("AiConversation.run"));
194
- };
195
-
196
- // src/functions/services.ts
197
- import { AiTool } from "@effect/ai";
198
- import { Context, Effect as Effect2, Layer, Match, Predicate, Record, Schema as Schema2 } from "effect";
199
- import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from "@dxos/ai";
200
- import { LocalFunctionExecutionService } from "@dxos/functions";
201
- import { invariant } from "@dxos/invariant";
202
- var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/functions/services.ts";
203
- var makeToolResolverFromFunctions = (functions, toolkit) => {
204
- return Layer.succeed(ToolResolverService, {
205
- resolve: Effect2.fn("resolveTool")(function* (id) {
206
- const tool = toolkit.tools[id];
207
- if (tool) {
208
- return tool;
209
- }
210
- const fn = functions.find((f) => f.name === id);
211
- if (!fn) {
212
- return yield* Effect2.fail(new AiToolNotFoundError(id));
213
- }
214
- return projectFunctionToTool(fn);
215
- })
216
- });
217
- };
218
- var makeToolExecutionServiceFromFunctions = (functions, toolkit, handlersLayer) => {
219
- return Layer.effect(ToolExecutionService, Effect2.gen(function* () {
220
- const toolkitHandler = yield* toolkit.pipe(Effect2.provide(handlersLayer));
221
- const localFunctionExecutionService = yield* LocalFunctionExecutionService;
222
- return {
223
- handlersFor: (toolkit2) => {
224
- const makeHandler = (tool) => {
225
- return Effect2.fn("toolFunctionHandler")(function* (input) {
226
- if (toolkitHandler.tools.find((t) => t.name === tool.name)) {
227
- return yield* toolkitHandler.handle(tool.name, input);
228
- }
229
- const { functionName } = Context.get(FunctionToolAnnotation)(tool.annotations);
230
- const fnDef = functions.find((f) => f.name === functionName);
231
- if (!fnDef) {
232
- return yield* Effect2.fail(new AiToolNotFoundError(tool.name));
233
- }
234
- return yield* localFunctionExecutionService.invokeFunction(fnDef, input).pipe(Effect2.catchAllDefect((defect) => Effect2.fail(defect)));
235
- });
236
- };
237
- return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => makeHandler(tool)));
238
- }
239
- };
240
- }));
241
- };
242
- var FunctionToolAnnotation = class extends Context.Tag("@dxos/assisatnt/FunctionToolAnnotation")() {
243
- };
244
- var toolCache = /* @__PURE__ */ new WeakMap();
245
- var projectFunctionToTool = (fn) => {
246
- if (toolCache.has(fn)) {
247
- return toolCache.get(fn);
248
- }
249
- const tool = AiTool.make(makeToolName(fn.name), {
250
- description: fn.description,
251
- parameters: createStructFieldsFromSchema(fn.inputSchema),
252
- // TODO(dmaretskyi): Include output schema.
253
- failure: Schema2.Any
254
- }).annotate(FunctionToolAnnotation, {
255
- functionName: fn.name
256
- });
257
- toolCache.set(fn, tool);
258
- return tool;
259
- };
260
- var makeToolName = (name) => {
261
- const toolName = name.replace(/[^a-zA-Z0-9]/g, "_");
262
- invariant(toolName.match(/^[a-zA-Z_][a-zA-Z0-9-_]*$/), void 0, {
263
- F: __dxlog_file2,
264
- L: 102,
265
- S: void 0,
266
- A: [
267
- "toolName.match(/^[a-zA-Z_][a-zA-Z0-9-_]*$/)",
268
- ""
269
- ]
270
- });
271
- return toolName;
272
- };
273
- var createStructFieldsFromSchema = (schema) => {
274
- return Match.value(schema.ast).pipe(Match.when(Predicate.isTagged("TypeLiteral"), (ast) => {
275
- return Object.fromEntries(ast.propertySignatures.map((prop) => [
276
- prop.name,
277
- Schema2.make(prop.type)
278
- ]));
279
- }), Match.orElseAbsurd);
280
- };
281
-
282
- // src/templates/system.ts
283
- import { Template } from "@dxos/blueprints";
284
-
285
- // raw-loader:./format.tpl?raw
286
- var format_default = "{{! System Core }}\n\n## Input Formats\n\n### Blueprints\n\nYou may be provided with Blueprint definitions that include specific instructions and tools for a given set of tasks.\nBlue print instructions will be enclosed in a <blueprint> tag.\n\n### Context objects\n\nYou may be provided with references to objects that are relevant to the current prompt.\nObjects will be in the form of a <object> tag with a <dxn> element that is the unique key of the object required by many tools.\nThe <typename> element is the type of the object.\n\n<object>\n <dxn>object-id</dxn>\n <typename>example.com/type/TypeName</typename>\n</object>\n\n## Response Formats\n\nIt is very important to respond in the correct format.\nYou response must be valid Markdown.\nIn some cases you should respond with well-formed XML tags along with the content.\n\n{{#if cot}}\nBefore responding, explain your reasoning and include your detailed chain-of-thought enclosed in a <cot> tag.\nThe <cot> tag should be the first thing in your response.\n{{/if}}\n\n## Block references\n\nYou can reply with a block reference for objects if you have the DXN. For example:\n\n<object>\n <dxn>dxn:queue:data:B6INSIBY3CBEF4M5VZRYBCMAHQMPYK5AJ:01K24XMVHSZHS97SG1VTVQDM5Z:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n<object>\n <dxn>dxn:echo:@:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n\n### Suggestions\n\nEach suggestion must in the form of a proposed action enclosed in a <suggest> tag.\nExample: <suggest>Show on a map</suggest>\n\n### Multiple choice questions\n\nYou may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.\nExample: <select><option>Yes</option><option>No</option></select>\n\n## Toolkit\n\nIf the user asks for a list of tools, then just emit a single self-closing <toolkit/> tag instead of listing the tools.\n";
287
-
288
- // raw-loader:./system.tpl?raw
289
- var system_default = "{{! System Prompt }}\n\nYou are a helpful assistant called Kai. \nYour name is Kai and you are powered by different language models.\nYou were created by DXOS and operate in Composer and on the DXOS EDGE network.\nIn your initial greeting state your name and make some suggestions based on the current context objects.\n\nYou are an advanced AI assistant capable of creating and managing artifacts from provided data and tools.\nYour task is to process user commands and questions and decide how best to respond.\nIn general be concise and direct.\n\nFollow these guidelines carefully:\n\nInclude the following steps:\n\n- Analyze the structure and type of the content in the user's message.\n- Determine if you complete the task using the available blueprint definitions?\n- If you can't complete the task using the available blueprint definitions, query the list of available blueprint definitions using the appropriate tool.\n- Identify which blueprint definitions are relevant to the user's request.\n- Evaluate the potential benefits of creating an artifact vs. normal processing for each identified element.\n- Make a final decision on whether to create an artifact and explain your reasoning.\n- Are the required blueprint definitions already available?\n- If not, select which blueprint definition(s) will be the most relevant and require them using the require_artifact_definitions tool.\n- The require'd artifact tools will be available for use after require.\n- If creating an artifact, outline how you will structure it within the response.\n- Decide if the artifact needs to be shown to the user.\n- Call the show tool to show the artifact to the user.\n- Your reasoning must include: whether to use artifacts or not, to create one or query, whether to show the artifact to the user, and how to structure the response.\n\n# Blueprints\n\n- Determine if the interaction involves an artifact. Prefer artifacts for tables, lists, spreadsheets, kanbans, games, images, and other structured data.\n- Determine if the user is explicitly talking about creating a new artifact, or wants to use an existing artifact.\n- If it is ambiguous, query for existing artifacts first and then decide.\n- If you decide to create an artifact, call the associated tool to create the artifact.\n- Artifacts are stored in the database. Tools are used to create and query artifacts.\n- If you are unsure about creating an artifact ask the user for clarification.\n\n- Artifacts are mutable objects that can change over the course of the conversation.\n- Always re-query the artifact using the tool (like query or inspect) to get the latest state of the artifact before answering the user.\n- You must never generate the id of the artifact; only recall the ids that are already in the history.\n- Artifacts are created by requiring the specific artifact using the require_artifact tool and creating it by calling the associated tool.\n\n- You can add suggested actions at the end of your response.\n- Suggested actions should be very concise and start with a verb and be phrased as a command to an agent -- not a question to the user.\n- Suggested actions must be in the form of a user instruction that you can follow.\n- Suggested actions could include actions that create artifacts.\n";
290
-
291
- // src/templates/system.ts
292
- var createSystemPrompt = (options) => Template.process(format_default, options);
293
- var templates = {
294
- // Editable system prompt.
295
- system: Template.make({
296
- source: system_default
297
- })
298
- };
299
-
300
- // src/types.ts
301
- import { Schema as Schema3 } from "effect";
302
- import { DXN, LOCAL_SPACE_TAG } from "@dxos/keys";
303
- import { trim } from "@dxos/util";
304
- var createArtifactElement = (id) => `<artifact id=${id} />`;
305
- var ArtifactId = class extends Schema3.String.annotations({
306
- description: trim`
307
- The ID of the referenced object. Formats accepted:
308
- - DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
309
- - space ID, object ID tuple (spaceID:objectID)
310
- - Only object ID that is assumed to be in the current space (XXXXX)
311
- `,
312
- examples: [
313
- "dxn:echo:@:XXXXX",
314
- "@dxn:echo:@:XXXXX",
315
- "spaceID:objectID",
316
- "XXXXX"
317
- ]
318
- }) {
319
- static toDXN(reference, owningSpaceId) {
320
- if (reference.startsWith("@dxn:")) {
321
- return DXN.parse(reference.slice(1));
322
- } else if (reference.startsWith("dxn:")) {
323
- return DXN.parse(reference);
324
- } else if (/^[A-Z0-9]+:[A-Z0-9]+$/.test(reference)) {
325
- const [spaceId, objectId] = reference.split(":");
326
- return new DXN(DXN.kind.ECHO, [
327
- spaceId === owningSpaceId ? LOCAL_SPACE_TAG : spaceId,
328
- objectId
329
- ]);
330
- } else if (/^[A-Z0-9]+$/.test(reference)) {
331
- return DXN.fromLocalObjectId(reference);
332
- } else {
333
- throw new Error(`Unable to parse object reference: ${reference}`);
334
- }
335
- }
336
- };
337
- export {
338
- AiAssistantError,
339
- AiContextBinder,
340
- AiConversation,
341
- AiSession,
342
- ArtifactDiffResolver,
343
- ArtifactId,
344
- Bindings,
345
- ContextBinding,
346
- GenerationObserver,
347
- VersionPin,
348
- createArtifactElement,
349
- createSystemPrompt,
350
- makeToolExecutionServiceFromFunctions,
351
- makeToolResolverFromFunctions,
352
- templates
353
- };
354
- //# sourceMappingURL=index.mjs.map