@dxos/assistant-toolkit 0.8.4-main.21d9917
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.
- package/LICENSE +8 -0
- package/README.md +3 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +2880 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/browser/testing/index.mjs +43 -0
- package/dist/lib/browser/testing/index.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +2881 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/lib/node-esm/testing/index.mjs +44 -0
- package/dist/lib/node-esm/testing/index.mjs.map +7 -0
- package/dist/types/src/blueprints/design/design-blueprint.d.ts +18 -0
- package/dist/types/src/blueprints/design/design-blueprint.d.ts.map +1 -0
- package/dist/types/src/blueprints/design/design-blueprint.test.d.ts +2 -0
- package/dist/types/src/blueprints/design/design-blueprint.test.d.ts.map +1 -0
- package/dist/types/src/blueprints/design/index.d.ts +3 -0
- package/dist/types/src/blueprints/design/index.d.ts.map +1 -0
- package/dist/types/src/blueprints/discord/discord-blueprint.d.ts +18 -0
- package/dist/types/src/blueprints/discord/discord-blueprint.d.ts.map +1 -0
- package/dist/types/src/blueprints/discord/index.d.ts +3 -0
- package/dist/types/src/blueprints/discord/index.d.ts.map +1 -0
- package/dist/types/src/blueprints/index.d.ts +7 -0
- package/dist/types/src/blueprints/index.d.ts.map +1 -0
- package/dist/types/src/blueprints/linear/index.d.ts +3 -0
- package/dist/types/src/blueprints/linear/index.d.ts.map +1 -0
- package/dist/types/src/blueprints/linear/linear-blueprint.d.ts +18 -0
- package/dist/types/src/blueprints/linear/linear-blueprint.d.ts.map +1 -0
- package/dist/types/src/blueprints/planning/index.d.ts +3 -0
- package/dist/types/src/blueprints/planning/index.d.ts.map +1 -0
- package/dist/types/src/blueprints/planning/planning-blueprint.d.ts +18 -0
- package/dist/types/src/blueprints/planning/planning-blueprint.d.ts.map +1 -0
- package/dist/types/src/blueprints/planning/planning-blueprint.test.d.ts +2 -0
- package/dist/types/src/blueprints/planning/planning-blueprint.test.d.ts.map +1 -0
- package/dist/types/src/blueprints/research/index.d.ts +3 -0
- package/dist/types/src/blueprints/research/index.d.ts.map +1 -0
- package/dist/types/src/blueprints/research/research-blueprint.d.ts +18 -0
- package/dist/types/src/blueprints/research/research-blueprint.d.ts.map +1 -0
- package/dist/types/src/blueprints/research/research-blueprint.test.d.ts +2 -0
- package/dist/types/src/blueprints/research/research-blueprint.test.d.ts.map +1 -0
- package/dist/types/src/blueprints/testing.d.ts +12 -0
- package/dist/types/src/blueprints/testing.d.ts.map +1 -0
- package/dist/types/src/blueprints/websearch/index.d.ts +4 -0
- package/dist/types/src/blueprints/websearch/index.d.ts.map +1 -0
- package/dist/types/src/blueprints/websearch/websearch-blueprint.d.ts +18 -0
- package/dist/types/src/blueprints/websearch/websearch-blueprint.d.ts.map +1 -0
- package/dist/types/src/blueprints/websearch/websearch-toolkit.d.ts +27 -0
- package/dist/types/src/blueprints/websearch/websearch-toolkit.d.ts.map +1 -0
- package/dist/types/src/crud/graph.d.ts +67 -0
- package/dist/types/src/crud/graph.d.ts.map +1 -0
- package/dist/types/src/crud/graph.test.d.ts +2 -0
- package/dist/types/src/crud/graph.test.d.ts.map +1 -0
- package/dist/types/src/crud/index.d.ts +2 -0
- package/dist/types/src/crud/index.d.ts.map +1 -0
- package/dist/types/src/experimental/feed.test.d.ts +2 -0
- package/dist/types/src/experimental/feed.test.d.ts.map +1 -0
- package/dist/types/src/functions/agent/index.d.ts +5 -0
- package/dist/types/src/functions/agent/index.d.ts.map +1 -0
- package/dist/types/src/functions/agent/prompt.d.ts +76 -0
- package/dist/types/src/functions/agent/prompt.d.ts.map +1 -0
- package/dist/types/src/functions/discord/fetch-messages.d.ts +11 -0
- package/dist/types/src/functions/discord/fetch-messages.d.ts.map +1 -0
- package/dist/types/src/functions/discord/fetch-messages.test.d.ts +2 -0
- package/dist/types/src/functions/discord/fetch-messages.test.d.ts.map +1 -0
- package/dist/types/src/functions/discord/index.d.ts +12 -0
- package/dist/types/src/functions/discord/index.d.ts.map +1 -0
- package/dist/types/src/functions/document/index.d.ts +13 -0
- package/dist/types/src/functions/document/index.d.ts.map +1 -0
- package/dist/types/src/functions/document/read.d.ts +7 -0
- package/dist/types/src/functions/document/read.d.ts.map +1 -0
- package/dist/types/src/functions/document/update.d.ts +6 -0
- package/dist/types/src/functions/document/update.d.ts.map +1 -0
- package/dist/types/src/functions/entity-extraction/entity-extraction.d.ts +174 -0
- package/dist/types/src/functions/entity-extraction/entity-extraction.d.ts.map +1 -0
- package/dist/types/src/functions/entity-extraction/entity-extraction.test.d.ts +2 -0
- package/dist/types/src/functions/entity-extraction/entity-extraction.test.d.ts.map +1 -0
- package/dist/types/src/functions/entity-extraction/index.d.ts +175 -0
- package/dist/types/src/functions/entity-extraction/index.d.ts.map +1 -0
- package/dist/types/src/functions/exa/data/exa-search-1748337321991.d.ts +38 -0
- package/dist/types/src/functions/exa/data/exa-search-1748337321991.d.ts.map +1 -0
- package/dist/types/src/functions/exa/data/exa-search-1748337331526.d.ts +37 -0
- package/dist/types/src/functions/exa/data/exa-search-1748337331526.d.ts.map +1 -0
- package/dist/types/src/functions/exa/data/exa-search-1748337344119.d.ts +58 -0
- package/dist/types/src/functions/exa/data/exa-search-1748337344119.d.ts.map +1 -0
- package/dist/types/src/functions/exa/data/index.d.ts +3 -0
- package/dist/types/src/functions/exa/data/index.d.ts.map +1 -0
- package/dist/types/src/functions/exa/exa.d.ts +5 -0
- package/dist/types/src/functions/exa/exa.d.ts.map +1 -0
- package/dist/types/src/functions/exa/index.d.ts +3 -0
- package/dist/types/src/functions/exa/index.d.ts.map +1 -0
- package/dist/types/src/functions/exa/mock.d.ts +5 -0
- package/dist/types/src/functions/exa/mock.d.ts.map +1 -0
- package/dist/types/src/functions/github/fetch-prs.d.ts +6 -0
- package/dist/types/src/functions/github/fetch-prs.d.ts.map +1 -0
- package/dist/types/src/functions/index.d.ts +8 -0
- package/dist/types/src/functions/index.d.ts.map +1 -0
- package/dist/types/src/functions/linear/index.d.ts +9 -0
- package/dist/types/src/functions/linear/index.d.ts.map +1 -0
- package/dist/types/src/functions/linear/linear.test.d.ts +2 -0
- package/dist/types/src/functions/linear/linear.test.d.ts.map +1 -0
- package/dist/types/src/functions/linear/sync-issues.d.ts +12 -0
- package/dist/types/src/functions/linear/sync-issues.d.ts.map +1 -0
- package/dist/types/src/functions/research/document-create.d.ts +9 -0
- package/dist/types/src/functions/research/document-create.d.ts.map +1 -0
- package/dist/types/src/functions/research/index.d.ts +21 -0
- package/dist/types/src/functions/research/index.d.ts.map +1 -0
- package/dist/types/src/functions/research/research-graph.d.ts +19 -0
- package/dist/types/src/functions/research/research-graph.d.ts.map +1 -0
- package/dist/types/src/functions/research/research.d.ts +14 -0
- package/dist/types/src/functions/research/research.d.ts.map +1 -0
- package/dist/types/src/functions/research/research.test.d.ts +2 -0
- package/dist/types/src/functions/research/research.test.d.ts.map +1 -0
- package/dist/types/src/functions/research/types.d.ts +6 -0
- package/dist/types/src/functions/research/types.d.ts.map +1 -0
- package/dist/types/src/functions/tasks/index.d.ts +15 -0
- package/dist/types/src/functions/tasks/index.d.ts.map +1 -0
- package/dist/types/src/functions/tasks/read.d.ts +7 -0
- package/dist/types/src/functions/tasks/read.d.ts.map +1 -0
- package/dist/types/src/functions/tasks/task-list.d.ts +74 -0
- package/dist/types/src/functions/tasks/task-list.d.ts.map +1 -0
- package/dist/types/src/functions/tasks/task-list.test.d.ts +2 -0
- package/dist/types/src/functions/tasks/task-list.test.d.ts.map +1 -0
- package/dist/types/src/functions/tasks/update.d.ts +9 -0
- package/dist/types/src/functions/tasks/update.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +6 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/initiative/Initiative.d.ts +84 -0
- package/dist/types/src/initiative/Initiative.d.ts.map +1 -0
- package/dist/types/src/initiative/InitiativeSchema.d.ts +19 -0
- package/dist/types/src/initiative/InitiativeSchema.d.ts.map +1 -0
- package/dist/types/src/initiative/functions/agent.d.ts +37 -0
- package/dist/types/src/initiative/functions/agent.d.ts.map +1 -0
- package/dist/types/src/initiative/functions/getContext.d.ts +17 -0
- package/dist/types/src/initiative/functions/getContext.d.ts.map +1 -0
- package/dist/types/src/initiative/functions/index.d.ts +4 -0
- package/dist/types/src/initiative/functions/index.d.ts.map +1 -0
- package/dist/types/src/initiative/functions/update.d.ts +7 -0
- package/dist/types/src/initiative/functions/update.d.ts.map +1 -0
- package/dist/types/src/initiative/index.d.ts +1 -0
- package/dist/types/src/initiative/index.d.ts.map +1 -0
- package/dist/types/src/initiative/initiative.test.d.ts +2 -0
- package/dist/types/src/initiative/initiative.test.d.ts.map +1 -0
- package/dist/types/src/sync/index.d.ts +2 -0
- package/dist/types/src/sync/index.d.ts.map +1 -0
- package/dist/types/src/sync/sync.d.ts +15 -0
- package/dist/types/src/sync/sync.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +2 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/plugins.d.ts +19 -0
- package/dist/types/src/testing/plugins.d.ts.map +1 -0
- package/dist/types/src/toolkits/AssistantToolkit.d.ts +43 -0
- package/dist/types/src/toolkits/AssistantToolkit.d.ts.map +1 -0
- package/dist/types/src/toolkits/AssistantToolkit.test.d.ts +2 -0
- package/dist/types/src/toolkits/AssistantToolkit.test.d.ts.map +1 -0
- package/dist/types/src/toolkits/SystemToolkit.d.ts +99 -0
- package/dist/types/src/toolkits/SystemToolkit.d.ts.map +1 -0
- package/dist/types/src/toolkits/index.d.ts +3 -0
- package/dist/types/src/toolkits/index.d.ts.map +1 -0
- package/dist/types/src/util/graphql.d.ts +22 -0
- package/dist/types/src/util/graphql.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +2 -0
- package/dist/types/src/util/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +83 -0
- package/src/blueprints/design/design-blueprint.test.ts +105 -0
- package/src/blueprints/design/design-blueprint.ts +31 -0
- package/src/blueprints/design/index.ts +7 -0
- package/src/blueprints/discord/discord-blueprint.ts +32 -0
- package/src/blueprints/discord/index.ts +7 -0
- package/src/blueprints/index.ts +10 -0
- package/src/blueprints/linear/index.ts +7 -0
- package/src/blueprints/linear/linear-blueprint.ts +33 -0
- package/src/blueprints/planning/index.ts +7 -0
- package/src/blueprints/planning/planning-blueprint.test.ts +126 -0
- package/src/blueprints/planning/planning-blueprint.ts +96 -0
- package/src/blueprints/research/index.ts +7 -0
- package/src/blueprints/research/research-blueprint.test.ts +7 -0
- package/src/blueprints/research/research-blueprint.ts +51 -0
- package/src/blueprints/testing.ts +34 -0
- package/src/blueprints/websearch/index.ts +9 -0
- package/src/blueprints/websearch/websearch-blueprint.ts +18 -0
- package/src/blueprints/websearch/websearch-toolkit.ts +8 -0
- package/src/crud/graph.test.ts +46 -0
- package/src/crud/graph.ts +380 -0
- package/src/crud/index.ts +5 -0
- package/src/experimental/feed.test.ts +111 -0
- package/src/functions/agent/index.ts +11 -0
- package/src/functions/agent/prompt.ts +118 -0
- package/src/functions/discord/fetch-messages.test.ts +58 -0
- package/src/functions/discord/fetch-messages.ts +252 -0
- package/src/functions/discord/index.ts +9 -0
- package/src/functions/document/index.ts +12 -0
- package/src/functions/document/read.ts +30 -0
- package/src/functions/document/update.ts +33 -0
- package/src/functions/entity-extraction/entity-extraction.conversations.json +6597 -0
- package/src/functions/entity-extraction/entity-extraction.test.ts +93 -0
- package/src/functions/entity-extraction/entity-extraction.ts +183 -0
- package/src/functions/entity-extraction/index.ts +9 -0
- package/src/functions/exa/data/exa-search-1748337321991.ts +131 -0
- package/src/functions/exa/data/exa-search-1748337331526.ts +144 -0
- package/src/functions/exa/data/exa-search-1748337344119.ts +133 -0
- package/src/functions/exa/data/index.ts +11 -0
- package/src/functions/exa/exa.ts +37 -0
- package/src/functions/exa/index.ts +6 -0
- package/src/functions/exa/mock.ts +71 -0
- package/src/functions/github/fetch-prs.ts +31 -0
- package/src/functions/index.ts +11 -0
- package/src/functions/linear/index.ts +9 -0
- package/src/functions/linear/linear.test.ts +84 -0
- package/src/functions/linear/sync-issues.ts +191 -0
- package/src/functions/research/document-create.ts +76 -0
- package/src/functions/research/index.ts +14 -0
- package/src/functions/research/research-graph.ts +49 -0
- package/src/functions/research/research-instructions.tpl +106 -0
- package/src/functions/research/research.conversations.json +1 -0
- package/src/functions/research/research.test.ts +171 -0
- package/src/functions/research/research.ts +189 -0
- package/src/functions/research/types.ts +26 -0
- package/src/functions/tasks/index.ts +11 -0
- package/src/functions/tasks/read.ts +35 -0
- package/src/functions/tasks/task-list.test.ts +99 -0
- package/src/functions/tasks/task-list.ts +165 -0
- package/src/functions/tasks/update.ts +53 -0
- package/src/index.ts +9 -0
- package/src/initiative/Initiative.ts +99 -0
- package/src/initiative/InitiativeSchema.ts +37 -0
- package/src/initiative/functions/agent.ts +57 -0
- package/src/initiative/functions/getContext.ts +74 -0
- package/src/initiative/functions/index.ts +7 -0
- package/src/initiative/functions/update.ts +63 -0
- package/src/initiative/index.ts +3 -0
- package/src/initiative/initiative.conversations.json +1 -0
- package/src/initiative/initiative.test.ts +485 -0
- package/src/sync/index.ts +5 -0
- package/src/sync/sync.ts +95 -0
- package/src/testing/index.ts +5 -0
- package/src/testing/plugins.tsx +68 -0
- package/src/toolkits/AssistantToolkit.conversations.json +1 -0
- package/src/toolkits/AssistantToolkit.test.ts +94 -0
- package/src/toolkits/AssistantToolkit.ts +70 -0
- package/src/toolkits/SystemToolkit.ts +299 -0
- package/src/toolkits/index.ts +6 -0
- package/src/typedefs.d.ts +8 -0
- package/src/util/graphql.ts +31 -0
- package/src/util/index.ts +5 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Array from 'effect/Array';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Function from 'effect/Function';
|
|
8
|
+
import * as Match from 'effect/Match';
|
|
9
|
+
import * as Option from 'effect/Option';
|
|
10
|
+
import * as Schema from 'effect/Schema';
|
|
11
|
+
|
|
12
|
+
import { AiService, ConsolePrinter, ModelName } from '@dxos/ai';
|
|
13
|
+
import { AiSession, GenerationObserver, createToolkit } from '@dxos/assistant';
|
|
14
|
+
import { Prompt, Template } from '@dxos/blueprints';
|
|
15
|
+
import { Obj, Ref, Type } from '@dxos/echo';
|
|
16
|
+
import { Database } from '@dxos/echo';
|
|
17
|
+
import { TracingService, defineFunction } from '@dxos/functions';
|
|
18
|
+
import { log } from '@dxos/log';
|
|
19
|
+
|
|
20
|
+
const DEFAULT_MODEL: ModelName = '@anthropic/claude-opus-4-0';
|
|
21
|
+
|
|
22
|
+
export default defineFunction({
|
|
23
|
+
key: 'dxos.org/function/prompt',
|
|
24
|
+
name: 'Agent',
|
|
25
|
+
description: 'Agentic worker that executes a provided prompt using blueprints and tools.',
|
|
26
|
+
inputSchema: Schema.Struct({
|
|
27
|
+
prompt: Type.Ref(Prompt.Prompt),
|
|
28
|
+
systemPrompt: Type.Ref(Prompt.Prompt).pipe(Schema.optional),
|
|
29
|
+
/**
|
|
30
|
+
* @default @anthropic/claude-opus-4-0
|
|
31
|
+
*/
|
|
32
|
+
model: Schema.optional(ModelName),
|
|
33
|
+
/**
|
|
34
|
+
* Input object or data.
|
|
35
|
+
* References get auto-resolved.
|
|
36
|
+
*/
|
|
37
|
+
input: Schema.Any.pipe(Schema.annotations({ title: 'Input' })),
|
|
38
|
+
}),
|
|
39
|
+
outputSchema: Schema.Any,
|
|
40
|
+
handler: Effect.fnUntraced(function* ({ data }) {
|
|
41
|
+
log.info('processing input', { input: data.input });
|
|
42
|
+
|
|
43
|
+
// TODO(wittjosiah): Support templated object as input.
|
|
44
|
+
// Currently the object templating only supports direct pass-through or strings.
|
|
45
|
+
// const input = { ...data.input };
|
|
46
|
+
// for (const key of Object.keys(data.input)) {
|
|
47
|
+
// const value = data.input[key];
|
|
48
|
+
// if (Ref.isRef(value)) {
|
|
49
|
+
// const object = yield* Database.Service.load(value);
|
|
50
|
+
// input[key] = Obj.toJSON(object);
|
|
51
|
+
// } else {
|
|
52
|
+
// input[key] = JSON.stringify(value);
|
|
53
|
+
// }
|
|
54
|
+
// }
|
|
55
|
+
const input = yield* Match.value(data.input).pipe(
|
|
56
|
+
Match.when(
|
|
57
|
+
(value: any) => Ref.isRef(value),
|
|
58
|
+
Effect.fnUntraced(function* (ref) {
|
|
59
|
+
const object = yield* Database.Service.load(ref);
|
|
60
|
+
return Obj.toJSON(object as Obj.Unknown);
|
|
61
|
+
}),
|
|
62
|
+
),
|
|
63
|
+
Match.orElse(() => Effect.succeed(data.input)),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
yield* Database.Service.flush({ indexes: true });
|
|
67
|
+
const prompt = yield* Database.Service.load(data.prompt);
|
|
68
|
+
const systemPrompt = data.systemPrompt ? yield* Database.Service.load(data.systemPrompt) : undefined;
|
|
69
|
+
yield* TracingService.emitStatus({ message: `Running ${prompt.id}` });
|
|
70
|
+
|
|
71
|
+
log.info('starting agent', { prompt: prompt.id, input });
|
|
72
|
+
|
|
73
|
+
const blueprints = yield* Function.pipe(
|
|
74
|
+
prompt.blueprints,
|
|
75
|
+
Array.appendAll(systemPrompt?.blueprints ?? []),
|
|
76
|
+
Effect.forEach(Database.Service.loadOption),
|
|
77
|
+
Effect.map(Array.filter(Option.isSome)),
|
|
78
|
+
Effect.map(Array.map((option) => option.value)),
|
|
79
|
+
);
|
|
80
|
+
const toolkit = yield* createToolkit({ blueprints });
|
|
81
|
+
|
|
82
|
+
const objects = yield* Function.pipe(
|
|
83
|
+
prompt.context,
|
|
84
|
+
Array.appendAll(systemPrompt?.context ?? []),
|
|
85
|
+
Effect.forEach(Database.Service.loadOption),
|
|
86
|
+
Effect.map(Array.filter(Option.isSome)),
|
|
87
|
+
Effect.map(Array.map((option) => option.value)),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const promptInstructions = yield* Database.Service.load(prompt.instructions.source);
|
|
91
|
+
const promptText = Template.process(promptInstructions.content, input);
|
|
92
|
+
|
|
93
|
+
const systemInstructions = systemPrompt
|
|
94
|
+
? yield* Database.Service.load(systemPrompt.instructions.source)
|
|
95
|
+
: undefined;
|
|
96
|
+
const systemText = systemInstructions ? Template.process(systemInstructions.content, {}) : undefined;
|
|
97
|
+
|
|
98
|
+
const session = new AiSession();
|
|
99
|
+
const result = yield* session
|
|
100
|
+
.run({
|
|
101
|
+
prompt: promptText,
|
|
102
|
+
system: systemText,
|
|
103
|
+
blueprints,
|
|
104
|
+
objects: objects as Obj.Unknown[],
|
|
105
|
+
toolkit,
|
|
106
|
+
observer: GenerationObserver.fromPrinter(new ConsolePrinter({ tag: 'agent' })),
|
|
107
|
+
})
|
|
108
|
+
.pipe(Effect.provide(AiService.model(data.model ?? DEFAULT_MODEL)));
|
|
109
|
+
const lastBlock = result
|
|
110
|
+
.at(-1)
|
|
111
|
+
?.blocks.filter((block) => block._tag === 'text')
|
|
112
|
+
.at(-1);
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
note: lastBlock?.text,
|
|
116
|
+
};
|
|
117
|
+
}),
|
|
118
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Toolkit from '@effect/ai/Toolkit';
|
|
6
|
+
import * as FetchHttpClient from '@effect/platform/FetchHttpClient';
|
|
7
|
+
import { describe, it } from '@effect/vitest';
|
|
8
|
+
import * as Config from 'effect/Config';
|
|
9
|
+
import * as Effect from 'effect/Effect';
|
|
10
|
+
import * as Layer from 'effect/Layer';
|
|
11
|
+
|
|
12
|
+
import { AiService } from '@dxos/ai';
|
|
13
|
+
import { AiServiceTestingPreset } from '@dxos/ai/testing';
|
|
14
|
+
import { makeToolExecutionServiceFromFunctions, makeToolResolverFromFunctions } from '@dxos/assistant';
|
|
15
|
+
import { TestHelpers } from '@dxos/effect/testing';
|
|
16
|
+
import { CredentialsService, FunctionInvocationService, TracingService } from '@dxos/functions';
|
|
17
|
+
import { FunctionInvocationServiceLayerTestMocked, TestDatabaseLayer } from '@dxos/functions-runtime/testing';
|
|
18
|
+
|
|
19
|
+
import { default as fetchMessages } from './fetch-messages';
|
|
20
|
+
|
|
21
|
+
const TestLayer = Layer.mergeAll(
|
|
22
|
+
AiService.model('@anthropic/claude-opus-4-0'),
|
|
23
|
+
makeToolResolverFromFunctions([], Toolkit.make()),
|
|
24
|
+
makeToolExecutionServiceFromFunctions(Toolkit.make() as any, Layer.empty as any),
|
|
25
|
+
).pipe(
|
|
26
|
+
Layer.provideMerge(
|
|
27
|
+
Layer.mergeAll(
|
|
28
|
+
AiServiceTestingPreset('direct'),
|
|
29
|
+
TestDatabaseLayer({}),
|
|
30
|
+
CredentialsService.layerConfig([{ service: 'discord.com', apiKey: Config.redacted('DISCORD_TOKEN') }]),
|
|
31
|
+
FetchHttpClient.layer,
|
|
32
|
+
FunctionInvocationServiceLayerTestMocked({ functions: [fetchMessages] }).pipe(
|
|
33
|
+
Layer.provideMerge(TracingService.layerNoop),
|
|
34
|
+
),
|
|
35
|
+
),
|
|
36
|
+
),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const DXOS_SERVER_ID = '837138313172353095';
|
|
40
|
+
|
|
41
|
+
describe('Feed', { timeout: 600_000 }, () => {
|
|
42
|
+
it.effect(
|
|
43
|
+
'fetch discord messages',
|
|
44
|
+
Effect.fnUntraced(
|
|
45
|
+
function* (_) {
|
|
46
|
+
const messages = yield* FunctionInvocationService.invokeFunction(fetchMessages, {
|
|
47
|
+
serverId: DXOS_SERVER_ID,
|
|
48
|
+
// channelId: '1404487604761526423',
|
|
49
|
+
last: '7d',
|
|
50
|
+
});
|
|
51
|
+
console.log(messages);
|
|
52
|
+
},
|
|
53
|
+
Effect.provide(TestLayer),
|
|
54
|
+
TestHelpers.provideTestContext,
|
|
55
|
+
TestHelpers.taggedTest('sync'),
|
|
56
|
+
),
|
|
57
|
+
);
|
|
58
|
+
});
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as FetchHttpClient from '@effect/platform/FetchHttpClient';
|
|
6
|
+
import { DiscordConfig, DiscordREST, DiscordRESTMemoryLive } from 'dfx';
|
|
7
|
+
import type {
|
|
8
|
+
GuildChannelResponse,
|
|
9
|
+
MessageResponse,
|
|
10
|
+
PrivateChannelResponse,
|
|
11
|
+
PrivateGroupChannelResponse,
|
|
12
|
+
ThreadResponse,
|
|
13
|
+
} from 'dfx/types';
|
|
14
|
+
import * as Array from 'effect/Array';
|
|
15
|
+
import * as Effect from 'effect/Effect';
|
|
16
|
+
import * as Function from 'effect/Function';
|
|
17
|
+
import * as Layer from 'effect/Layer';
|
|
18
|
+
import * as Option from 'effect/Option';
|
|
19
|
+
import * as Schema from 'effect/Schema';
|
|
20
|
+
|
|
21
|
+
import { Obj } from '@dxos/echo';
|
|
22
|
+
import { CredentialsService, TracingService, defineFunction } from '@dxos/functions';
|
|
23
|
+
import { log } from '@dxos/log';
|
|
24
|
+
import { Message } from '@dxos/types';
|
|
25
|
+
|
|
26
|
+
// TODO(dmaretskyi): Extract.
|
|
27
|
+
const TimeRange = class extends Schema.String.pipe(Schema.pattern(/\d+(s|m|h|d)/)).annotations({
|
|
28
|
+
description: 'Time range. 1d - 1 day, 2h - 2 hours, 30m - 30 minutes, 15s - 15 seconds.',
|
|
29
|
+
examples: ['1d', '2h', '30m', '15s'],
|
|
30
|
+
}) {
|
|
31
|
+
static toSeconds(timeRange: Schema.Schema.Type<typeof TimeRange>) {
|
|
32
|
+
const match = timeRange.match(/(\d+)(s|m|h|d)/);
|
|
33
|
+
if (!match) {
|
|
34
|
+
throw new Error(`Invalid time range: ${timeRange}`);
|
|
35
|
+
}
|
|
36
|
+
const [_, amount, unit] = match;
|
|
37
|
+
switch (unit) {
|
|
38
|
+
case 's':
|
|
39
|
+
return Number(amount);
|
|
40
|
+
case 'm':
|
|
41
|
+
return Number(amount) * 60;
|
|
42
|
+
case 'h':
|
|
43
|
+
return Number(amount) * 60 * 60;
|
|
44
|
+
case 'd':
|
|
45
|
+
return Number(amount) * 24 * 60 * 60;
|
|
46
|
+
default:
|
|
47
|
+
throw new Error(`Invalid time range unit: ${unit}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
type TimeRange = Schema.Schema.Type<typeof TimeRange>;
|
|
52
|
+
|
|
53
|
+
const DiscordConfigFromCredential = Layer.unwrapEffect(
|
|
54
|
+
Effect.gen(function* () {
|
|
55
|
+
return DiscordConfig.layer({
|
|
56
|
+
token: yield* CredentialsService.getApiKey({ service: 'discord.com' }),
|
|
57
|
+
rest: {
|
|
58
|
+
baseUrl: 'https://api-proxy.dxos.workers.dev/discord.com/api/v10',
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
type DiscordChannel = GuildChannelResponse | PrivateChannelResponse | PrivateGroupChannelResponse | ThreadResponse;
|
|
65
|
+
|
|
66
|
+
const DEFAULT_AFTER = 1704067200; // 2024-01-01
|
|
67
|
+
const DEFAULT_LIMIT = 500;
|
|
68
|
+
const DEFAULT_IGNORE_USERNAMES = ['GitHub', 'Needle'];
|
|
69
|
+
|
|
70
|
+
// TODO(dmaretskyi): Align with standard thread type.
|
|
71
|
+
type Thread = {
|
|
72
|
+
discordChannelId: string;
|
|
73
|
+
name?: string;
|
|
74
|
+
messages: Message.Message[];
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export default defineFunction({
|
|
78
|
+
key: 'dxos.org/function/fetch-discord-messages',
|
|
79
|
+
name: 'Sync Discord messages',
|
|
80
|
+
inputSchema: Schema.Struct({
|
|
81
|
+
serverId: Schema.String.annotations({
|
|
82
|
+
description: 'The ID of the server to fetch messages from.',
|
|
83
|
+
}),
|
|
84
|
+
channelId: Schema.optional(Schema.String).annotations({
|
|
85
|
+
description:
|
|
86
|
+
'The ID of the channel to fetch messages from. Will crawl all channels from the server if not specified.',
|
|
87
|
+
}),
|
|
88
|
+
after: Schema.optional(Schema.Number).annotations({
|
|
89
|
+
description:
|
|
90
|
+
'Fetch messages that were sent after a given date. Unix timestamp in seconds. Exclusive with `last`.',
|
|
91
|
+
}),
|
|
92
|
+
last: TimeRange.annotations({
|
|
93
|
+
description:
|
|
94
|
+
'Time range to fetch most recent messages. Specifies the range in the past, from now. "1d" would fetch messages from the last 24 hours.',
|
|
95
|
+
}),
|
|
96
|
+
limit: Schema.optional(Schema.Number).annotations({
|
|
97
|
+
description: 'The maximum number of messages to fetch.',
|
|
98
|
+
}),
|
|
99
|
+
pageSize: Schema.optional(Schema.Number).annotations({
|
|
100
|
+
description: 'The number of messages to fetch per page.',
|
|
101
|
+
}),
|
|
102
|
+
ignoreUsernames: Schema.optional(Schema.Array(Schema.String)).annotations({
|
|
103
|
+
description: 'Exclude messages from these usernames.',
|
|
104
|
+
}),
|
|
105
|
+
}),
|
|
106
|
+
handler: Effect.fnUntraced(
|
|
107
|
+
function* ({
|
|
108
|
+
data: {
|
|
109
|
+
serverId,
|
|
110
|
+
channelId,
|
|
111
|
+
after,
|
|
112
|
+
last,
|
|
113
|
+
pageSize = 100,
|
|
114
|
+
limit = DEFAULT_LIMIT,
|
|
115
|
+
ignoreUsernames = DEFAULT_IGNORE_USERNAMES,
|
|
116
|
+
},
|
|
117
|
+
}) {
|
|
118
|
+
if (!after && !last) {
|
|
119
|
+
throw new Error('cannot specify both `after` and `last`');
|
|
120
|
+
}
|
|
121
|
+
const afterTs = last ? Date.now() / 1000 - TimeRange.toSeconds(last) : (after ?? DEFAULT_AFTER);
|
|
122
|
+
|
|
123
|
+
const rest = yield* DiscordREST;
|
|
124
|
+
|
|
125
|
+
let channels: DiscordChannel[] = [];
|
|
126
|
+
channels.push(...(yield* rest.listGuildChannels(serverId)));
|
|
127
|
+
const { threads: guildThreads } = yield* rest.getActiveGuildThreads(serverId);
|
|
128
|
+
channels.push(...guildThreads);
|
|
129
|
+
if (channelId) {
|
|
130
|
+
channels = channels.filter((channel) => channel.id === channelId);
|
|
131
|
+
}
|
|
132
|
+
if (channels.length === 0) {
|
|
133
|
+
throw new Error('no channels found');
|
|
134
|
+
}
|
|
135
|
+
for (const channel of channels) {
|
|
136
|
+
log.info('channel', { id: channel.id, name: 'name' in channel ? channel.name : undefined });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
yield* TracingService.emitStatus({ message: `Will fetch from channels: ${channels.length}` });
|
|
140
|
+
|
|
141
|
+
const threads = yield* Effect.forEach(
|
|
142
|
+
channels,
|
|
143
|
+
Effect.fnUntraced(function* (channel) {
|
|
144
|
+
const allMessages: Message.Message[] = [];
|
|
145
|
+
|
|
146
|
+
let lastMessage: Option.Option<Message.Message> = Option.none();
|
|
147
|
+
while (true) {
|
|
148
|
+
const { id: lastId = undefined } = Function.pipe(
|
|
149
|
+
lastMessage,
|
|
150
|
+
Option.map(Obj.getKeys('discord.com')),
|
|
151
|
+
Option.flatMap(Option.fromIterable),
|
|
152
|
+
Option.getOrElse(() => ({ id: undefined })),
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const options = {
|
|
156
|
+
after: !lastId ? `${generateSnowflake(afterTs)}` : lastId,
|
|
157
|
+
limit: pageSize,
|
|
158
|
+
};
|
|
159
|
+
log.info('fetching messages', {
|
|
160
|
+
lastId,
|
|
161
|
+
afterTs,
|
|
162
|
+
afterSnowflake: options.after,
|
|
163
|
+
after: parseSnowflake(options.after),
|
|
164
|
+
limit: options.limit,
|
|
165
|
+
});
|
|
166
|
+
const messages = yield* rest.listMessages(channel.id, options).pipe(
|
|
167
|
+
Effect.map(Array.map(makeMessage)),
|
|
168
|
+
Effect.map(Array.reverse),
|
|
169
|
+
Effect.catchTag('ErrorResponse', (err) =>
|
|
170
|
+
err.cause.code === 50001 ? Effect.succeed([]) : Effect.fail(err),
|
|
171
|
+
),
|
|
172
|
+
);
|
|
173
|
+
if (messages.length > 0) {
|
|
174
|
+
lastMessage = Option.fromNullable(messages.at(-1));
|
|
175
|
+
allMessages.push(...messages);
|
|
176
|
+
} else {
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
yield* TracingService.emitStatus({ message: `Fetched messages: ${allMessages.length}` });
|
|
180
|
+
if (allMessages.length >= limit) {
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
discordChannelId: channel.id,
|
|
187
|
+
name: 'name' in channel ? (channel.name ?? undefined) : undefined,
|
|
188
|
+
messages: allMessages
|
|
189
|
+
.filter((message) => !message.sender.name || !ignoreUsernames.includes(message.sender.name))
|
|
190
|
+
.filter((message) =>
|
|
191
|
+
message.blocks.some((block: any) => block._tag === 'text' && block.text.trim().length > 0),
|
|
192
|
+
),
|
|
193
|
+
} satisfies Thread;
|
|
194
|
+
}),
|
|
195
|
+
{ concurrency: 10 },
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
return threads
|
|
199
|
+
.filter((thread) => thread.messages.length > 0)
|
|
200
|
+
.map(serializeThread)
|
|
201
|
+
.join('\n');
|
|
202
|
+
},
|
|
203
|
+
Effect.provide(
|
|
204
|
+
DiscordRESTMemoryLive.pipe(Layer.provideMerge(DiscordConfigFromCredential)).pipe(
|
|
205
|
+
Layer.provide(FetchHttpClient.layer),
|
|
206
|
+
),
|
|
207
|
+
),
|
|
208
|
+
Effect.orDie,
|
|
209
|
+
),
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* @param unixTimestamp in seconds
|
|
214
|
+
*/
|
|
215
|
+
const generateSnowflake = (unixTimestamp: number): bigint => {
|
|
216
|
+
const discordEpoch = 1420070400000n; // Discord Epoch (ms)
|
|
217
|
+
return (BigInt(unixTimestamp * 1000) - discordEpoch) << 22n;
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const parseSnowflake = (snowflake: string): Date => {
|
|
221
|
+
const discordEpoch = 1420070400000n; // Discord Epoch (ms)
|
|
222
|
+
return new Date(Number((BigInt(snowflake) >> 22n) + discordEpoch));
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
// TODO(burdon): Move to @dxos/types.
|
|
226
|
+
const makeMessage = (message: MessageResponse): Message.Message =>
|
|
227
|
+
Obj.make(Message.Message, {
|
|
228
|
+
[Obj.Meta]: {
|
|
229
|
+
keys: [
|
|
230
|
+
{ id: message.id, source: 'discord.com' },
|
|
231
|
+
{ id: message.channel_id, source: 'discord.com/thread' },
|
|
232
|
+
],
|
|
233
|
+
},
|
|
234
|
+
sender: { name: message.author.username },
|
|
235
|
+
created: message.timestamp,
|
|
236
|
+
blocks: [{ _tag: 'text', text: message.content }],
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Standard JSON serialization is to verbose for large amounts of data.
|
|
241
|
+
*/
|
|
242
|
+
const serializeThread = (thread: Thread): string => {
|
|
243
|
+
return `<thread id=${thread.discordChannelId} name=${thread.name ?? ''}>\n${thread.messages
|
|
244
|
+
.map(
|
|
245
|
+
(message) =>
|
|
246
|
+
` ${message.sender.name}: ${message.blocks
|
|
247
|
+
.filter((block: any) => block._tag === 'text')
|
|
248
|
+
.map((block: any) => block.text)
|
|
249
|
+
.join(' ')}`,
|
|
250
|
+
)
|
|
251
|
+
.join('\n')}\n</thread>`;
|
|
252
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Schema from 'effect/Schema';
|
|
7
|
+
|
|
8
|
+
import { ArtifactId } from '@dxos/assistant';
|
|
9
|
+
import { Database } from '@dxos/echo';
|
|
10
|
+
import { defineFunction } from '@dxos/functions';
|
|
11
|
+
import { Markdown } from '@dxos/plugin-markdown/types';
|
|
12
|
+
|
|
13
|
+
export default defineFunction({
|
|
14
|
+
key: 'dxos.org/function/markdown/read',
|
|
15
|
+
name: 'Read markdown document',
|
|
16
|
+
description: 'Read markdown document.',
|
|
17
|
+
inputSchema: Schema.Struct({
|
|
18
|
+
id: ArtifactId.annotations({
|
|
19
|
+
description: 'The ID of the document to read.',
|
|
20
|
+
}),
|
|
21
|
+
}),
|
|
22
|
+
outputSchema: Schema.Struct({
|
|
23
|
+
content: Schema.String,
|
|
24
|
+
}),
|
|
25
|
+
handler: Effect.fn(function* ({ data: { id } }) {
|
|
26
|
+
const doc = yield* Database.Service.resolve(ArtifactId.toDXN(id), Markdown.Document);
|
|
27
|
+
const { content } = yield* Database.Service.load(doc.content);
|
|
28
|
+
return { content };
|
|
29
|
+
}),
|
|
30
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Schema from 'effect/Schema';
|
|
7
|
+
|
|
8
|
+
import { ArtifactId } from '@dxos/assistant';
|
|
9
|
+
import { Database, Obj } from '@dxos/echo';
|
|
10
|
+
import { defineFunction } from '@dxos/functions';
|
|
11
|
+
import { Markdown } from '@dxos/plugin-markdown/types';
|
|
12
|
+
|
|
13
|
+
export default defineFunction({
|
|
14
|
+
key: 'dxos.org/function/markdown/update',
|
|
15
|
+
name: 'Update markdown',
|
|
16
|
+
description: 'Updates the entire contents of the markdown document.',
|
|
17
|
+
inputSchema: Schema.Struct({
|
|
18
|
+
id: ArtifactId.annotations({
|
|
19
|
+
description: 'The ID of the document to write.',
|
|
20
|
+
}),
|
|
21
|
+
content: Schema.String.annotations({
|
|
22
|
+
description: 'New content to write to the document.',
|
|
23
|
+
}),
|
|
24
|
+
}),
|
|
25
|
+
outputSchema: Schema.Void,
|
|
26
|
+
handler: Effect.fn(function* ({ data: { id, content } }) {
|
|
27
|
+
const doc = yield* Database.Service.resolve(ArtifactId.toDXN(id), Markdown.Document);
|
|
28
|
+
const text = yield* Database.Service.load(doc.content);
|
|
29
|
+
Obj.change(text, (t) => {
|
|
30
|
+
t.content = content;
|
|
31
|
+
});
|
|
32
|
+
}),
|
|
33
|
+
});
|