@do-kit/agents 0.2.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 友人A
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # @do-kit/agents
2
+
3
+ Lightweight utilities and helpers maintained in the `agents` package.
4
+
5
+ Install
6
+
7
+ ```bash
8
+ pnpm add @do-kit/agents
9
+ ```
10
+
11
+ Usage
12
+
13
+ ```ts
14
+ import { createAugmentResponseFromRequest } from '@do-kit/agents';
15
+
16
+ // The request body may reference an external provider like `deepseek`.
17
+ // You do NOT need to install `@ai-sdk/deepseek` unless you want to use it locally.
18
+ const body = {
19
+ provider: 'deepseek',
20
+ model: 'deepseek-chat',
21
+ messages: [{ role: 'user', content: 'Hello' }],
22
+ };
23
+
24
+ const response = await createAugmentResponseFromRequest(body);
25
+ // `response` is a UI-compatible stream response
26
+ ```
27
+
28
+ Development
29
+
30
+ ```bash
31
+ pnpm -w -F @do-kit/agents build
32
+ ```
@@ -0,0 +1,102 @@
1
+ 'use strict';
2
+
3
+ var ai = require('ai');
4
+ require('@ai-sdk/deepseek');
5
+
6
+ const version$1 = "0.2.0";
7
+
8
+ const version = version$1;
9
+
10
+ function injectDocumentStateMessages(messages) {
11
+ return messages.flatMap((message) => {
12
+ if (message.role === "user" && message.metadata?.documentState) {
13
+ const documentState = message.metadata.documentState;
14
+ return [
15
+ {
16
+ role: "assistant",
17
+ id: `assistant-document-state-${message.id}`,
18
+ parts: [
19
+ ...documentState.selection ? [
20
+ {
21
+ type: "text",
22
+ text: `This is the latest state of the selection (ignore previous selections, you MUST issue operations against this latest version of the selection): ${JSON.stringify(
23
+ documentState.selectedBlocks
24
+ )}`
25
+ },
26
+ {
27
+ type: "text",
28
+ text: JSON.stringify(documentState.selectedBlocks)
29
+ },
30
+ {
31
+ type: "text",
32
+ text: `This is the latest state of the entire document (INCLUDING the selected text),
33
+ you can use this to find the selected text to understand the context (but you MUST NOT issue operations against this document, you MUST issue operations against the selection): ${JSON.stringify(
34
+ documentState.blocks
35
+ )}`
36
+ },
37
+ {
38
+ type: "text",
39
+ text: JSON.stringify(documentState.blocks)
40
+ }
41
+ ] : [
42
+ {
43
+ type: "text",
44
+ text: `There is no active selection. This is the latest state of the document (ignore previous documents, you MUST issue operations against this latest version of the document).
45
+ The cursor is BETWEEN two blocks as indicated by cursor: true.
46
+ ${documentState.isEmptyDocument ? `Because the document is empty, YOU MUST first update the empty block before adding new blocks.` : "Prefer updating existing blocks over removing and adding (but this also depends on the user's question)."}`
47
+ },
48
+ {
49
+ type: "text",
50
+ text: JSON.stringify(documentState.blocks)
51
+ }
52
+ ]
53
+ ]
54
+ },
55
+ message
56
+ ];
57
+ }
58
+ return [message];
59
+ });
60
+ }
61
+ function toolDefinitionsToToolSet(toolDefinitions) {
62
+ return Object.fromEntries(
63
+ Object.entries(toolDefinitions).map(([name, definition]) => [
64
+ name,
65
+ ai.tool({
66
+ ...definition,
67
+ inputSchema: ai.jsonSchema(definition.inputSchema),
68
+ outputSchema: ai.jsonSchema(definition.outputSchema)
69
+ })
70
+ ])
71
+ );
72
+ }
73
+ const systemPrompt = `
74
+ You're manipulating a text document using HTML blocks.
75
+ Make sure to follow the json schema provided. When referencing ids they MUST be EXACTLY the same (including the trailing $).
76
+ List items are 1 block with 1 list item each, so block content \`<ul><li>item1</li></ul>\` is valid, but \`<ul><li>item1</li><li>item2</li></ul>\` is invalid. We'll merge them automatically.
77
+ For code blocks, you can use the \`data-language\` attribute on a <code> block (wrapped with <pre>) to specify the language.
78
+
79
+ If the user requests updates to the document, use the "applyDocumentOperations" tool to update the document.
80
+ ---
81
+ IF there is no selection active in the latest state, first, determine what part of the document the user is talking about. You SHOULD probably take cursor info into account if needed.
82
+ EXAMPLE: if user says "below" (without pointing to a specific part of the document) he / she probably indicates the block(s) after the cursor.
83
+ EXAMPLE: If you want to insert content AT the cursor position (UNLESS indicated otherwise by the user), then you need \`referenceId\` to point to the block before the cursor with position \`after\` (or block below and \`before\`
84
+ ---
85
+ `;
86
+
87
+ const augmentAgent = ({
88
+ model,
89
+ messages,
90
+ toolDefinitions
91
+ }) => {
92
+ return ai.streamText({
93
+ model,
94
+ system: systemPrompt,
95
+ messages: ai.convertToModelMessages(injectDocumentStateMessages(messages)),
96
+ tools: toolDefinitionsToToolSet(toolDefinitions),
97
+ toolChoice: "required"
98
+ });
99
+ };
100
+
101
+ exports.augmentAgent = augmentAgent;
102
+ exports.version = version;
@@ -0,0 +1,99 @@
1
+ import { tool, jsonSchema, streamText, convertToModelMessages } from 'ai';
2
+ import '@ai-sdk/deepseek';
3
+
4
+ const version$1 = "0.2.0";
5
+
6
+ const version = version$1;
7
+
8
+ function injectDocumentStateMessages(messages) {
9
+ return messages.flatMap((message) => {
10
+ if (message.role === "user" && message.metadata?.documentState) {
11
+ const documentState = message.metadata.documentState;
12
+ return [
13
+ {
14
+ role: "assistant",
15
+ id: `assistant-document-state-${message.id}`,
16
+ parts: [
17
+ ...documentState.selection ? [
18
+ {
19
+ type: "text",
20
+ text: `This is the latest state of the selection (ignore previous selections, you MUST issue operations against this latest version of the selection): ${JSON.stringify(
21
+ documentState.selectedBlocks
22
+ )}`
23
+ },
24
+ {
25
+ type: "text",
26
+ text: JSON.stringify(documentState.selectedBlocks)
27
+ },
28
+ {
29
+ type: "text",
30
+ text: `This is the latest state of the entire document (INCLUDING the selected text),
31
+ you can use this to find the selected text to understand the context (but you MUST NOT issue operations against this document, you MUST issue operations against the selection): ${JSON.stringify(
32
+ documentState.blocks
33
+ )}`
34
+ },
35
+ {
36
+ type: "text",
37
+ text: JSON.stringify(documentState.blocks)
38
+ }
39
+ ] : [
40
+ {
41
+ type: "text",
42
+ text: `There is no active selection. This is the latest state of the document (ignore previous documents, you MUST issue operations against this latest version of the document).
43
+ The cursor is BETWEEN two blocks as indicated by cursor: true.
44
+ ${documentState.isEmptyDocument ? `Because the document is empty, YOU MUST first update the empty block before adding new blocks.` : "Prefer updating existing blocks over removing and adding (but this also depends on the user's question)."}`
45
+ },
46
+ {
47
+ type: "text",
48
+ text: JSON.stringify(documentState.blocks)
49
+ }
50
+ ]
51
+ ]
52
+ },
53
+ message
54
+ ];
55
+ }
56
+ return [message];
57
+ });
58
+ }
59
+ function toolDefinitionsToToolSet(toolDefinitions) {
60
+ return Object.fromEntries(
61
+ Object.entries(toolDefinitions).map(([name, definition]) => [
62
+ name,
63
+ tool({
64
+ ...definition,
65
+ inputSchema: jsonSchema(definition.inputSchema),
66
+ outputSchema: jsonSchema(definition.outputSchema)
67
+ })
68
+ ])
69
+ );
70
+ }
71
+ const systemPrompt = `
72
+ You're manipulating a text document using HTML blocks.
73
+ Make sure to follow the json schema provided. When referencing ids they MUST be EXACTLY the same (including the trailing $).
74
+ List items are 1 block with 1 list item each, so block content \`<ul><li>item1</li></ul>\` is valid, but \`<ul><li>item1</li><li>item2</li></ul>\` is invalid. We'll merge them automatically.
75
+ For code blocks, you can use the \`data-language\` attribute on a <code> block (wrapped with <pre>) to specify the language.
76
+
77
+ If the user requests updates to the document, use the "applyDocumentOperations" tool to update the document.
78
+ ---
79
+ IF there is no selection active in the latest state, first, determine what part of the document the user is talking about. You SHOULD probably take cursor info into account if needed.
80
+ EXAMPLE: if user says "below" (without pointing to a specific part of the document) he / she probably indicates the block(s) after the cursor.
81
+ EXAMPLE: If you want to insert content AT the cursor position (UNLESS indicated otherwise by the user), then you need \`referenceId\` to point to the block before the cursor with position \`after\` (or block below and \`before\`
82
+ ---
83
+ `;
84
+
85
+ const augmentAgent = ({
86
+ model,
87
+ messages,
88
+ toolDefinitions
89
+ }) => {
90
+ return streamText({
91
+ model,
92
+ system: systemPrompt,
93
+ messages: convertToModelMessages(injectDocumentStateMessages(messages)),
94
+ tools: toolDefinitionsToToolSet(toolDefinitions),
95
+ toolChoice: "required"
96
+ });
97
+ };
98
+
99
+ export { augmentAgent, version };
@@ -0,0 +1,13 @@
1
+ import { streamText } from 'ai';
2
+
3
+ declare const version: string;
4
+
5
+ type AugmentOptions = {
6
+ model: any;
7
+ messages: any[];
8
+ toolDefinitions?: Record<string, any>;
9
+ };
10
+ type CreateAugmentAgentResult = ReturnType<typeof streamText>;
11
+ declare const augmentAgent: ({ model, messages, toolDefinitions, }: AugmentOptions) => CreateAugmentAgentResult;
12
+
13
+ export { augmentAgent, version };
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@do-kit/agents",
3
+ "description": "@do-kit agents library",
4
+ "version": "0.2.1",
5
+ "main": "dist/cjs/index.js",
6
+ "module": "dist/esm/index.mjs",
7
+ "types": "dist/types/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "registry": "https://registry.npmjs.org/"
14
+ },
15
+ "devDependencies": {
16
+ "@rollup/plugin-json": "^6.1.0",
17
+ "rollup": "^4.34.7",
18
+ "rollup-plugin-dts": "^6.1.1",
19
+ "rollup-plugin-esbuild": "6.2.1",
20
+ "vitest": "^4.0.7",
21
+ "@do-kit/typescript-config": "0.2.0"
22
+ },
23
+ "scripts": {
24
+ "dev": "rollup -c rollup.config.mjs -w",
25
+ "build": "rimraf dist && rollup -c rollup.config.mjs",
26
+ "clean": "rimraf .turbo node_modules dist",
27
+ "test": "vitest run --color",
28
+ "test:cov": "vitest run --color --coverage"
29
+ }
30
+ }