@dxos/assistant 0.8.4-main.7ace549 → 0.8.4-main.937b3ca

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 (84) hide show
  1. package/dist/lib/browser/chunk-7CDDG6LL.mjs +578 -0
  2. package/dist/lib/browser/chunk-7CDDG6LL.mjs.map +7 -0
  3. package/dist/lib/browser/extraction/index.mjs +3 -3
  4. package/dist/lib/browser/extraction/index.mjs.map +3 -3
  5. package/dist/lib/browser/index.mjs +239 -678
  6. package/dist/lib/browser/index.mjs.map +4 -4
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/browser/testing/index.mjs +32 -0
  9. package/dist/lib/browser/testing/index.mjs.map +7 -0
  10. package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs +579 -0
  11. package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs.map +7 -0
  12. package/dist/lib/node-esm/extraction/index.mjs +3 -3
  13. package/dist/lib/node-esm/extraction/index.mjs.map +3 -3
  14. package/dist/lib/node-esm/index.mjs +239 -678
  15. package/dist/lib/node-esm/index.mjs.map +4 -4
  16. package/dist/lib/node-esm/meta.json +1 -1
  17. package/dist/lib/node-esm/testing/index.mjs +33 -0
  18. package/dist/lib/node-esm/testing/index.mjs.map +7 -0
  19. package/dist/types/src/conversation/context.d.ts +74 -42
  20. package/dist/types/src/conversation/context.d.ts.map +1 -1
  21. package/dist/types/src/conversation/context.test.d.ts +2 -0
  22. package/dist/types/src/conversation/context.test.d.ts.map +1 -0
  23. package/dist/types/src/conversation/conversation.d.ts +14 -14
  24. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  25. package/dist/types/src/conversation/conversation.test.d.ts +2 -0
  26. package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
  27. package/dist/types/src/errors.d.ts +12 -16
  28. package/dist/types/src/errors.d.ts.map +1 -1
  29. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +179 -178
  30. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  31. package/dist/types/src/extraction/extraction.d.ts +285 -241
  32. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  33. package/dist/types/src/extraction/quotes.d.ts +2 -2
  34. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  35. package/dist/types/src/functions/services.d.ts +3 -3
  36. package/dist/types/src/functions/services.d.ts.map +1 -1
  37. package/dist/types/src/functions.test.d.ts +2 -0
  38. package/dist/types/src/functions.test.d.ts.map +1 -0
  39. package/dist/types/src/session/GenericToolkit.d.ts +1 -0
  40. package/dist/types/src/session/GenericToolkit.d.ts.map +1 -1
  41. package/dist/types/src/session/format.d.ts +3 -3
  42. package/dist/types/src/session/format.d.ts.map +1 -1
  43. package/dist/types/src/session/observer.d.ts +4 -3
  44. package/dist/types/src/session/observer.d.ts.map +1 -1
  45. package/dist/types/src/session/session.d.ts +14 -9
  46. package/dist/types/src/session/session.d.ts.map +1 -1
  47. package/dist/types/src/session/toolkit.d.ts +5 -6
  48. package/dist/types/src/session/toolkit.d.ts.map +1 -1
  49. package/dist/types/src/session/version-pin.d.ts +1 -1
  50. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  51. package/dist/types/src/testing/index.d.ts +2 -0
  52. package/dist/types/src/testing/index.d.ts.map +1 -0
  53. package/dist/types/src/testing/layer.d.ts +21 -0
  54. package/dist/types/src/testing/layer.d.ts.map +1 -0
  55. package/dist/types/src/types/artifact.d.ts +12 -3
  56. package/dist/types/src/types/artifact.d.ts.map +1 -1
  57. package/dist/types/tsconfig.tsbuildinfo +1 -1
  58. package/package.json +38 -29
  59. package/src/conversation/context.test.ts +46 -0
  60. package/src/conversation/context.ts +183 -65
  61. package/src/conversation/conversation.test.ts +65 -0
  62. package/src/conversation/conversation.ts +46 -49
  63. package/src/diff.test.ts +1 -1
  64. package/src/errors.ts +2 -2
  65. package/src/execution-graph.ts +2 -2
  66. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +3 -3
  67. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +1 -1
  68. package/src/extraction/extraction.ts +3 -3
  69. package/src/extraction/quotes.ts +3 -4
  70. package/src/functions/services.ts +28 -10
  71. package/src/functions.conversations.json +1 -0
  72. package/src/functions.test.ts +74 -0
  73. package/src/session/GenericToolkit.ts +1 -0
  74. package/src/session/format.test.ts +101 -38
  75. package/src/session/format.ts +11 -12
  76. package/src/session/observer.ts +3 -1
  77. package/src/session/session.conversations.json +1 -1
  78. package/src/session/session.test.ts +7 -13
  79. package/src/session/session.ts +33 -12
  80. package/src/session/toolkit.ts +13 -22
  81. package/src/session/version-pin.ts +2 -2
  82. package/src/testing/index.ts +5 -0
  83. package/src/testing/layer.ts +78 -0
  84. package/src/types/artifact.ts +18 -6
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/session/artifact-diff.ts", "../../../src/session/observer.ts", "../../../src/errors.ts", "../../../src/session/session.ts", "../../../src/session/format.ts", "../../../src/session/toolkit.ts", "../../../src/session/version-pin.ts", "../../../src/session/GenericToolkit.ts", "../../../src/types/artifact.ts", "../../../src/functions/services.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport * as Context from 'effect/Context';\n\nimport { type ObjectVersion } from '@dxos/echo-db';\nimport { type ObjectId } from '@dxos/keys';\n\n/**\n * Resolves artifact ids to their versions.\n * Used to give the model a sense of the changes to the artifacts made by users during the conversation.\n * The artifacts versions are pinned in the history, and whenever the artifact changes in-between assistant's steps,\n * a diff is inserted into the conversation.\n *\n * Can be optionally provided to the session run call.\n */\n// TODO(dmaretskyi): Convert to Context.Reference\nexport class ArtifactDiffResolver extends Context.Tag('@dxos/assistant/ArtifactDiffResolver')<\n ArtifactDiffResolver,\n ArtifactDiffResolver.Service\n>() {}\n\nexport namespace ArtifactDiffResolver {\n export type Service = {\n resolve: (\n artifacts: {\n id: ObjectId;\n lastVersion: ObjectVersion;\n }[],\n ) => Promise<\n Map<\n ObjectId,\n {\n version: ObjectVersion;\n diff?: string;\n }\n >\n >;\n };\n}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport type * as Tool from '@effect/ai/Tool';\nimport * as Effect from 'effect/Effect';\nimport * as Function from 'effect/Function';\n\nimport { type AiParser, type ConsolePrinter } from '@dxos/ai';\nimport { type Message } from '@dxos/types';\n\n/**\n * Live observer of the generation process.\n */\nexport interface GenerationObserver<Tools extends Record<string, Tool.Any> = any>\n extends AiParser.ParseResponseCallbacks<Tools> {\n /**\n * Complete messages fired during the session, both from the model and from the user.\n * This message will contain all message blocks emitted through the `onBlock` callback.\n */\n onMessage: (message: Message.Message) => Effect.Effect<void>;\n}\n\nexport const GenerationObserver = Object.freeze({\n make: ({\n onBegin = Function.constant(Effect.void),\n onPart = Function.constant(Effect.void),\n onBlock = Function.constant(Effect.void),\n onEnd = Function.constant(Effect.void),\n onMessage = Function.constant(Effect.void),\n }: Partial<GenerationObserver> = {}): GenerationObserver => ({\n onBegin,\n onPart,\n onBlock,\n onEnd,\n onMessage,\n }),\n\n noop: () => GenerationObserver.make(),\n\n /**\n * Debug printer to be used in unit-tests and browser devtools.\n */\n fromPrinter: (printer: ConsolePrinter) =>\n GenerationObserver.make({\n onBlock: (block) =>\n Effect.sync(() => {\n if (block.pending) {\n return; // Only prints full blocks (better for unit-tests and browser devtools).\n }\n printer.printContentBlock(block);\n }),\n onMessage: (message) =>\n Effect.sync(() => {\n if (message.sender.role === 'assistant') {\n return; // Skip assistant messages since they are printed in the `onBlock` callback.\n }\n printer.printMessage(message);\n }),\n }),\n});\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { BaseError } from '@dxos/errors';\n\n/**\n * Generic error from AI model.\n */\nexport class AiModelError extends BaseError.extend('AiModelError', 'AI model error') {}\n\n/**\n * Generic error for AI agent execution.\n */\nexport class AiAssistantError extends BaseError.extend('AiAssistantError', 'AI assistant error') {}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport type * as AiError from '@effect/ai/AiError';\nimport * as LanguageModel from '@effect/ai/LanguageModel';\nimport type * as Tool from '@effect/ai/Tool';\nimport type * as Toolkit from '@effect/ai/Toolkit';\nimport * as Chunk from 'effect/Chunk';\nimport * as Effect from 'effect/Effect';\nimport type * as Schema from 'effect/Schema';\nimport * as Stream from 'effect/Stream';\n\nimport {\n AiParser,\n AiPreprocessor,\n type AiToolNotFoundError,\n type PromptPreprocessingError,\n type ToolExecutionService,\n type ToolResolverService,\n callTool,\n getToolCalls,\n} from '@dxos/ai';\nimport { type Blueprint } from '@dxos/blueprints';\nimport { todo } from '@dxos/debug';\nimport { Obj } from '@dxos/echo';\nimport { type FunctionInvocationService, TracingService } from '@dxos/functions';\nimport { log } from '@dxos/log';\nimport { Message } from '@dxos/types';\n\nimport { type AiAssistantError } from '../errors';\n\nimport { formatSystemPrompt, formatUserPrompt } from './format';\nimport { GenerationObserver } from './observer';\n\nexport type AiSessionRunError = AiError.AiError | PromptPreprocessingError | AiToolNotFoundError | AiAssistantError;\n\nexport type AiSessionRunRequirements =\n | LanguageModel.LanguageModel\n | ToolExecutionService\n | ToolResolverService\n | TracingService\n | FunctionInvocationService;\n\nexport type AiSessionOptions = {};\n\nexport type AiSessionRunProps<Tools extends Record<string, Tool.Any>> = {\n prompt: string;\n // TODO(wittjosiah): Rename to systemPrompt.\n system?: string;\n history?: Message.Message[];\n objects?: Obj.Unknown[];\n blueprints?: readonly Blueprint.Blueprint[];\n toolkit?: Toolkit.WithHandler<Tools>;\n observer?: GenerationObserver<Tools>;\n};\n\n/**\n * Contains message history, tools, current context.\n * Current context means the state of the app, time of day, and other contextual information.\n * It makes requests to the model, its a state machine.\n * It keeps track of the current goal.\n * It manages the context window.\n * Tracks the success criteria of reaching the goal, exposing metrics (stretch).\n * Could be run locally in the app or remotely.\n * Could be personal or shared.\n */\nexport class AiSession {\n /** Prevents concurrent execution of session. */\n private readonly _semaphore = Effect.runSync(Effect.makeSemaphore(1));\n\n /** Message history from queue. */\n // TODO(burdon): Evolve into supporting a git-like graph of messages.\n private _history: Message.Message[] = [];\n\n /** Pending messages for this session (incl. the current prompt). */\n private _pending: Message.Message[] = [];\n\n private _started = 0;\n private _ended = 0;\n private _toolCalls = 0;\n\n constructor(private readonly _options: AiSessionOptions = {}) {}\n\n get duration(): number {\n return this._ended - this._started;\n }\n\n get toolCalls(): number {\n return this._toolCalls;\n }\n\n run = <Tools extends Record<string, Tool.Any>>({\n prompt,\n system: systemTemplate,\n history = [],\n objects = [],\n blueprints = [],\n toolkit,\n observer = GenerationObserver.noop(),\n }: AiSessionRunProps<Tools>): Effect.Effect<Message.Message[], AiSessionRunError, AiSessionRunRequirements> =>\n Effect.gen(this, function* () {\n this._started = Date.now();\n this._history = [...history];\n this._pending = [];\n const pending = this._pending;\n\n const submitMessage = Effect.fnUntraced(function* (message: Message.Message) {\n pending.push(message);\n yield* observer.onMessage(message);\n yield* TracingService.emitConverationMessage(message);\n return message;\n });\n\n // Submit the prompt.\n // TODO(burdon): Remove if cancelled?\n const promptMessage = yield* submitMessage(yield* formatUserPrompt({ prompt, history }));\n\n // Generate system and prompt messages.\n const system = yield* formatSystemPrompt({ system: systemTemplate, blueprints, objects }).pipe(Effect.orDie);\n\n // log('system', { prompt: system });\n\n // Tool call loop.\n do {\n log('request', {\n prompt: promptMessage,\n system: { snippet: createSnippet(system), length: system.length },\n pending: this._pending.length,\n history: this._history.length,\n objects: objects?.length ?? 0,\n });\n\n const prompt = yield* AiPreprocessor.preprocessPrompt([...this._history, ...this._pending], { system });\n\n // Execute the stream request.\n // logDump('prompt', Prompt.Prompt.pipe(Schema.encodeSync)(prompt));\n const blocks = yield* LanguageModel.streamText({\n prompt,\n toolkit,\n disableToolCallResolution: true,\n }).pipe(\n // TOOD(dmaretskyi): Error mapping.\n AiParser.parseResponse({\n onBegin: () => observer.onBegin(),\n onBlock: (block) => observer.onBlock(block),\n onPart: (part) => observer.onPart(part),\n onEnd: (summary) => observer.onEnd(summary),\n }),\n Stream.runCollect,\n Effect.map(Chunk.toArray),\n );\n\n // log('blocks', { blocks });\n\n // Create the response message.\n const response = yield* submitMessage(\n Obj.make(Message.Message, {\n created: new Date().toISOString(),\n sender: { role: 'assistant' },\n blocks,\n }),\n );\n\n // Parse the response for tool calls.\n const toolCalls = getToolCalls(response);\n if (toolCalls.length === 0) {\n break;\n } else if (!toolkit) {\n throw new Error('No toolkit provided'); // TODO(burdon): Throw user error?\n }\n\n // Execute the tool calls.\n // TODO(burdon): Retry errors? Write result when each completes individually?\n const toolResults = yield* Effect.forEach(toolCalls, (toolCall) => {\n return callTool(toolkit, toolCall).pipe(\n Effect.provide(\n TracingService.layerSubframe((context) => ({\n ...context,\n parentMessage: response.id,\n toolCallId: toolCall.toolCallId,\n })),\n ),\n );\n });\n\n // Add to queue and continue loop.\n // TODO(wittjosiah): Sometimes tool error results are added to the queue before the tool agent statuses.\n // This results in a broken execution graph.\n yield* submitMessage(\n Obj.make(Message.Message, {\n created: new Date().toISOString(),\n sender: { role: 'tool' },\n blocks: toolResults,\n }),\n );\n\n this._toolCalls++;\n } while (true);\n\n this._ended = Date.now();\n log('done', { pending: this._pending.length, duration: this.duration, tools: this._toolCalls });\n return this._pending;\n }).pipe(this._semaphore.withPermits(1), Effect.withSpan('AiSession.run'));\n\n /**\n * @deprecated\n */\n // TODO(burdon): Implement or remove.\n async runStructured<S extends Schema.Schema.AnyNoContext>(\n _schema: S,\n _options: AiSessionRunProps<any>,\n ): Promise<Schema.Schema.Type<S>> {\n return todo();\n // const parser = structuredOutputParser(schema);\n // const result = await this.run({\n // ...options,\n // executableTools: [...(options.executableTools ?? []), parser.tool],\n // });\n // return parser.getResult(result);\n }\n}\n\nconst createSnippet = (text: string, len = 32) =>\n text.length <= len * 2 ? text : [text.slice(0, len), '...', text.slice(-len)].join('');\n\n// TODO(dmaretskyi): Extract as a general util.\nconst logDump = (message: string, data: unknown) => {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const { writeFileSync } = require('node:fs');\n const path = `/tmp/log-data-${Date.now()}.json`;\n writeFileSync(path, JSON.stringify(data, null, 2));\n log.info(message, { path });\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport * as Effect from 'effect/Effect';\nimport * as Function from 'effect/Function';\nimport * as Option from 'effect/Option';\n\nimport { Template } from '@dxos/blueprints';\nimport { Obj } from '@dxos/echo';\nimport { ObjectVersion } from '@dxos/echo-db';\nimport { type ObjectId } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { type ContentBlock, Message } from '@dxos/types';\nimport { trim } from '@dxos/util';\n\nimport { AiAssistantError } from '../errors';\n\nimport { ArtifactDiffResolver } from './artifact-diff';\nimport { type AiSessionRunError, type AiSessionRunProps } from './session';\n\n/**\n * Formats the system prompt.\n */\n// TODO(burdon): Move to AiPreprocessor.\nexport const formatSystemPrompt = ({\n system,\n blueprints = [],\n objects = [],\n}: Pick<AiSessionRunProps<any>, 'system' | 'blueprints' | 'objects'>) =>\n Effect.gen(function* () {\n const blueprintDefs = yield* Function.pipe(\n blueprints,\n Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)),\n Effect.flatMap(\n Effect.forEach((template) =>\n Effect.gen(function* () {\n return trim`\n <blueprint>\n ${yield* Template.processTemplate(template)}\n </blueprint>\n `;\n }),\n ),\n ),\n Effect.map((blueprints) =>\n blueprints.length > 0 ? ['## Blueprints Definitions', ...blueprints].join('\\n\\n') : undefined,\n ),\n );\n\n const objectDefs = yield* Function.pipe(\n objects,\n Effect.forEach((object) =>\n Effect.succeed(trim`\n <object>\n <dxn>${Obj.getDXN(object)}</dxn>\n <typename>${Obj.getTypename(object)}</typename>\n </object>\n `),\n ),\n Effect.map((objects) => (objects.length > 0 ? ['## Context Objects', ...objects].join('\\n\\n') : undefined)),\n );\n\n return yield* Function.pipe(\n Effect.succeed([system, blueprintDefs, objectDefs].filter((def): def is string => def !== undefined)),\n Effect.map((parts) => parts.join('\\n\\n')),\n );\n });\n\n/**\n * Formats the user prompt.\n */\n// TODO(burdon): Move to AiPreprocessor.\n// TODO(burdon): Convert util below to `Effect.fn` (to preserve stack info)\nexport const formatUserPrompt = ({\n prompt,\n history = [],\n}: Pick<AiSessionRunProps<any>, 'prompt' | 'history'>): Effect.Effect<Message.Message, AiSessionRunError> =>\n Effect.gen(function* () {\n const blocks: ContentBlock.Any[] = [];\n\n // TODO(dmaretskyi): Evaluate other approaches as `serviceOption` isn't represented in the type system.\n const artifactDiffResolver = yield* Effect.serviceOption(ArtifactDiffResolver);\n if (Option.isSome(artifactDiffResolver)) {\n const versions = gatherObjectVersions(history);\n const artifactDiff = yield* Effect.tryPromise({\n try: () =>\n artifactDiffResolver.value.resolve(\n [...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),\n ),\n catch: AiAssistantError.wrap({ message: 'Artifact diff resolution error' }),\n });\n\n log('version', { artifactDiff, versions });\n for (const [id, { version }] of [...artifactDiff.entries()]) {\n if (ObjectVersion.equals(version, versions.get(id)!)) {\n artifactDiff.delete(id);\n continue;\n }\n\n blocks.push({ _tag: 'anchor', objectId: id, version });\n }\n\n if (artifactDiff.size > 0) {\n blocks.push(createArtifactUpdateBlock(artifactDiff));\n }\n }\n\n return Obj.make(Message.Message, {\n created: new Date().toISOString(),\n sender: { role: 'user' },\n blocks: [...blocks, { _tag: 'text', text: prompt }],\n });\n });\n\nconst gatherObjectVersions = (messages: Message.Message[]): Map<ObjectId, ObjectVersion> => {\n const artifactIds = new Map<ObjectId, ObjectVersion>();\n for (const message of messages) {\n for (const block of message.blocks) {\n if (block._tag === 'anchor') {\n artifactIds.set(block.objectId, block.version as ObjectVersion);\n }\n }\n }\n\n return artifactIds;\n};\n\nconst createArtifactUpdateBlock = (\n artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,\n): ContentBlock.Any => {\n return {\n _tag: 'text',\n // TODO(dmaretskyi): Does this need to be a special content-block?\n disposition: 'artifact-update',\n text: trim`\n The following artifacts have been updated since the last message:\n ${[...artifactDiff.entries()]\n .map(([id, { diff }]) => `<changed-artifact id=\"${id}\">${diff ? `\\n${diff}` : ''}</changed-artifact>`)\n .join('\\n')}\n `,\n };\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport type * as Tool from '@effect/ai/Tool';\nimport * as Toolkit from '@effect/ai/Toolkit';\nimport * as Effect from 'effect/Effect';\n\nimport { type AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';\nimport { type Blueprint } from '@dxos/blueprints';\nimport { isTruthy } from '@dxos/util';\n\nexport type CreateToolkitProps = {\n toolkit?: Toolkit.Any;\n blueprints?: readonly Blueprint.Blueprint[];\n};\n\n/**\n * Build a combined toolkit from the blueprint tools and the provided toolkit.\n */\nexport const createToolkit = ({\n toolkit: toolkitProp,\n blueprints = [],\n}: CreateToolkitProps): Effect.Effect<\n Toolkit.WithHandler<any>,\n AiToolNotFoundError,\n ToolResolverService | ToolExecutionService | Tool.HandlersFor<any>\n> =>\n Effect.gen(function* () {\n const blueprintToolkit = yield* ToolResolverService.resolveToolkit(blueprints.flatMap(({ tools }) => tools));\n const blueprintToolHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));\n\n const toolkit = Toolkit.merge(...[toolkitProp, blueprintToolkit].filter(isTruthy));\n return yield* toolkit.pipe(Effect.provide(blueprintToolHandler)) as any as Effect.Effect<\n Toolkit.WithHandler<any>,\n never,\n Tool.HandlersFor<any>\n >;\n });\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport * as Schema from 'effect/Schema';\n\nimport { type Obj } from '@dxos/echo';\nimport { type ObjectVersion, getVersion } from '@dxos/echo-db';\nimport { ObjectId } from '@dxos/keys';\nimport { type ContentBlock } from '@dxos/types';\n\n// TODO(dmaretskyi): Extract.\nconst ObjectVersionSchema = Schema.Unknown as Schema.Schema<ObjectVersion>;\n\nconst VersionPinSchema = Schema.Struct({\n // TODO(dmaretskyi): Use Ref when those support encoding.\n objectId: ObjectId,\n // TODO(dmaretskyi): Could be opaque.\n version: ObjectVersionSchema,\n});\n\n/**\n * Used to pin a specific version of an object during the course of a conversation.\n * This allows us to detect when the object changes during the course of the conversation.\n */\nexport interface VersionPin extends Schema.Schema.Type<typeof VersionPinSchema> {}\n\nexport const VersionPin: typeof VersionPinSchema & {\n DISPOSITION: 'version-pin';\n fromObject: (object: Obj.Unknown) => VersionPin;\n createBlock: (pin: VersionPin) => ContentBlock.Any;\n} = class extends VersionPinSchema {\n static readonly DISPOSITION = 'version-pin';\n static fromObject(object: Obj.Unknown): VersionPin {\n return VersionPin.make({\n objectId: object.id,\n version: getVersion(object),\n });\n }\n\n static createBlock(pin: VersionPin): ContentBlock.Any {\n return {\n _tag: 'json',\n disposition: VersionPin.DISPOSITION,\n data: JSON.stringify(pin),\n };\n }\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport type * as Tool from '@effect/ai/Tool';\nimport * as Toolkit from '@effect/ai/Toolkit';\nimport * as Effect from 'effect/Effect';\nimport * as Layer from 'effect/Layer';\nimport * as Pipeable from 'effect/Pipeable';\nimport type * as Schema from 'effect/Schema';\nimport type * as SchemaAST from 'effect/SchemaAST';\n\n/**\n * Unique identifier for generic toolkit instances.\n */\nexport const TypeId = '~@dxos/assistant/GenericToolkit';\n\n/**\n * Type-level representation of the generic toolkit identifier.\n */\nexport type TypeId = typeof TypeId;\n\n/**\n * Type-safe way to define toolkits where we don't know specific types of tools,\n * and we want to bundle definition and handlers together.\n *\n * Usefull for plugin-based systems, where tools are dynamically added.\n *\n * @param TR requirements type to the tool invocation.\n * @param E failure type to the handlers layer.\n * @param R requirements type to the handlers layer.\n */\nexport interface GenericToolkit<TR = never, E = never, R = never> extends Pipeable.Pipeable {\n readonly [TypeId]: TypeId;\n\n /**\n * Toolkit definition.\n */\n readonly toolkit: Toolkit.Toolkit<GenericTools<TR>>;\n\n /**\n * Handlers layer.\n */\n readonly layer: Layer.Layer<unknown, E, R>;\n\n /**\n * Handlers effect.\n */\n readonly handlers: Effect.Effect<Toolkit.WithHandler<GenericTools<TR>>, E, R>;\n}\n\n/**\n * Generic constraint.\n *\n * NOTE: Only use in place of `T extends PortableToolkit.Any`. Not suitable for standalone use.\n */\nexport interface Any {\n readonly [TypeId]: TypeId;\n readonly toolkit: Toolkit.Toolkit<any>;\n readonly layer: Layer.Layer<unknown, any, any>;\n}\n\nexport type InvocationRequirements<T extends Any> = T extends GenericToolkit<infer TR, infer _E, infer _R> ? TR : never;\nexport type Failure<T extends Any> = T extends GenericToolkit<infer _TR, infer E, infer _R> ? E : never;\nexport type Requirements<T extends Any> = T extends GenericToolkit<infer _TR, infer _E, infer R> ? R : never;\n\n/**\n * Creates a portable toolkit from toolkit definition and handlers layer.\n */\nexport const make = <Tools extends Record<string, Tool.Any>, E, R>(\n toolkit: Toolkit.Toolkit<Tools>,\n layer: Layer.Layer<Tool.HandlersFor<Tools>, E, R>,\n): GenericToolkit<E, R> => {\n return {\n [TypeId]: TypeId,\n toolkit: toolkit as any,\n layer: layer as any,\n handlers: toolkit.pipe(Effect.provide(layer)) as any,\n pipe() {\n // eslint-disable-next-line prefer-rest-params\n return Pipeable.pipeArguments(this, arguments);\n },\n };\n};\n\n/**\n * Merges multiple portable toolkits into a single portable toolkit.\n */\nexport const merge = <const Toolkits extends ReadonlyArray<Any>>(\n /**\n * The toolkits to merge together.\n */\n ...toolkits: Toolkits\n): GenericToolkit<\n InvocationRequirements<Toolkits[number]>,\n Failure<Toolkits[number]>,\n Requirements<Toolkits[number]>\n> => {\n return make(\n Toolkit.merge(...toolkits.map((t) => t.toolkit)),\n Layer.mergeAll(...(toolkits.map((t) => t.layer) as any)),\n ) as any;\n};\n\n/**\n * Generic tools type where we cannot statically enumerate individual tools.\n */\nexport type GenericTools<R = never> = Record<\n string,\n Tool.Tool<\n string,\n {\n readonly parameters: AnyStructSchemaNoContext;\n readonly success: Schema.Schema.AnyNoContext;\n readonly failure: typeof Schema.Never;\n readonly failureMode: Tool.FailureMode;\n },\n R\n >\n>;\n\nexport interface AnyStructSchemaNoContext extends Pipeable.Pipeable {\n readonly [Schema.TypeId]: any;\n readonly make: any;\n readonly Type: any;\n readonly Encoded: any;\n readonly Context: never;\n readonly ast: SchemaAST.AST;\n readonly fields: Schema.Struct.Fields;\n readonly annotations: any;\n}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport type * as Effect from 'effect/Effect';\nimport * as Schema from 'effect/Schema';\n\nimport { type Err, Type } from '@dxos/echo';\nimport { Database } from '@dxos/echo';\nimport { EncodedReference } from '@dxos/echo-protocol';\nimport { DXN, LOCAL_SPACE_TAG, type ObjectId, type SpaceId } from '@dxos/keys';\nimport { trim } from '@dxos/util';\n\n/**\n * @deprecated\n */\nexport const createArtifactElement = (id: ObjectId) => `<artifact id=${id} />`;\n\n/**\n * A model-friendly way to reference an object.\n * Supports vairous formats that will be normalized to a DXN.\n *\n * @deprecated Use `RefFromLLM` instead.\n */\n// TODO(burdon): Rename RefFromLLM? -- yes -dm\nexport const ArtifactId: Schema.Schema<string> & {\n toDXN: (reference: ArtifactId, owningSpaceId?: SpaceId) => DXN;\n resolve: <S extends Type.Entity.Any>(\n schema: S,\n ref: ArtifactId,\n ) => Effect.Effect<Schema.Schema.Type<S>, Err.ObjectNotFoundError, Database.Service>;\n} = class extends Schema.String.annotations({\n // TODO(dmaretskyi): This section gets overriden.\n description: trim`\n The ID of the referenced object. Formats accepted:\n - DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.\n - space ID, object ID tuple (spaceID:objectID)\n - Only object ID that is assumed to be in the current space (XXXXX)\n `,\n examples: ['dxn:echo:@:XXXXX', '@dxn:echo:@:XXXXX', 'spaceID:objectID', 'XXXXX'],\n}) {\n static toDXN(reference: ArtifactId, owningSpaceId?: SpaceId): DXN {\n // Allow @dxn: prefix for compatibility with in-text references.\n if (reference.startsWith('@dxn:')) {\n return DXN.parse(reference.slice(1));\n } else if (reference.startsWith('dxn:')) {\n return DXN.parse(reference);\n } else if (/^[A-Z0-9]+:[A-Z0-9]+$/.test(reference)) {\n const [spaceId, objectId] = reference.split(':');\n // This is a workaround because the current Filter API doesn't work with fully qualified Echo DXNs.\n // We check if the space ID is the same as the owning space and then use LOCAL_SPACE_TAG for local references.\n // TODO(dmaretskyi): Fix this in the Echo and Filter API to properly handle fully qualified DXNs.\n return new DXN(DXN.kind.ECHO, [spaceId === owningSpaceId ? LOCAL_SPACE_TAG : spaceId, objectId]);\n } else if (/^[A-Z0-9]+$/.test(reference)) {\n return DXN.fromLocalObjectId(reference);\n } else {\n throw new Error(`Unable to parse object reference: ${reference}`);\n }\n }\n\n /**\n * Resolves an artifact ID to an object.\n */\n static resolve<S extends Type.Entity.Any>(\n schema: S,\n ref: ArtifactId,\n ): Effect.Effect<Schema.Schema.Type<S>, Err.ObjectNotFoundError, Database.Service> {\n const dxn = ArtifactId.toDXN(ref);\n return Database.Service.resolve(dxn, schema);\n }\n};\n\nexport type ArtifactId = Schema.Schema.Type<typeof ArtifactId>;\n\n/**\n * Schema that decodes ECHO reference object from an LLM-friendly input.\n */\nexport const RefFromLLM = Schema.transform(ArtifactId, Type.Ref(Type.Obj), {\n decode: (fromA, fromI) => EncodedReference.fromDXN(ArtifactId.toDXN(fromA)),\n encode: (toI, toA) => EncodedReference.toDXN(toI).toString(),\n strict: false,\n});\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport * as Tool from '@effect/ai/Tool';\nimport type * as Toolkit from '@effect/ai/Toolkit';\nimport * as Context from 'effect/Context';\nimport * as Effect from 'effect/Effect';\nimport * as Layer from 'effect/Layer';\nimport * as Record from 'effect/Record';\nimport * as Schema from 'effect/Schema';\nimport type * as SchemaAST from 'effect/SchemaAST';\n\nimport { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';\nimport { todo } from '@dxos/debug';\nimport { Query, Type } from '@dxos/echo';\nimport { Database } from '@dxos/echo';\nimport { Function, FunctionDefinition, FunctionInvocationService } from '@dxos/functions';\nimport { invariant } from '@dxos/invariant';\n\nimport { RefFromLLM } from '../types';\n\n/**\n * Constructs a `ToolResolverService` whose `resolve(id)` looks up tools in the following order:\n * 1. Toolkit: return an existing tool from the provided `toolkit` if one is present under `id`.\n * 2. Functions in DB: query `FunctionType` by `key=id`; if found, deserialize and project to a tool\n * (propagating any `deployedFunctionId` discovered in the object's metadata).\n * 3. Functions passed in: fall back to a matching `FunctionDefinition` from the `functions` array.\n *\n * If none of the above yield a match, the effect fails with `AiToolNotFoundError`.\n *\n * Requires `Database.Service` in the environment.\n */\nexport const makeToolResolverFromFunctions = (\n functions: FunctionDefinition.Any[],\n toolkit: Toolkit.Toolkit<any>,\n): Layer.Layer<ToolResolverService, never, Database.Service> => {\n return Layer.effect(\n ToolResolverService,\n Effect.gen(function* () {\n const dbService = yield* Database.Service;\n return {\n resolve: (id): Effect.Effect<Tool.Any, AiToolNotFoundError> =>\n Effect.gen(function* () {\n // TODO(dmaretskyi): Use FunctionInvocationService.resolveFunction().\n\n const tool = toolkit.tools[id];\n if (tool) {\n return tool;\n }\n\n const [dbFunction] = yield* Database.Service.runQuery(Query.type(Function.Function, { key: id }));\n\n const functionDef = dbFunction\n ? FunctionDefinition.deserialize(dbFunction)\n : functions.find((fn) => fn.key === id);\n\n if (!functionDef) {\n return yield* Effect.fail(new AiToolNotFoundError(id));\n }\n\n return projectFunctionToTool(functionDef);\n }).pipe(Effect.provideService(Database.Service, dbService)),\n } satisfies Context.Tag.Service<ToolResolverService>;\n }),\n );\n};\n\nexport const makeToolExecutionServiceFromFunctions = (\n toolkit: Toolkit.Toolkit<any>,\n handlersLayer: Layer.Layer<Tool.Handler<any>, never, never>,\n): Layer.Layer<ToolExecutionService, never, FunctionInvocationService> => {\n return Layer.effect(\n ToolExecutionService,\n Effect.gen(function* () {\n const toolkitHandler = yield* toolkit.pipe(Effect.provide(handlersLayer));\n invariant(isHandlerLike(toolkitHandler));\n const functionInvocationService = yield* FunctionInvocationService;\n\n return {\n handlersFor: (toolkit) => {\n const makeHandler = (tool: Tool.Any): ((params: unknown) => Effect.Effect<unknown, any, any>) => {\n return Effect.fn(`toolFunctionHandler ${tool.name}`)(function* (input: any) {\n if (toolkitHandler.tools[tool.name]) {\n if (Tool.isProviderDefined(tool)) {\n throw new Error('Attempted to call a provider-defined tool');\n }\n\n // TODO(wittjosiah): Everything is `never` here.\n const { result } = yield* (toolkitHandler.handle as any)(tool.name, input);\n return result;\n }\n\n const { definition: functionDef } = Context.get(FunctionToolAnnotation)(tool.annotations as any);\n\n return yield* functionInvocationService\n .invokeFunction(functionDef, input as any)\n .pipe(Effect.catchAllDefect((defect) => Effect.fail(defect)));\n });\n };\n\n return toolkit.of(\n Record.map(toolkit.tools, (tool, _name) => (Tool.isUserDefined(tool) ? makeHandler(tool) : null)) as any,\n ) as any;\n },\n };\n }),\n );\n};\n\nclass FunctionToolAnnotation extends Context.Tag('@dxos/assistant/FunctionToolAnnotation')<\n FunctionToolAnnotation,\n { definition: FunctionDefinition.Any }\n>() {}\n\nconst toolCache = new WeakMap<FunctionDefinition.Any, Tool.Any>();\n\n/**\n * Projects a `FunctionDefinition` into an `AiTool`.\n *\n * @param fn The function definition to project into a tool.\n * @param meta Optional projection metadata.\n * @param meta.deployedFunctionId Backend deployment ID used for remote invocation when present.\n * This is the EDGE service's function deployment identifier (not the ECHO object ID/DXN and not `FunctionDefinition.key`).\n */\nconst projectFunctionToTool = (fn: FunctionDefinition.Any): Tool.Any => {\n if (toolCache.has(fn)) {\n return toolCache.get(fn)!;\n }\n\n // TODO(burdon): Use and map function.key?\n // TODO(dmaretskyi): Use function key instead.\n const tool = Tool.make(makeToolName(fn.name), {\n description: fn.description,\n parameters: createStructFieldsFromSchema(fn.inputSchema),\n // TODO(dmaretskyi): Include output schema.\n failure: Schema.Any, // TODO(dmaretskyi): Better type for the failure?\n }).annotate(FunctionToolAnnotation, { definition: fn });\n toolCache.set(fn, tool);\n return tool;\n};\n\n/**\n * @returns Tool name produced from function name by escaping invalid characters.\n */\nconst makeToolName = (name: string) => {\n const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, '-');\n invariant(toolName.match(/^[a-z_][a-z0-9-_]*$/));\n return toolName;\n};\n\n// TODO(dmaretskyi): Factor out.\nconst createStructFieldsFromSchema = (schema: Schema.Schema<any, any>): Record<string, Schema.Schema<any, any>> => {\n switch (schema.ast._tag) {\n case 'TypeLiteral':\n return Object.fromEntries(\n schema.ast.propertySignatures.map((prop) => [prop.name, Schema.make(mapSchemaTypeForLLM(prop.type))]),\n );\n case 'VoidKeyword':\n return {};\n default:\n return todo(`Unsupported schema AST: ${schema.ast._tag}`);\n }\n};\n\n/**\n * Picks an LLM-friendly schema type for the given schema AST.\n * The picked schema type decodes to the original schema type.\n */\nconst mapSchemaTypeForLLM = (ast: SchemaAST.AST): SchemaAST.AST => {\n if (Type.Ref.isRefSchemaAST(ast)) {\n return RefFromLLM.ast;\n }\n\n return ast;\n};\n\nconst isHandlerLike = (value: unknown): value is Toolkit.WithHandler<Record<string, Tool.Any>> => {\n return typeof (value as any).tools === 'object' && typeof (value as any).handle === 'function';\n};\n"],
5
+ "mappings": ";;;;;;AAIA,YAAYA,aAAa;AAclB,IAAMC,uBAAN,cAA2CC,YAAI,sCAAA,EAAA,EAAA;AAGjD;;;AChBL,YAAYC,YAAY;AACxB,YAAYC,cAAc;AAiBnB,IAAMC,qBAAqBC,OAAOC,OAAO;EAC9CC,MAAM,CAAC,EACLC,UAAmBC,kBAAgBC,WAAI,GACvCC,SAAkBF,kBAAgBC,WAAI,GACtCE,UAAmBH,kBAAgBC,WAAI,GACvCG,QAAiBJ,kBAAgBC,WAAI,GACrCI,YAAqBL,kBAAgBC,WAAI,EAAC,IACX,CAAC,OAA2B;IAC3DF;IACAG;IACAC;IACAC;IACAC;EACF;EAEAC,MAAM,MAAMX,mBAAmBG,KAAI;;;;EAKnCS,aAAa,CAACC,YACZb,mBAAmBG,KAAK;IACtBK,SAAS,CAACM,UACDC,YAAK,MAAA;AACV,UAAID,MAAME,SAAS;AACjB;MACF;AACAH,cAAQI,kBAAkBH,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C7B,MAAImC,UAAUE,IAAIrC,GAAAA,GAAK;AACrB,WAAOmC,UAAUX,IAAIxB,GAAAA;EACvB;AAIA,QAAMZ,OAAYkD,UAAKC,aAAavC,IAAGiB,IAAI,GAAG;IAC5CuB,aAAaxC,IAAGwC;IAChBC,YAAYC,6BAA6B1C,IAAG2C,WAAW;;IAEvDC,SAAgBC;EAClB,CAAA,EAAGC,SAASrB,wBAAwB;IAAEF,YAAYvB;EAAG,CAAA;AACrDmC,YAAUY,IAAI/C,KAAIZ,IAAAA;AAClB,SAAOA;AACT;AAKA,IAAMmD,eAAe,CAACtB,SAAAA;AACpB,QAAM+B,WAAW/B,KAAKgC,YAAW,EAAGC,QAAQ,iBAAiB,GAAA;AAC7DvC,YAAUqC,SAASG,MAAM,qBAAA,GAAA,QAAA;;;;;;;;;AACzB,SAAOH;AACT;AAGA,IAAMN,+BAA+B,CAACU,WAAAA;AACpC,UAAQA,OAAOC,IAAIC,MAAI;IACrB,KAAK;AACH,aAAOC,OAAOC,YACZJ,OAAOC,IAAII,mBAAmB1B,IAAI,CAAC2B,SAAS;QAACA,KAAKzC;QAAaqB,aAAKqB,oBAAoBD,KAAKjE,IAAI,CAAA;OAAG,CAAA;IAExG,KAAK;AACH,aAAO,CAAC;IACV;AACE,aAAOmE,MAAK,2BAA2BR,OAAOC,IAAIC,IAAI,EAAE;EAC5D;AACF;AAMA,IAAMK,sBAAsB,CAACN,QAAAA;AAC3B,MAAIQ,MAAKC,IAAIC,eAAeV,GAAAA,GAAM;AAChC,WAAOW,WAAWX;EACpB;AAEA,SAAOA;AACT;AAEA,IAAMzC,gBAAgB,CAACqD,UAAAA;AACrB,SAAO,OAAQA,MAAc5E,UAAU,YAAY,OAAQ4E,MAAc3C,WAAW;AACtF;",
6
+ "names": ["Context", "ArtifactDiffResolver", "Tag", "Effect", "Function", "GenerationObserver", "Object", "freeze", "make", "onBegin", "constant", "void", "onPart", "onBlock", "onEnd", "onMessage", "noop", "fromPrinter", "printer", "block", "sync", "pending", "printContentBlock", "message", "sender", "role", "printMessage", "BaseError", "AiModelError", "BaseError", "extend", "AiAssistantError", "LanguageModel", "Chunk", "Effect", "Stream", "AiParser", "AiPreprocessor", "callTool", "getToolCalls", "todo", "Obj", "TracingService", "log", "Message", "Effect", "Function", "Option", "Template", "Obj", "ObjectVersion", "log", "Message", "trim", "formatSystemPrompt", "system", "blueprints", "objects", "gen", "blueprintDefs", "pipe", "forEach", "blueprint", "succeed", "instructions", "flatMap", "template", "trim", "Template", "processTemplate", "map", "length", "join", "undefined", "objectDefs", "object", "Obj", "getDXN", "getTypename", "filter", "def", "parts", "formatUserPrompt", "prompt", "history", "blocks", "artifactDiffResolver", "serviceOption", "ArtifactDiffResolver", "isSome", "versions", "gatherObjectVersions", "artifactDiff", "tryPromise", "try", "value", "resolve", "entries", "id", "version", "lastVersion", "catch", "AiAssistantError", "wrap", "message", "log", "ObjectVersion", "equals", "get", "delete", "push", "_tag", "objectId", "size", "createArtifactUpdateBlock", "make", "Message", "created", "Date", "toISOString", "sender", "role", "text", "messages", "artifactIds", "Map", "block", "set", "disposition", "diff", "AiSession", "_semaphore", "runSync", "makeSemaphore", "_history", "_pending", "_started", "_ended", "_toolCalls", "_options", "duration", "toolCalls", "run", "prompt", "system", "systemTemplate", "history", "objects", "blueprints", "toolkit", "observer", "GenerationObserver", "noop", "gen", "Date", "now", "pending", "submitMessage", "fnUntraced", "message", "push", "onMessage", "TracingService", "emitConverationMessage", "promptMessage", "formatUserPrompt", "formatSystemPrompt", "pipe", "orDie", "log", "snippet", "createSnippet", "length", "AiPreprocessor", "preprocessPrompt", "blocks", "streamText", "disableToolCallResolution", "AiParser", "parseResponse", "onBegin", "onBlock", "block", "onPart", "part", "onEnd", "summary", "runCollect", "map", "toArray", "response", "Obj", "make", "Message", "created", "toISOString", "sender", "role", "getToolCalls", "Error", "toolResults", "forEach", "toolCall", "callTool", "provide", "layerSubframe", "context", "parentMessage", "id", "toolCallId", "tools", "withPermits", "withSpan", "runStructured", "_schema", "todo", "text", "len", "slice", "join", "Toolkit", "Effect", "ToolExecutionService", "ToolResolverService", "isTruthy", "createToolkit", "toolkit", "toolkitProp", "blueprints", "gen", "blueprintToolkit", "ToolResolverService", "resolveToolkit", "flatMap", "tools", "blueprintToolHandler", "toContext", "ToolExecutionService", "handlersFor", "merge", "filter", "isTruthy", "pipe", "provide", "Schema", "getVersion", "ObjectId", "ObjectVersionSchema", "Unknown", "VersionPinSchema", "Struct", "objectId", "ObjectId", "version", "VersionPin", "DISPOSITION", "fromObject", "object", "make", "id", "getVersion", "createBlock", "pin", "_tag", "disposition", "data", "JSON", "stringify", "merge", "Toolkit", "Effect", "Layer", "Pipeable", "TypeId", "make", "toolkit", "layer", "handlers", "pipe", "provide", "pipeArguments", "arguments", "merge", "toolkits", "map", "t", "mergeAll", "Schema", "Type", "Database", "EncodedReference", "DXN", "LOCAL_SPACE_TAG", "trim", "createArtifactElement", "id", "ArtifactId", "String", "annotations", "description", "trim", "examples", "toDXN", "reference", "owningSpaceId", "startsWith", "DXN", "parse", "slice", "test", "spaceId", "objectId", "split", "kind", "ECHO", "LOCAL_SPACE_TAG", "fromLocalObjectId", "Error", "resolve", "schema", "ref", "dxn", "Database", "Service", "RefFromLLM", "transform", "Type", "Ref", "Obj", "decode", "fromA", "fromI", "EncodedReference", "fromDXN", "encode", "toI", "toA", "toString", "strict", "Tool", "Context", "Effect", "Layer", "Record", "Schema", "AiToolNotFoundError", "ToolExecutionService", "ToolResolverService", "todo", "Query", "Type", "Database", "Function", "FunctionDefinition", "FunctionInvocationService", "invariant", "makeToolResolverFromFunctions", "functions", "toolkit", "effect", "ToolResolverService", "gen", "dbService", "Database", "Service", "resolve", "id", "tool", "tools", "dbFunction", "runQuery", "Query", "type", "Function", "key", "functionDef", "FunctionDefinition", "deserialize", "find", "fn", "fail", "AiToolNotFoundError", "projectFunctionToTool", "pipe", "provideService", "makeToolExecutionServiceFromFunctions", "handlersLayer", "ToolExecutionService", "toolkitHandler", "provide", "invariant", "isHandlerLike", "functionInvocationService", "FunctionInvocationService", "handlersFor", "makeHandler", "name", "input", "isProviderDefined", "Error", "result", "handle", "definition", "get", "FunctionToolAnnotation", "annotations", "invokeFunction", "catchAllDefect", "defect", "of", "map", "_name", "isUserDefined", "Tag", "toolCache", "WeakMap", "has", "make", "makeToolName", "description", "parameters", "createStructFieldsFromSchema", "inputSchema", "failure", "Any", "annotate", "set", "toolName", "toLowerCase", "replace", "match", "schema", "ast", "_tag", "Object", "fromEntries", "propertySignatures", "prop", "mapSchemaTypeForLLM", "todo", "Type", "Ref", "isRefSchemaAST", "RefFromLLM", "value"]
7
+ }
@@ -10,7 +10,7 @@ import { Message } from "@dxos/types";
10
10
  var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/extraction/extraction.ts";
11
11
  var ExtractionInput = Schema.Struct({
12
12
  message: Message.Message,
13
- objects: Schema.optional(Schema.Array(Type.Expando)),
13
+ objects: Schema.optional(Schema.Array(Type.Obj)),
14
14
  options: Schema.optional(Schema.Struct({
15
15
  timeout: Schema.optional(Schema.Number),
16
16
  fallbackToRaw: Schema.optional(Schema.Boolean)
@@ -176,7 +176,7 @@ var ReferencedQuotes = Schema2.Struct({
176
176
  });
177
177
  var findQuotes = async (quotes, db) => {
178
178
  const references = await Promise.all(quotes.map(async (quote) => {
179
- const { objects } = await db.query(Query.select(Filter.text(quote, {
179
+ const objects = await db.query(Query.select(Filter.text(quote, {
180
180
  type: "vector"
181
181
  }))).run();
182
182
  log3.info("found quotes in vector index", {
@@ -184,7 +184,7 @@ var findQuotes = async (quotes, db) => {
184
184
  quote
185
185
  }, {
186
186
  F: __dxlog_file3,
187
- L: 35,
187
+ L: 34,
188
188
  S: void 0,
189
189
  C: (f, a) => f(...a)
190
190
  });
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/extraction/extraction.ts", "../../../../src/extraction/extraction-llm-function/extraction-llm-function.ts", "../../../../src/extraction/extraction-ner-function/extraction-ner-function.ts", "../../../../src/extraction/extraction-ner-function/named-entity-recognition.ts", "../../../../src/extraction/quotes.ts"],
4
- "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport * as Schema from 'effect/Schema';\n\nimport { asyncTimeout } from '@dxos/async';\nimport { Type } from '@dxos/echo';\nimport { type FunctionDefinition } from '@dxos/functions';\nimport { type FunctionExecutor } from '@dxos/functions-runtime';\nimport { log } from '@dxos/log';\nimport { Message } from '@dxos/types';\n\nexport const ExtractionInput = Schema.Struct({\n message: Message.Message,\n objects: Schema.optional(Schema.Array(Type.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: Message.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\nimport { type FunctionDefinition, defineFunction } from '@dxos/functions';\n\nimport { ExtractionInput, ExtractionOutput } from '../extraction';\n\nexport const extractionAnthropicFunction: FunctionDefinition<ExtractionInput, ExtractionOutput> = defineFunction({\n key: 'dxos.org/function/extraction/extract-entities',\n name: 'Extract Entities',\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 throw new Error('not implemented');\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.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 DataType.ContentBlock.Text,\n // {\n // _tag: 'text',\n // text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`,\n // } satisfies DataType.ContentBlock.Text,\n // ],\n // }),\n // ],\n // artifacts: [],\n // prompt: '',\n // tools: [],\n // } as any); // TODO(burdon): Rewrite test.\n\n // return {\n // timeElapsed: performance.now() - startTime,\n // message: create(DataType.Message.Message, {\n // ...message,\n // blocks: message.blocks.map((block) =>\n // block._tag !== 'transcript'\n // ? block\n // : {\n // ...block,\n // text: insertReferences(block.text, result),\n // },\n // ),\n // }),\n // };\n },\n});\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { defineFunction } from '@dxos/functions';\n\nimport { ExtractionInput, ExtractionOutput } from '../extraction';\n\n/**\n * Named Entity Recognition model\n */\n// TODO(mykola): Make it use vector index to query objects and not pass objects as input.\nexport const extractionNerFunction = defineFunction({\n key: 'dxos.org/function/extraction/extract-entities',\n name: 'Extract Entities',\n description: 'Extracts named entities from a text',\n inputSchema: ExtractionInput,\n outputSchema: ExtractionOutput,\n handler: async ({ data: { message, options }, context }) => {\n throw new Error('not implemented');\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');\n // return extractFullEntities(block.text);\n // }),\n // ).then((entities) => entities.flat());\n // const entities = options?.timeout ? await asyncTimeout(entitiesPromise, options.timeout) : await entitiesPromise;\n\n // log.info('entities', { entities });\n // const quoteReferences = await findQuotes(\n // entities.map((entity) => entity.word),\n // db,\n // );\n\n // const blocksWithReferences = message.blocks.map((block) => {\n // invariant(block._tag === 'transcript' || block._tag === 'text');\n // return { ...block, text: insertReferences(block._tag, quoteReferences) };\n // });\n\n // const messageWithReferences = create(Message.Message, {\n // ...message,\n // blocks: blocksWithReferences,\n // });\n\n // log.info('output', { messageWithReferences });\n // return {\n // message: messageWithReferences,\n // timeElapsed: performance.now() - startTime,\n // };\n },\n});\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport {\n type TokenClassificationOutput,\n type TokenClassificationPipelineType,\n type TokenClassificationSingle,\n pipeline,\n} from '@xenova/transformers';\n\nimport { log } from '@dxos/log';\n\nlet _ner: Promise<TokenClassificationPipelineType>;\n\n/**\n * Named Entity Recognition pipeline.\n * Initializes the pipeline on first call.\n * @returns The singleton promise that resolves to a token classification pipeline.\n */\nexport const getNer = () => {\n if (!_ner) {\n _ner = pipeline('ner', 'Xenova/bert-base-NER').then((ner) => {\n log.info('NER model is ready');\n return ner;\n });\n }\n\n return _ner;\n};\n\n/**\n * Extracts entities from text using the named entity recognition pipeline and does simple grouping of tokens.\n */\nexport const extractFullEntities = async (text: string): Promise<TokenClassificationSingle[]> => {\n const ner = await getNer();\n const entities = (await ner(text)) as TokenClassificationOutput;\n const tokenGroups = createTokenGroups(entities);\n const combinedEntities = tokenGroups.map(combineNerTokens);\n return combinedEntities;\n};\n\nexport const createTokenGroups = (tokens: TokenClassificationSingle[]): TokenClassificationSingle[][] => {\n const tokenGroups: TokenClassificationSingle[][] = [];\n\n const canBeCombined = (prevToken: TokenClassificationSingle, currentToken: TokenClassificationSingle) => {\n const [currentState, currentEntity] = currentToken.entity.split('-'); // B, I, O\n const [prevState, prevEntity] = prevToken.entity.split('-'); // B, I, O\n if (prevEntity !== currentEntity) {\n return false;\n }\n if (prevState === 'O') {\n return false;\n }\n if (currentState === 'I') {\n return true;\n }\n if ((prevState === 'B' || prevState === 'I') && currentState === 'B') {\n return currentToken.word.startsWith('#');\n }\n return false;\n };\n\n for (const token of tokens) {\n if (tokenGroups.length === 0) {\n tokenGroups.push([token]);\n continue;\n }\n const lastTokenGroup = tokenGroups.at(-1)!;\n if (canBeCombined(lastTokenGroup.at(-1)!, token)) {\n lastTokenGroup.push(token);\n continue;\n }\n tokenGroups.push([token]);\n }\n\n return tokenGroups;\n};\n\nexport const combineNerTokens = (group: TokenClassificationSingle[]): TokenClassificationSingle => {\n const combinedToken: TokenClassificationSingle = {\n entity: group.at(0)!.entity.split('-')[1],\n score: group.reduce((acc, token) => acc + token.score, 0) / group.length,\n index: group.at(0)!.index,\n word: group.at(0)!.word,\n start: group.at(0)!.start,\n end: group.at(-1)!.end,\n };\n\n for (const token of group.slice(1)) {\n if (token.word.startsWith('#')) {\n combinedToken.word += token.word.replace(/^#+/, '');\n } else {\n combinedToken.word += ' ' + token.word;\n }\n }\n\n return combinedToken;\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport * as Schema from 'effect/Schema';\n\nimport { Filter, Query } from '@dxos/echo';\nimport { type EchoDatabase } from '@dxos/echo-db';\nimport { ObjectId } from '@dxos/keys';\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"],
5
- "mappings": ";;;;AAIA,YAAYA,YAAY;AAExB,SAASC,oBAAoB;AAC7B,SAASC,YAAY;AAGrB,SAASC,WAAW;AACpB,SAASC,eAAe;;AAEjB,IAAMC,kBAAyBC,cAAO;EAC3CC,SAASH,QAAQA;EACjBI,SAAgBC,gBAAgBC,aAAMR,KAAKS,OAAO,CAAA;EAClDC,SAAgBH,gBACPH,cAAO;IACZO,SAAgBJ,gBAAgBK,aAAM;IACtCC,eAAsBN,gBAAgBO,cAAO;EAC/C,CAAA,CAAA;AAEJ,CAAA;AAGO,IAAMC,mBAA0BX,cAAO;EAC5CC,SAASH,QAAQA;EACjBc,aAAoBJ;AACtB,CAAA;AA6BO,I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AAMO,IAAMC,mBAAmB,CAACP,MAAcR,WAAAA;AAC7C,aAAWN,SAASM,OAAOV,YAAY;AACrC,QAAI,CAACL,SAAS+B,QAAQtB,MAAMF,EAAE,GAAG;AAC/B;IACF;AAGA,UAAMyB,QAAQ,IAAIC,OAAOxB,MAAMA,MAAMyB,QAAQ,uBAAuB,MAAA,GAAS,IAAA;AAC7EX,WAAOA,KAAKW,QAAQF,OAAO,IAAIvB,MAAMA,KAAK,KAAKR,IAAIkC,kBAAkB1B,MAAMF,EAAE,EAAEqB,SAAQ,CAAA,GAAK;EAC9F;AAEA,SAAOL;AACT;",
6
- "names": ["Schema", "asyncTimeout", "Type", "log", "Message", "ExtractionInput", "Struct", "message", "objects", "optional", "Array", "Expando", "options", "timeout", "Number", "fallbackToRaw", "Boolean", "ExtractionOutput", "timeElapsed", "processTranscriptMessage", "params", "executor", "invoke", "function", "input", "error", "warn", "defineFunction", "extractionAnthropicFunction", "defineFunction", "key", "name", "description", "inputSchema", "ExtractionInput", "outputSchema", "ExtractionOutput", "handler", "data", "message", "objects", "context", "Error", "defineFunction", "extractionNerFunction", "defineFunction", "key", "name", "description", "inputSchema", "ExtractionInput", "outputSchema", "ExtractionOutput", "handler", "data", "message", "options", "context", "Error", "pipeline", "log", "_ner", "getNer", "then", "ner", "info", "extractFullEntities", "text", "entities", "tokenGroups", "createTokenGroups", "combinedEntities", "map", "combineNerTokens", "tokens", "canBeCombined", "prevToken", "currentToken", "currentState", "currentEntity", "entity", "split", "prevState", "prevEntity", "word", "startsWith", "token", "length", "push", "lastTokenGroup", "at", "group", "combinedToken", "score", "reduce", "acc", "index", "start", "end", "slice", "replace", "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"]
4
+ "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport * as Schema from 'effect/Schema';\n\nimport { asyncTimeout } from '@dxos/async';\nimport { Type } from '@dxos/echo';\nimport { type FunctionDefinition } from '@dxos/functions';\nimport { type FunctionExecutor } from '@dxos/functions-runtime';\nimport { log } from '@dxos/log';\nimport { Message } from '@dxos/types';\n\nexport const ExtractionInput = Schema.Struct({\n message: Message.Message,\n objects: Schema.optional(Schema.Array(Type.Obj)),\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: Message.Message,\n timeElapsed: Schema.Number,\n});\nexport interface ExtractionOutput extends Schema.Schema.Type<typeof ExtractionOutput> {}\n\nexport type ProcessTranscriptMessageProps = {\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: ProcessTranscriptMessageProps): 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\nimport { type FunctionDefinition, defineFunction } from '@dxos/functions';\n\nimport { ExtractionInput, ExtractionOutput } from '../extraction';\n\nexport const extractionAnthropicFunction: FunctionDefinition<ExtractionInput, ExtractionOutput> = defineFunction({\n key: 'dxos.org/function/extraction/extract-entities',\n name: 'Extract Entities',\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 throw new Error('not implemented');\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.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 DataType.ContentBlock.Text,\n // {\n // _tag: 'text',\n // text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`,\n // } satisfies DataType.ContentBlock.Text,\n // ],\n // }),\n // ],\n // artifacts: [],\n // prompt: '',\n // tools: [],\n // } as any); // TODO(burdon): Rewrite test.\n\n // return {\n // timeElapsed: performance.now() - startTime,\n // message: create(DataType.Message.Message, {\n // ...message,\n // blocks: message.blocks.map((block) =>\n // block._tag !== 'transcript'\n // ? block\n // : {\n // ...block,\n // text: insertReferences(block.text, result),\n // },\n // ),\n // }),\n // };\n },\n});\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { defineFunction } from '@dxos/functions';\n\nimport { ExtractionInput, ExtractionOutput } from '../extraction';\n\n/**\n * Named Entity Recognition model\n */\n// TODO(mykola): Make it use vector index to query objects and not pass objects as input.\nexport const extractionNerFunction = defineFunction({\n key: 'dxos.org/function/extraction/extract-entities',\n name: 'Extract Entities',\n description: 'Extracts named entities from a text',\n inputSchema: ExtractionInput,\n outputSchema: ExtractionOutput,\n handler: async ({ data: { message, options }, context }) => {\n throw new Error('not implemented');\n // log.info('input', { message, options });\n // const startTime = performance.now();\n // const { db } = context.getService(Database.Service);\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');\n // return extractFullEntities(block.text);\n // }),\n // ).then((entities) => entities.flat());\n // const entities = options?.timeout ? await asyncTimeout(entitiesPromise, options.timeout) : await entitiesPromise;\n\n // log.info('entities', { entities });\n // const quoteReferences = await findQuotes(\n // entities.map((entity) => entity.word),\n // db,\n // );\n\n // const blocksWithReferences = message.blocks.map((block) => {\n // invariant(block._tag === 'transcript' || block._tag === 'text');\n // return { ...block, text: insertReferences(block._tag, quoteReferences) };\n // });\n\n // const messageWithReferences = create(Message.Message, {\n // ...message,\n // blocks: blocksWithReferences,\n // });\n\n // log.info('output', { messageWithReferences });\n // return {\n // message: messageWithReferences,\n // timeElapsed: performance.now() - startTime,\n // };\n },\n});\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport {\n type TokenClassificationOutput,\n type TokenClassificationPipelineType,\n type TokenClassificationSingle,\n pipeline,\n} from '@xenova/transformers';\n\nimport { log } from '@dxos/log';\n\nlet _ner: Promise<TokenClassificationPipelineType>;\n\n/**\n * Named Entity Recognition pipeline.\n * Initializes the pipeline on first call.\n * @returns The singleton promise that resolves to a token classification pipeline.\n */\nexport const getNer = () => {\n if (!_ner) {\n _ner = pipeline('ner', 'Xenova/bert-base-NER').then((ner) => {\n log.info('NER model is ready');\n return ner;\n });\n }\n\n return _ner;\n};\n\n/**\n * Extracts entities from text using the named entity recognition pipeline and does simple grouping of tokens.\n */\nexport const extractFullEntities = async (text: string): Promise<TokenClassificationSingle[]> => {\n const ner = await getNer();\n const entities = (await ner(text)) as TokenClassificationOutput;\n const tokenGroups = createTokenGroups(entities);\n const combinedEntities = tokenGroups.map(combineNerTokens);\n return combinedEntities;\n};\n\nexport const createTokenGroups = (tokens: TokenClassificationSingle[]): TokenClassificationSingle[][] => {\n const tokenGroups: TokenClassificationSingle[][] = [];\n\n const canBeCombined = (prevToken: TokenClassificationSingle, currentToken: TokenClassificationSingle) => {\n const [currentState, currentEntity] = currentToken.entity.split('-'); // B, I, O\n const [prevState, prevEntity] = prevToken.entity.split('-'); // B, I, O\n if (prevEntity !== currentEntity) {\n return false;\n }\n if (prevState === 'O') {\n return false;\n }\n if (currentState === 'I') {\n return true;\n }\n if ((prevState === 'B' || prevState === 'I') && currentState === 'B') {\n return currentToken.word.startsWith('#');\n }\n return false;\n };\n\n for (const token of tokens) {\n if (tokenGroups.length === 0) {\n tokenGroups.push([token]);\n continue;\n }\n const lastTokenGroup = tokenGroups.at(-1)!;\n if (canBeCombined(lastTokenGroup.at(-1)!, token)) {\n lastTokenGroup.push(token);\n continue;\n }\n tokenGroups.push([token]);\n }\n\n return tokenGroups;\n};\n\nexport const combineNerTokens = (group: TokenClassificationSingle[]): TokenClassificationSingle => {\n const combinedToken: TokenClassificationSingle = {\n entity: group.at(0)!.entity.split('-')[1],\n score: group.reduce((acc, token) => acc + token.score, 0) / group.length,\n index: group.at(0)!.index,\n word: group.at(0)!.word,\n start: group.at(0)!.start,\n end: group.at(-1)!.end,\n };\n\n for (const token of group.slice(1)) {\n if (token.word.startsWith('#')) {\n combinedToken.word += token.word.replace(/^#+/, '');\n } else {\n combinedToken.word += ' ' + token.word;\n }\n }\n\n return combinedToken;\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport * as Schema from 'effect/Schema';\n\nimport { type Database, Filter, Query } from '@dxos/echo';\nimport { ObjectId } from '@dxos/keys';\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: Database.Database): 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"],
5
+ "mappings": ";;;;AAIA,YAAYA,YAAY;AAExB,SAASC,oBAAoB;AAC7B,SAASC,YAAY;AAGrB,SAASC,WAAW;AACpB,SAASC,eAAe;;AAEjB,IAAMC,kBAAyBC,cAAO;EAC3CC,SAASH,QAAQA;EACjBI,SAAgBC,gBAAgBC,aAAMR,KAAKS,GAAG,CAAA;EAC9CC,SAAgBH,gBACPH,cAAO;IACZO,SAAgBJ,gBAAgBK,aAAM;IACtCC,eAAsBN,gBAAgBO,cAAO;EAC/C,CAAA,CAAA;AAEJ,CAAA;AAGO,IAAMC,mBAA0BX,cAAO;EAC5CC,SAASH,QAAQA;EACjBc,aAAoBJ;AACtB,CAAA;AA6BO,IAAMK,2BAA2B,OAAOC,WAAAA;AAC7C,MAAI;AACF,QAAIA,OAAOR,SAASC,WAAWO,OAAOR,QAAQC,UAAU,GAA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,MAAMF,EAAE,GAAG;AAC/B;IACF;AAGA,UAAMyB,QAAQ,IAAIC,OAAOxB,MAAMA,MAAMyB,QAAQ,uBAAuB,MAAA,GAAS,IAAA;AAC7EX,WAAOA,KAAKW,QAAQF,OAAO,IAAIvB,MAAMA,KAAK,KAAKR,IAAIkC,kBAAkB1B,MAAMF,EAAE,EAAEqB,SAAQ,CAAA,GAAK;EAC9F;AAEA,SAAOL;AACT;",
6
+ "names": ["Schema", "asyncTimeout", "Type", "log", "Message", "ExtractionInput", "Struct", "message", "objects", "optional", "Array", "Obj", "options", "timeout", "Number", "fallbackToRaw", "Boolean", "ExtractionOutput", "timeElapsed", "processTranscriptMessage", "params", "executor", "invoke", "function", "input", "error", "warn", "defineFunction", "extractionAnthropicFunction", "defineFunction", "key", "name", "description", "inputSchema", "ExtractionInput", "outputSchema", "ExtractionOutput", "handler", "data", "message", "objects", "context", "Error", "defineFunction", "extractionNerFunction", "defineFunction", "key", "name", "description", "inputSchema", "ExtractionInput", "outputSchema", "ExtractionOutput", "handler", "data", "message", "options", "context", "Error", "pipeline", "log", "_ner", "getNer", "then", "ner", "info", "extractFullEntities", "text", "entities", "tokenGroups", "createTokenGroups", "combinedEntities", "map", "combineNerTokens", "tokens", "canBeCombined", "prevToken", "currentToken", "currentState", "currentEntity", "entity", "split", "prevState", "prevEntity", "word", "startsWith", "token", "length", "push", "lastTokenGroup", "at", "group", "combinedToken", "score", "reduce", "acc", "index", "start", "end", "slice", "replace", "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"]
7
7
  }