@ct-agents/tools 0.1.4 → 0.1.5
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ct-agents/tools",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"src"
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"zod": "4.4.3",
|
|
15
|
-
"@ct-agents/protocol": "0.1.
|
|
15
|
+
"@ct-agents/protocol": "0.1.5"
|
|
16
16
|
},
|
|
17
17
|
"publishConfig": {
|
|
18
18
|
"access": "public"
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { ToolExecution, ToolHandler } from '@ct-agents/protocol';
|
|
3
|
+
|
|
4
|
+
export const todoWriteStepStatusSchema = z.enum([
|
|
5
|
+
'pending',
|
|
6
|
+
'in_progress',
|
|
7
|
+
'completed',
|
|
8
|
+
]);
|
|
9
|
+
|
|
10
|
+
const todoWriteStepSchema = z.object({
|
|
11
|
+
step: z.string().trim().min(1).max(160),
|
|
12
|
+
status: todoWriteStepStatusSchema,
|
|
13
|
+
}).strict();
|
|
14
|
+
|
|
15
|
+
type TodoWriteStep = z.infer<typeof todoWriteStepSchema>;
|
|
16
|
+
|
|
17
|
+
export function validateLinearTodoSteps(
|
|
18
|
+
steps: TodoWriteStep[],
|
|
19
|
+
context: z.RefinementCtx,
|
|
20
|
+
): void {
|
|
21
|
+
if (steps.length === 0 || steps.every((step) => step.status === 'completed')) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const activeIndexes = steps
|
|
26
|
+
.map((step, index) => step.status === 'in_progress' ? index : -1)
|
|
27
|
+
.filter((index) => index >= 0);
|
|
28
|
+
if (activeIndexes.length !== 1) {
|
|
29
|
+
context.addIssue({
|
|
30
|
+
code: 'custom',
|
|
31
|
+
path: ['steps'],
|
|
32
|
+
message: '非空未完成计划必须且只能包含一个 in_progress 步骤',
|
|
33
|
+
});
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const activeIndex = activeIndexes[0];
|
|
38
|
+
if (activeIndex === undefined) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
for (const [index, step] of steps.entries()) {
|
|
42
|
+
const expectedStatus = index < activeIndex
|
|
43
|
+
? 'completed'
|
|
44
|
+
: index === activeIndex
|
|
45
|
+
? 'in_progress'
|
|
46
|
+
: 'pending';
|
|
47
|
+
if (step.status !== expectedStatus) {
|
|
48
|
+
context.addIssue({
|
|
49
|
+
code: 'custom',
|
|
50
|
+
path: ['steps', index, 'status'],
|
|
51
|
+
message: '计划状态必须符合 completed* -> in_progress -> pending*',
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const todoWriteSnapshotSchema = z.object({
|
|
58
|
+
steps: z.array(todoWriteStepSchema).max(20),
|
|
59
|
+
}).strict().superRefine((snapshot, context) => {
|
|
60
|
+
validateLinearTodoSteps(snapshot.steps, context);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export type TodoWriteSnapshot = z.infer<typeof todoWriteSnapshotSchema>;
|
|
64
|
+
|
|
65
|
+
const todoWriteStepJsonSchema = {
|
|
66
|
+
type: 'object',
|
|
67
|
+
additionalProperties: false,
|
|
68
|
+
required: ['step', 'status'],
|
|
69
|
+
properties: {
|
|
70
|
+
step: { type: 'string', minLength: 1, maxLength: 160, description: '任务步骤文本。' },
|
|
71
|
+
status: {
|
|
72
|
+
type: 'string',
|
|
73
|
+
enum: ['pending', 'in_progress', 'completed'],
|
|
74
|
+
description: '步骤在线性计划中的当前状态。',
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
} as const;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 写入线性任务计划的完整快照。
|
|
81
|
+
*
|
|
82
|
+
* handler 不读取历史快照或 Resource;事件持久化和模型上下文恢复继续由 Environment/Harness 负责。
|
|
83
|
+
*/
|
|
84
|
+
export function createTodoWriteToolHandler(): ToolHandler<TodoWriteSnapshot, TodoWriteSnapshot> {
|
|
85
|
+
return {
|
|
86
|
+
descriptor: {
|
|
87
|
+
name: 'todo-write',
|
|
88
|
+
title: '更新任务计划',
|
|
89
|
+
description: [
|
|
90
|
+
'在长任务中创建、推进、完成或清空线性任务计划。',
|
|
91
|
+
'每次调用必须提交完整 steps 快照:未完成计划只能有一个 in_progress,之前只能是 completed,之后只能是 pending;全部完成时保留所有 completed 步骤。',
|
|
92
|
+
'使用 { steps: [] } 显式清空计划。',
|
|
93
|
+
].join('\n'),
|
|
94
|
+
inputSchema: {
|
|
95
|
+
type: 'object',
|
|
96
|
+
additionalProperties: false,
|
|
97
|
+
required: ['steps'],
|
|
98
|
+
properties: {
|
|
99
|
+
steps: {
|
|
100
|
+
type: 'array',
|
|
101
|
+
maxItems: 20,
|
|
102
|
+
description: '完整计划快照。空数组表示清空;非空未完成快照必须符合 completed* -> in_progress -> pending*,全部完成快照只能包含 completed。',
|
|
103
|
+
items: todoWriteStepJsonSchema,
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
resultSchema: {
|
|
108
|
+
type: 'object',
|
|
109
|
+
additionalProperties: false,
|
|
110
|
+
required: ['steps'],
|
|
111
|
+
properties: {
|
|
112
|
+
steps: {
|
|
113
|
+
type: 'array',
|
|
114
|
+
maxItems: 20,
|
|
115
|
+
description: '规范化后的完整计划快照。',
|
|
116
|
+
items: todoWriteStepJsonSchema,
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
annotations: {
|
|
121
|
+
readOnly: true,
|
|
122
|
+
idempotent: true,
|
|
123
|
+
openWorld: false,
|
|
124
|
+
destructive: false,
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
inputSchema: todoWriteSnapshotSchema,
|
|
128
|
+
resultSchema: todoWriteSnapshotSchema,
|
|
129
|
+
execute: async (input): Promise<ToolExecution<TodoWriteSnapshot>> => {
|
|
130
|
+
const normalizedSnapshot = todoWriteSnapshotSchema.parse(input);
|
|
131
|
+
return {
|
|
132
|
+
status: 'completed',
|
|
133
|
+
modelResult: {
|
|
134
|
+
steps: normalizedSnapshot.steps.map((step) => ({ ...step })),
|
|
135
|
+
},
|
|
136
|
+
effects: [],
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|