@grisaiaevy/crafting-agent 0.0.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 +21 -0
- package/README.md +159 -0
- package/dist/image/api.d.ts +3 -0
- package/dist/image/api.js +23 -0
- package/dist/image/api.js.map +1 -0
- package/dist/image/index.d.ts +2 -0
- package/dist/image/index.js +19 -0
- package/dist/image/index.js.map +1 -0
- package/dist/image/types.d.ts +8 -0
- package/dist/image/types.js +3 -0
- package/dist/image/types.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/logger/index.d.ts +1 -0
- package/dist/logger/index.js +7 -0
- package/dist/logger/index.js.map +1 -0
- package/dist/logger/logger.d.ts +3 -0
- package/dist/logger/logger.js +23 -0
- package/dist/logger/logger.js.map +1 -0
- package/dist/prompt/index.d.ts +2 -0
- package/dist/prompt/index.js +6 -0
- package/dist/prompt/index.js.map +1 -0
- package/dist/prompt/prompt.d.ts +2 -0
- package/dist/prompt/prompt.js +68 -0
- package/dist/prompt/prompt.js.map +1 -0
- package/dist/providers/doubao.d.ts +10 -0
- package/dist/providers/doubao.js +76 -0
- package/dist/providers/doubao.js.map +1 -0
- package/dist/providers/google.d.ts +12 -0
- package/dist/providers/google.js +70 -0
- package/dist/providers/google.js.map +1 -0
- package/dist/providers/index.d.ts +6 -0
- package/dist/providers/index.js +23 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/message_converer.d.ts +4 -0
- package/dist/providers/message_converer.js +206 -0
- package/dist/providers/message_converer.js.map +1 -0
- package/dist/providers/minimax.d.ts +10 -0
- package/dist/providers/minimax.js +120 -0
- package/dist/providers/minimax.js.map +1 -0
- package/dist/providers/openrouter.d.ts +17 -0
- package/dist/providers/openrouter.js +178 -0
- package/dist/providers/openrouter.js.map +1 -0
- package/dist/providers/stream.d.ts +1 -0
- package/dist/providers/stream.js +18 -0
- package/dist/providers/stream.js.map +1 -0
- package/dist/storage/index.d.ts +1 -0
- package/dist/storage/index.js +6 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/task-storage.d.ts +13 -0
- package/dist/storage/task-storage.js +7 -0
- package/dist/storage/task-storage.js.map +1 -0
- package/dist/task/converter.d.ts +4 -0
- package/dist/task/converter.js +66 -0
- package/dist/task/converter.js.map +1 -0
- package/dist/task/index.d.ts +5 -0
- package/dist/task/index.js +11 -0
- package/dist/task/index.js.map +1 -0
- package/dist/task/task_executor.d.ts +22 -0
- package/dist/task/task_executor.js +216 -0
- package/dist/task/task_executor.js.map +1 -0
- package/dist/task/types.d.ts +1 -0
- package/dist/task/types.js +18 -0
- package/dist/task/types.js.map +1 -0
- package/dist/tools/index.d.ts +6 -0
- package/dist/tools/index.js +23 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/replace_content.d.ts +9 -0
- package/dist/tools/replace_content.js +72 -0
- package/dist/tools/replace_content.js.map +1 -0
- package/dist/tools/tool_converter.d.ts +3 -0
- package/dist/tools/tool_converter.js +59 -0
- package/dist/tools/tool_converter.js.map +1 -0
- package/dist/tools/types.d.ts +1 -0
- package/dist/tools/types.js +18 -0
- package/dist/tools/types.js.map +1 -0
- package/dist/tools/update_todo_list.d.ts +13 -0
- package/dist/tools/update_todo_list.js +80 -0
- package/dist/tools/update_todo_list.js.map +1 -0
- package/dist/tools/web_search.d.ts +9 -0
- package/dist/tools/web_search.js +44 -0
- package/dist/tools/web_search.js.map +1 -0
- package/dist/tools/write_content.d.ts +9 -0
- package/dist/tools/write_content.js +49 -0
- package/dist/tools/write_content.js.map +1 -0
- package/dist/types/common.d.ts +14 -0
- package/dist/types/common.js +3 -0
- package/dist/types/common.js.map +1 -0
- package/dist/types/context.d.ts +10 -0
- package/dist/types/context.js +3 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.js +24 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/prompt.d.ts +5 -0
- package/dist/types/prompt.js +3 -0
- package/dist/types/prompt.js.map +1 -0
- package/dist/types/provider.d.ts +14 -0
- package/dist/types/provider.js +372 -0
- package/dist/types/provider.js.map +1 -0
- package/dist/types/stream.d.ts +59 -0
- package/dist/types/stream.js +3 -0
- package/dist/types/stream.js.map +1 -0
- package/dist/types/task.d.ts +46 -0
- package/dist/types/task.js +3 -0
- package/dist/types/task.js.map +1 -0
- package/dist/types/tools.d.ts +36 -0
- package/dist/types/tools.js +30 -0
- package/dist/types/tools.js.map +1 -0
- package/dist/utils/fetch_html.d.ts +1 -0
- package/dist/utils/fetch_html.js +24 -0
- package/dist/utils/fetch_html.js.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +18 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +58 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Task, Project, Config, ChatMessage } from '../types';
|
|
2
|
+
export declare abstract class TaskStorage {
|
|
3
|
+
abstract getTaskById(id: string): Promise<Task | null>;
|
|
4
|
+
abstract createOrUpdateTask(task: Task): Promise<Task | null>;
|
|
5
|
+
abstract getTasksByProjectId(projectId: string): Promise<Task[]>;
|
|
6
|
+
abstract getProjectById(id: string): Promise<Project | null>;
|
|
7
|
+
abstract createOrUpdateProject(project: Project): Promise<Project | null>;
|
|
8
|
+
abstract getAllProjects(): Promise<Project[]>;
|
|
9
|
+
abstract createConfig(data: Config): Promise<Config>;
|
|
10
|
+
abstract getConfigByKey(key: string, category?: string): Promise<Config | null>;
|
|
11
|
+
abstract createChatMessage(data: ChatMessage): Promise<ChatMessage>;
|
|
12
|
+
abstract getChatMessagesByTaskId(taskId: string): Promise<ChatMessage[]>;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"task-storage.js","sourceRoot":"","sources":["../../src/storage/task-storage.ts"],"names":[],"mappings":";;;AAEA,MAAsB,WAAW;CAyBhC;AAzBD,kCAyBC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ContentBlock } from "../types";
|
|
2
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
3
|
+
export declare function convertContentBlock(blocks: Anthropic.ContentBlockParam[]): ContentBlock[];
|
|
4
|
+
export declare function convertToAnthropicContentBlocks(blocks: ContentBlock[]): Anthropic.Messages.ContentBlockParam[];
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertContentBlock = convertContentBlock;
|
|
4
|
+
exports.convertToAnthropicContentBlocks = convertToAnthropicContentBlocks;
|
|
5
|
+
function convertContentBlock(blocks) {
|
|
6
|
+
return blocks.map((block) => {
|
|
7
|
+
const contentBlock = {
|
|
8
|
+
type: block.type,
|
|
9
|
+
};
|
|
10
|
+
if (block.type === 'text' && 'text' in block) {
|
|
11
|
+
contentBlock.text = block.text;
|
|
12
|
+
}
|
|
13
|
+
else if (block.type === 'tool_use' && 'id' in block) {
|
|
14
|
+
contentBlock.tool_use_id = block.id;
|
|
15
|
+
contentBlock.name = block.name;
|
|
16
|
+
contentBlock.input = block.input;
|
|
17
|
+
}
|
|
18
|
+
else if (block.type === 'thinking') {
|
|
19
|
+
contentBlock.content = block.thinking;
|
|
20
|
+
}
|
|
21
|
+
else if (block.type === 'tool_result') {
|
|
22
|
+
contentBlock.content = block.content;
|
|
23
|
+
contentBlock.tool_use_id = block.tool_use_id;
|
|
24
|
+
}
|
|
25
|
+
return contentBlock;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function convertToAnthropicContentBlocks(blocks) {
|
|
29
|
+
return blocks.map((block) => {
|
|
30
|
+
if (block.type === 'text' && block.text !== undefined) {
|
|
31
|
+
return {
|
|
32
|
+
type: 'text',
|
|
33
|
+
text: block.text,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
else if (block.type === 'tool_use' && block.tool_use_id && block.name) {
|
|
37
|
+
return {
|
|
38
|
+
type: 'tool_use',
|
|
39
|
+
id: block.tool_use_id,
|
|
40
|
+
name: block.name,
|
|
41
|
+
input: block.input || {},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
else if (block.type === 'tool_result' && block.tool_use_id && block.content !== undefined) {
|
|
45
|
+
return {
|
|
46
|
+
type: 'tool_result',
|
|
47
|
+
tool_use_id: block.tool_use_id,
|
|
48
|
+
content: typeof block.content === 'string' ? block.content : '',
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
else if (block.type === 'thinking') {
|
|
52
|
+
return {
|
|
53
|
+
type: 'thinking',
|
|
54
|
+
thinking: typeof block.content === 'string' ? block.content : '',
|
|
55
|
+
signature: '',
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
return {
|
|
60
|
+
type: 'text',
|
|
61
|
+
text: '',
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=converter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"converter.js","sourceRoot":"","sources":["../../src/task/converter.ts"],"names":[],"mappings":";;AAGA,kDAqBG;AAEH,0EAmCG;AA1DH,SAAgB,mBAAmB,CAC/B,MAAqC;IAErC,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QAC1B,MAAM,YAAY,GAAiB;YACjC,IAAI,EAAE,KAAK,CAAC,IAAI;SACjB,CAAA;QACD,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM,IAAI,MAAM,IAAI,KAAK,EAAE,CAAC;YAC7C,YAAY,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAA;QAChC,CAAC;aAAM,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU,IAAI,IAAI,IAAI,KAAK,EAAE,CAAC;YACtD,YAAY,CAAC,WAAW,GAAG,KAAK,CAAC,EAAE,CAAA;YACnC,YAAY,CAAC,IAAI,GAAG,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,KAAK,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;aAChE,CAAA;QACH,CAAC;aAAM,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;YACnC,OAAO;gBACH,IAAI,EAAE,UAAU;gBAChB,QAAQ,EAAE,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;gBAChE,SAAS,EAAE,EAAE;aAChB,CAAA;QACL,CAAC;aAAM,CAAC;YACN,OAAO;gBACL,IAAI,EAAE,MAAM;gBACZ,IAAI,EAAE,EAAE;aACT,CAAA;QACH,CAAC;IACH,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { TaskContext, Project, Config, ChatMessage, ModelConfig, ContentBlock, ProviderConfig, } from '../types';
|
|
2
|
+
export type { Task as TaskType } from '../types';
|
|
3
|
+
export { TaskStorage } from '../storage';
|
|
4
|
+
export { convertToAnthropicContentBlocks, convertContentBlock } from './converter';
|
|
5
|
+
export { Task } from './task_executor';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Task = exports.convertContentBlock = exports.convertToAnthropicContentBlocks = exports.TaskStorage = void 0;
|
|
4
|
+
var storage_1 = require("../storage");
|
|
5
|
+
Object.defineProperty(exports, "TaskStorage", { enumerable: true, get: function () { return storage_1.TaskStorage; } });
|
|
6
|
+
var converter_1 = require("./converter");
|
|
7
|
+
Object.defineProperty(exports, "convertToAnthropicContentBlocks", { enumerable: true, get: function () { return converter_1.convertToAnthropicContentBlocks; } });
|
|
8
|
+
Object.defineProperty(exports, "convertContentBlock", { enumerable: true, get: function () { return converter_1.convertContentBlock; } });
|
|
9
|
+
var task_executor_1 = require("./task_executor");
|
|
10
|
+
Object.defineProperty(exports, "Task", { enumerable: true, get: function () { return task_executor_1.Task; } });
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/task/index.ts"],"names":[],"mappings":";;;AAUA,sCAAwC;AAA/B,sGAAA,WAAW,OAAA;AACpB,yCAAkF;AAAzE,4HAAA,+BAA+B,OAAA;AAAE,gHAAA,mBAAmB,OAAA;AAC7D,iDAAsC;AAA7B,qGAAA,IAAI,OAAA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ProviderConfig, TaskContext, ModelConfig, ApiStream, ApiStreamToolCall } from '../types';
|
|
2
|
+
import { TaskStorage } from '../storage';
|
|
3
|
+
import { ApiHandler } from '../providers';
|
|
4
|
+
export declare class Task {
|
|
5
|
+
private projectId;
|
|
6
|
+
private taskID;
|
|
7
|
+
private modelConfig;
|
|
8
|
+
private taskContext;
|
|
9
|
+
private storage;
|
|
10
|
+
private conversationRound;
|
|
11
|
+
private historyMessages;
|
|
12
|
+
private userInstruction;
|
|
13
|
+
private variables;
|
|
14
|
+
private systemPromptBuilder;
|
|
15
|
+
constructor(taskId: string, projectId: string, modelConfig: ModelConfig, storage: TaskStorage, taskContext: TaskContext);
|
|
16
|
+
initTask(): Promise<void>;
|
|
17
|
+
private loadHistoryMessages;
|
|
18
|
+
buildUserTaskMessage(lastToolCall: ApiStreamToolCall | undefined, lastToolCallResult: string | undefined): Promise<void>;
|
|
19
|
+
startTask(userInstruction: string): ApiStream;
|
|
20
|
+
private saveMessage;
|
|
21
|
+
buildApiHandler(providerConfig?: ProviderConfig): ApiHandler;
|
|
22
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Task = void 0;
|
|
4
|
+
const openrouter_1 = require("../providers/openrouter");
|
|
5
|
+
const minimax_1 = require("../providers/minimax");
|
|
6
|
+
const doubao_1 = require("../providers/doubao");
|
|
7
|
+
const converter_1 = require("./converter");
|
|
8
|
+
const logger_1 = require("../logger");
|
|
9
|
+
const prompt_1 = require("../prompt");
|
|
10
|
+
class Task {
|
|
11
|
+
constructor(taskId, projectId, modelConfig, storage, taskContext) {
|
|
12
|
+
this.conversationRound = 1;
|
|
13
|
+
this.historyMessages = [];
|
|
14
|
+
this.variables = {};
|
|
15
|
+
this.taskID = taskId;
|
|
16
|
+
this.projectId = projectId;
|
|
17
|
+
this.modelConfig = modelConfig;
|
|
18
|
+
this.storage = storage;
|
|
19
|
+
this.userInstruction = '';
|
|
20
|
+
this.variables = taskContext.variables || {};
|
|
21
|
+
this.taskContext = taskContext;
|
|
22
|
+
this.systemPromptBuilder = taskContext.systemPromptBuilder || prompt_1.DefaultSystemPromptBuilder;
|
|
23
|
+
}
|
|
24
|
+
async initTask() {
|
|
25
|
+
const project = await this.storage.createOrUpdateProject({
|
|
26
|
+
id: this.projectId,
|
|
27
|
+
updatedAt: new Date()
|
|
28
|
+
});
|
|
29
|
+
if (project?.id) {
|
|
30
|
+
this.projectId = project.id;
|
|
31
|
+
}
|
|
32
|
+
const task = await this.storage.createOrUpdateTask({
|
|
33
|
+
id: this.taskID,
|
|
34
|
+
name: this.userInstruction,
|
|
35
|
+
projectId: this.projectId,
|
|
36
|
+
updatedAt: new Date()
|
|
37
|
+
});
|
|
38
|
+
if (task) {
|
|
39
|
+
this.taskID = task.id || '';
|
|
40
|
+
}
|
|
41
|
+
if (project?.settings) {
|
|
42
|
+
Object.entries(project.settings).forEach(([key, val]) => {
|
|
43
|
+
this.variables[key] = String(val);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (this.modelConfig?.agentModel?.provider) {
|
|
47
|
+
const modelConfig = await this.storage.getConfigByKey(this.modelConfig.agentModel.provider, 'llm');
|
|
48
|
+
if (modelConfig) {
|
|
49
|
+
this.modelConfig.agentModel = JSON.parse(modelConfig.val || '{}');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
throw new Error('Agent model provider is required');
|
|
54
|
+
}
|
|
55
|
+
await this.loadHistoryMessages();
|
|
56
|
+
}
|
|
57
|
+
async loadHistoryMessages() {
|
|
58
|
+
const messages = await this.storage.getChatMessagesByTaskId(this.taskID);
|
|
59
|
+
logger_1.logger.info({
|
|
60
|
+
taskId: this.taskID,
|
|
61
|
+
messages: messages,
|
|
62
|
+
}, 'Query chat message history');
|
|
63
|
+
this.historyMessages = messages
|
|
64
|
+
.filter((msg) => msg.role === 'user' || msg.role === 'assistant')
|
|
65
|
+
.map((msg) => {
|
|
66
|
+
const content = (0, converter_1.convertToAnthropicContentBlocks)(msg.content || []);
|
|
67
|
+
return {
|
|
68
|
+
role: msg.role,
|
|
69
|
+
content,
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
if (messages.length > 0) {
|
|
73
|
+
const maxRound = Math.max(...messages.map((msg) => msg.conversationRound || 0));
|
|
74
|
+
this.conversationRound = maxRound + 1;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async buildUserTaskMessage(lastToolCall, lastToolCallResult) {
|
|
78
|
+
const userMessageBlocks = [];
|
|
79
|
+
if (lastToolCall && lastToolCallResult) {
|
|
80
|
+
userMessageBlocks.push({
|
|
81
|
+
type: 'tool_result',
|
|
82
|
+
content: lastToolCallResult,
|
|
83
|
+
tool_use_id: lastToolCall.function.id || '',
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
if (this.userInstruction) {
|
|
87
|
+
userMessageBlocks.push({ type: 'text', text: this.userInstruction });
|
|
88
|
+
userMessageBlocks.push({ type: 'text', text: `<enviorment>${await this.systemPromptBuilder.buildEnvironmentDetails(this.taskContext)}</enviorment>` });
|
|
89
|
+
this.userInstruction = '';
|
|
90
|
+
}
|
|
91
|
+
await this.saveMessage('user', userMessageBlocks);
|
|
92
|
+
}
|
|
93
|
+
async *startTask(userInstruction) {
|
|
94
|
+
logger_1.logger.info({
|
|
95
|
+
userInstruction: userInstruction,
|
|
96
|
+
}, 'start task');
|
|
97
|
+
this.userInstruction = userInstruction;
|
|
98
|
+
await this.initTask();
|
|
99
|
+
const handler = this.buildApiHandler(this.modelConfig?.agentModel);
|
|
100
|
+
const systemPrompt = await this.systemPromptBuilder.buildSystemPrompt();
|
|
101
|
+
await this.saveMessage('system', [{ type: 'text', text: systemPrompt }]);
|
|
102
|
+
const taskInLoop = true;
|
|
103
|
+
let lastToolCall;
|
|
104
|
+
let lastToolCallResult;
|
|
105
|
+
while (taskInLoop) {
|
|
106
|
+
await this.buildUserTaskMessage(lastToolCall, lastToolCallResult);
|
|
107
|
+
lastToolCall = undefined;
|
|
108
|
+
lastToolCallResult = undefined;
|
|
109
|
+
const stream = handler.chatCompletion(systemPrompt, this.historyMessages, this.taskContext.tools?.map((tool) => tool.tool()) || []);
|
|
110
|
+
let accumulatedText = '';
|
|
111
|
+
let accumulatedThinking = '';
|
|
112
|
+
for await (const chunk of stream) {
|
|
113
|
+
switch (chunk.type) {
|
|
114
|
+
case 'text':
|
|
115
|
+
accumulatedText += chunk.text;
|
|
116
|
+
yield chunk;
|
|
117
|
+
break;
|
|
118
|
+
case 'reasoning':
|
|
119
|
+
accumulatedThinking += chunk.reasoning;
|
|
120
|
+
yield chunk;
|
|
121
|
+
break;
|
|
122
|
+
case 'tool_calls':
|
|
123
|
+
if (lastToolCall) {
|
|
124
|
+
lastToolCall.function.arguments += chunk.tool_call.function.arguments;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
lastToolCall = chunk.tool_call;
|
|
128
|
+
}
|
|
129
|
+
yield chunk;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const assistantContentBlocks = [];
|
|
134
|
+
if (accumulatedThinking) {
|
|
135
|
+
assistantContentBlocks.push({
|
|
136
|
+
type: 'thinking',
|
|
137
|
+
thinking: accumulatedThinking,
|
|
138
|
+
signature: '',
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
if (accumulatedText) {
|
|
142
|
+
assistantContentBlocks.push({
|
|
143
|
+
type: 'text',
|
|
144
|
+
text: accumulatedText,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
if (lastToolCall) {
|
|
148
|
+
let input = lastToolCall.function.arguments;
|
|
149
|
+
if (typeof input === 'string') {
|
|
150
|
+
try {
|
|
151
|
+
input = JSON.parse(input);
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
assistantContentBlocks.push({
|
|
157
|
+
type: 'tool_use',
|
|
158
|
+
id: lastToolCall.function.id || '',
|
|
159
|
+
input: input,
|
|
160
|
+
name: lastToolCall.function.name || '',
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
await this.saveMessage('assistant', assistantContentBlocks);
|
|
164
|
+
if (lastToolCall?.function?.name) {
|
|
165
|
+
const toolName = lastToolCall.function.name;
|
|
166
|
+
const toolHandler = this.taskContext.tools?.find((tool) => {
|
|
167
|
+
const toolDef = tool.tool();
|
|
168
|
+
return toolDef.type === 'function' && toolDef.function.name === toolName;
|
|
169
|
+
});
|
|
170
|
+
if (toolHandler) {
|
|
171
|
+
lastToolCallResult = await toolHandler.execute(lastToolCall, this.taskContext);
|
|
172
|
+
logger_1.logger.info({
|
|
173
|
+
taskId: this.taskID,
|
|
174
|
+
projectId: this.projectId,
|
|
175
|
+
toolName: lastToolCall.function.name,
|
|
176
|
+
toolCallId: lastToolCall.function.id,
|
|
177
|
+
result: lastToolCallResult,
|
|
178
|
+
}, 'Tool execution result');
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async saveMessage(role, blocks) {
|
|
187
|
+
if (role === 'user' || role === 'assistant') {
|
|
188
|
+
this.historyMessages.push({ role: role, content: blocks });
|
|
189
|
+
}
|
|
190
|
+
await this.storage.createChatMessage({
|
|
191
|
+
taskId: this.taskID,
|
|
192
|
+
projectId: this.projectId,
|
|
193
|
+
conversationRound: this.conversationRound,
|
|
194
|
+
messageOrder: 1,
|
|
195
|
+
role: role,
|
|
196
|
+
content: (0, converter_1.convertContentBlock)(blocks),
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
buildApiHandler(providerConfig) {
|
|
200
|
+
if (!providerConfig?.provider) {
|
|
201
|
+
throw new Error('Provider configuration is missing or incomplete');
|
|
202
|
+
}
|
|
203
|
+
switch (providerConfig.provider) {
|
|
204
|
+
case 'minimax':
|
|
205
|
+
return new minimax_1.MinimaxHandler(providerConfig);
|
|
206
|
+
case 'doubao':
|
|
207
|
+
return new doubao_1.DoubaoHandler(providerConfig);
|
|
208
|
+
case 'openrouter':
|
|
209
|
+
return new openrouter_1.OpenRouterHandler(providerConfig);
|
|
210
|
+
default:
|
|
211
|
+
throw new Error(`Unsupported provider: ${providerConfig.provider}`);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
exports.Task = Task;
|
|
216
|
+
//# sourceMappingURL=task_executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"task_executor.js","sourceRoot":"","sources":["../../src/task/task_executor.ts"],"names":[],"mappings":";;;AACA,wDAA2D;AAG3D,kDAAqD;AAErD,gDAAmD;AAEnD,2CAAkF;AAElF,sCAAkC;AAClC,sCAA4E;AAE5E,MAAa,IAAI;IAYf,YAAY,MAAc,EAAE,SAAiB,EAAE,WAAwB,EAAE,OAAoB,EAAE,WAAwB;QAN/G,sBAAiB,GAAW,CAAC,CAAA;QAC7B,oBAAe,GAA6B,EAAE,CAAA;QAE9C,cAAS,GAA2B,EAAE,CAAA;QAI5C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;QAC1B,IAAI,CAAC,WAAW,GAAG,WA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AC,CAAA;IACJ,CAAC;IAID,eAAe,CAAC,cAA+B;QAC7C,IAAI,CAAC,cAAc,EAAE,QAAQ,EAAE,CAAC;YAC9B,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAA;QACpE,CAAC;QAED,QAAQ,cAAc,CAAC,QAAQ,EAAE,CAAC;YAChC,KAAK,SAAS;gBACZ,OAAO,IAAI,wBAAc,CAAC,cAAc,CAAC,CAAA;YAC3C,KAAK,QAAQ;gBACX,OAAO,IAAI,sBAAa,CAAC,cAAc,CAAC,CAAA;YAC1C,KAAK,YAAY;gBACf,OAAO,IAAI,8BAAiB,CAAC,cAAc,CAAC,CAAA;YAC9C;gBACE,MAAM,IAAI,KAAK,CAAC,yBAAyB,cAAc,CAAC,QAAQ,EAAE,CAAC,CAAA;QACvE,CAAC;IACH,CAAC;CAEF;AA1OD,oBA0OC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../types';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("../types"), exports);
|
|
18
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/task/types.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types"), exports);
|
|
18
|
+
__exportStar(require("./tool_converter"), exports);
|
|
19
|
+
__exportStar(require("./update_todo_list"), exports);
|
|
20
|
+
__exportStar(require("./web_search"), exports);
|
|
21
|
+
__exportStar(require("./replace_content"), exports);
|
|
22
|
+
__exportStar(require("./write_content"), exports);
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAuB;AACvB,mDAAgC;AAChC,qDAAkC;AAClC,+CAA4B;AAC5B,oDAAiC;AACjC,kDAA+B"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ChatCompletionTool as OpenAITool } from 'openai/resources/chat/completions';
|
|
2
|
+
import { ToolHandler, ApiStreamToolCall, TaskContext } from '../types';
|
|
3
|
+
export declare const ReplaceContentTool: OpenAITool;
|
|
4
|
+
export default class ReplaceContentToolHandler implements ToolHandler {
|
|
5
|
+
private context;
|
|
6
|
+
tool(): OpenAITool;
|
|
7
|
+
setContext(context: TaskContext): void;
|
|
8
|
+
execute(tool: ApiStreamToolCall, context?: TaskContext): Promise<string>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReplaceContentTool = void 0;
|
|
4
|
+
exports.ReplaceContentTool = {
|
|
5
|
+
type: 'function',
|
|
6
|
+
function: {
|
|
7
|
+
name: 'replace_content',
|
|
8
|
+
description: '请求替换内容。使用 SEARCH/REPLACE 块来定义对内容特定部分的精确更改。直接返回替换后的内容,不进行文件操作。',
|
|
9
|
+
parameters: {
|
|
10
|
+
type: 'object',
|
|
11
|
+
properties: {
|
|
12
|
+
diff: {
|
|
13
|
+
type: 'string',
|
|
14
|
+
description: `一个或多个 SEARCH/REPLACE 块,遵循以下确切格式:
|
|
15
|
+
\`\`\`
|
|
16
|
+
------- SEARCH
|
|
17
|
+
[要查找的确切内容]
|
|
18
|
+
=======
|
|
19
|
+
[要替换为的新内容]
|
|
20
|
+
+++++++ REPLACE
|
|
21
|
+
\`\`\`
|
|
22
|
+
关键规则:
|
|
23
|
+
1. SEARCH 内容必须完全匹配要查找的部分:
|
|
24
|
+
* 逐字符匹配,包括空格、缩进、行尾
|
|
25
|
+
* 包括所有注释、文档字符串等
|
|
26
|
+
2. SEARCH/REPLACE 块只会替换第一个匹配项。
|
|
27
|
+
* 如果需要多次更改,请包含多个唯一的 SEARCH/REPLACE 块。
|
|
28
|
+
* 在每个 SEARCH 部分中包含足够多的行以唯一匹配需要更改的行集。
|
|
29
|
+
* 使用多个 SEARCH/REPLACE 块时,按它们在内容中出现的顺序列出。
|
|
30
|
+
3. 保持 SEARCH/REPLACE 块简洁:
|
|
31
|
+
* 将大的 SEARCH/REPLACE 块拆分为一系列较小的块,每个块更改内容的一小部分。
|
|
32
|
+
* 只包含更改的行,如果需要唯一性,可以包含几行周围的行。
|
|
33
|
+
* 不要在 SEARCH/REPLACE 块中包含长串未更改的行。
|
|
34
|
+
* 每行必须完整。永远不要中途截断行,这可能导致匹配失败。
|
|
35
|
+
4. 特殊操作:
|
|
36
|
+
* 移动代码:使用两个 SEARCH/REPLACE 块(一个从原位置删除 + 一个在新位置插入)
|
|
37
|
+
* 删除代码:使用空的 REPLACE 部分`,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
required: ['diff'],
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
class ReplaceContentToolHandler {
|
|
45
|
+
constructor() {
|
|
46
|
+
this.context = null;
|
|
47
|
+
}
|
|
48
|
+
tool() {
|
|
49
|
+
return exports.ReplaceContentTool;
|
|
50
|
+
}
|
|
51
|
+
setContext(context) {
|
|
52
|
+
this.context = context;
|
|
53
|
+
}
|
|
54
|
+
async execute(tool, context) {
|
|
55
|
+
let args = tool.function.arguments;
|
|
56
|
+
if (typeof args === 'string') {
|
|
57
|
+
try {
|
|
58
|
+
args = JSON.parse(args);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
throw new Error(`Failed to parse tool arguments: ${error}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const parameters = args;
|
|
65
|
+
if (!parameters.diff) {
|
|
66
|
+
throw new Error('diff parameter is required');
|
|
67
|
+
}
|
|
68
|
+
return parameters.diff;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.default = ReplaceContentToolHandler;
|
|
72
|
+
//# sourceMappingURL=replace_content.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replace_content.js","sourceRoot":"","sources":["../../src/tools/replace_content.ts"],"names":[],"mappings":";;;AAGa,QAAA,kBAAkB,GAAe;IAC5C,IAAI,EAAE,UAAU;IAChB,QAAQ,EAAE;QACR,IAAI,EAAE,iBAAiB;QACvB,WAAW,EACT,+DAA+D;QACjE,UAAU,EAAE;YACV,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,IAAI,EAAE;oBACJ,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE;;;;;;;;;;;;;;;;;;;;;;;0BAuBG;iBACjB;aACF;YACD,QAAQ,EAAE,CAAC,MAAM,CAAC;SACnB;KACF;CACF,CAAA;AAED,MAAqB,yBAAyB;IAA9C;QACU,YAAO,GAAuB,IAAI,CAAA;IAiC5C,CAAC;IA/BC,IAAI;QACF,OAAO,0BAAkB,CAAA;IAC3B,CAAC;IAED,UAAU,CAAC,OAAoB;QAC7B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;IAED,KAAK,CAAC,OAAO,CACX,IAAuB,EACvB,OAAqB;QAErB,IAAI,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAA;QAClC,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,IAAI,CAAC;gBACH,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;YACzB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,IAAI,KAAK,CAAC,mCAAmC,KAAK,EAAE,CAAC,CAAA;YAC7D,CAAC;QACH,CAAC;QAED,MAAM,UAAU,GAAG,IAElB,CAAA;QAED,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAA;QAC/C,CAAC;QAED,OAAO,UAAU,CAAC,IAAI,CAAA;IACxB,CAAC;CACF;AAlCD,4CAkCC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertToAnthropicTool = convertToAnthropicTool;
|
|
4
|
+
function convertToAnthropicTool(tool) {
|
|
5
|
+
const properties = {};
|
|
6
|
+
const required = [];
|
|
7
|
+
if (tool.type === 'function' && tool.function?.parameters?.properties) {
|
|
8
|
+
const requiredArray = Array.isArray(tool.function.parameters.required)
|
|
9
|
+
? tool.function.parameters.required
|
|
10
|
+
: [];
|
|
11
|
+
const requiredParams = new Set(requiredArray);
|
|
12
|
+
for (const [paramName, param] of Object.entries(tool.function.parameters.properties)) {
|
|
13
|
+
if (requiredParams.has(paramName)) {
|
|
14
|
+
required.push(paramName);
|
|
15
|
+
}
|
|
16
|
+
const typedParam = param;
|
|
17
|
+
const paramType = typedParam.type || "string";
|
|
18
|
+
const paramSchema = {
|
|
19
|
+
type: paramType,
|
|
20
|
+
description: typedParam.description,
|
|
21
|
+
};
|
|
22
|
+
if (paramType === "array" && typedParam.items) {
|
|
23
|
+
paramSchema.items = typedParam.items;
|
|
24
|
+
}
|
|
25
|
+
if (paramType === "object" && typedParam.properties) {
|
|
26
|
+
paramSchema.properties = typedParam.properties;
|
|
27
|
+
}
|
|
28
|
+
const reservedKeys = new Set([
|
|
29
|
+
"name",
|
|
30
|
+
"required",
|
|
31
|
+
"instruction",
|
|
32
|
+
"usage",
|
|
33
|
+
"dependencies",
|
|
34
|
+
"description",
|
|
35
|
+
"contextRequirements",
|
|
36
|
+
"type",
|
|
37
|
+
"items",
|
|
38
|
+
"properties",
|
|
39
|
+
]);
|
|
40
|
+
for (const key in typedParam) {
|
|
41
|
+
if (!reservedKeys.has(key) && typedParam[key] !== undefined) {
|
|
42
|
+
paramSchema[key] = typedParam[key];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
properties[paramName] = paramSchema;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const toolInputSchema = {
|
|
49
|
+
name: tool.type === 'function' ? tool.function.name : '',
|
|
50
|
+
description: tool.type === 'function' ? tool.function.description : '',
|
|
51
|
+
input_schema: {
|
|
52
|
+
type: "object",
|
|
53
|
+
properties,
|
|
54
|
+
required,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
return toolInputSchema;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=tool_converter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool_converter.js","sourceRoot":"","sources":["../../src/tools/tool_converter.ts"],"names":[],"mappings":";;AAGA,wDA6DC;AA7DD,SAAgB,sBAAsB,CAAG,IAAgB;IACrD,MAAM,UAAU,GAAwB,EAAE,CAAA;IAC1C,MAAM,QAAQ,GAAa,EAAE,CAAA;IAE7B,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,IAAI,IAAI,CAAC,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC;QACpE,MAAM,aAAa,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC;YAClE,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,QAAQ;YACnC,CAAC,CAAC,EAAE,CAAA;QACR,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,aAAa,CAAC,CAAA;QAC7C,KAAK,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;YACnF,IAAI,cAAc,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;gBAChC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;YAC5B,CAAC;YACD,MAAM,UAAU,GAAG,KAAY,CAAA;YAC/B,MAAM,SAAS,GAAW,UAAU,CAAC,IAAI,IAAI,QAAQ,CAAA;YAErD,MAAM,WAAW,GAAQ;gBACrB,IAAI,EAAE,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
|
|
|
1
|
+
export * from '../types/tools';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("../types/tools"), exports);
|
|
18
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/tools/types.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA8B"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ChatCompletionTool as OpenAITool } from 'openai/resources/chat/completions';
|
|
2
|
+
import { ToolHandler, ApiStreamToolCall, TaskContext } from '../types';
|
|
3
|
+
export declare const TodoTool: OpenAITool;
|
|
4
|
+
export declare class TodoToolHandler implements ToolHandler {
|
|
5
|
+
tool(): OpenAITool;
|
|
6
|
+
execute(toolCall: ApiStreamToolCall, context: TaskContext): Promise<string>;
|
|
7
|
+
}
|
|
8
|
+
export interface TodoItem {
|
|
9
|
+
idx: number;
|
|
10
|
+
content: string;
|
|
11
|
+
status: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function formatReminderSection(todoList?: TodoItem[]): string;
|