personal-bot 0.2.15 → 0.2.16

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 (56) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/README.md +1 -1
  4. package/README.zh-CN.md +1 -1
  5. package/docs/agent-collaboration.md +5 -6
  6. package/docs/dsh.md +11 -11
  7. package/lib/common/schema.d.ts +2 -0
  8. package/lib/common/schema.js +9 -0
  9. package/lib/extension/dsh/command.js +13 -18
  10. package/lib/extension/dsh/workspace.js +0 -2
  11. package/lib/integrations/dsh/dsh-agents.d.ts +1 -2
  12. package/lib/integrations/dsh/dsh-agents.js +0 -4
  13. package/lib/model/agent.d.ts +0 -1
  14. package/lib/services/agents/agent-notices.js +1 -1
  15. package/lib/services/agents/agent-service.js +3 -2
  16. package/lib/services/agents/start-options.d.ts +4 -0
  17. package/lib/services/agents/start-options.js +20 -0
  18. package/lib/tools/agents/actions.d.ts +1 -5
  19. package/lib/tools/agents/actions.js +12 -1
  20. package/lib/tools/agents/contract.d.ts +50 -77
  21. package/lib/tools/agents/contract.js +54 -32
  22. package/lib/tools/agents/dsh-tool.js +6 -11
  23. package/lib/tools/history/contract.d.ts +28 -11
  24. package/lib/tools/history/contract.js +18 -12
  25. package/lib/tools/input.d.ts +11 -0
  26. package/lib/tools/input.js +23 -0
  27. package/lib/tools/teams/actions.d.ts +3 -4
  28. package/lib/tools/teams/actions.js +7 -5
  29. package/lib/tools/teams/contract.d.ts +56 -87
  30. package/lib/tools/teams/contract.js +55 -31
  31. package/lib/tools/teams/dsh-tool.js +7 -18
  32. package/package.json +1 -1
  33. package/resources/prompts/native-coordinator.md +1 -1
  34. package/src/common/schema.ts +10 -0
  35. package/src/extension/agent-events.ts +1 -1
  36. package/src/extension/dsh/command.ts +14 -19
  37. package/src/extension/dsh/workspace.ts +0 -1
  38. package/src/integrations/dsh/dsh-agents.ts +1 -4
  39. package/src/model/agent.ts +1 -1
  40. package/src/services/agents/agent-notices.ts +1 -1
  41. package/src/services/agents/agent-service.ts +4 -2
  42. package/src/services/agents/start-options.ts +19 -0
  43. package/src/tools/agents/actions.ts +18 -10
  44. package/src/tools/agents/contract.ts +68 -33
  45. package/src/tools/agents/dsh-tool.ts +7 -12
  46. package/src/tools/agents/tool.ts +2 -8
  47. package/src/tools/history/actions.ts +1 -1
  48. package/src/tools/history/contract.ts +27 -14
  49. package/src/tools/input.ts +29 -0
  50. package/src/tools/teams/actions.ts +10 -14
  51. package/src/tools/teams/contract.ts +71 -33
  52. package/src/tools/teams/dsh-tool.ts +7 -18
  53. package/src/tools/teams/tool.ts +3 -13
  54. package/lib/tools/agents/dsh-contract.d.ts +0 -45
  55. package/lib/tools/agents/dsh-contract.js +0 -19
  56. package/src/tools/agents/dsh-contract.ts +0 -20
@@ -2,22 +2,16 @@ import { defineTool } from "@earendil-works/pi-coding-agent";
2
2
  import type { AgentService } from "../../services/agents/agent-service.js";
3
3
  import { agentOwner } from "../../integrations/pi/agent-context.js";
4
4
  import type { CallAccess } from "../access.js";
5
- import { requireThat } from "../../common/validation.js";
6
- import { schema } from "../../common/schema.js";
7
5
  import { executeAgent } from "./actions.js";
8
- import { agentDescription, agentActionSchema, agentParametersSchema, agentResultSchema } from "./contract.js";
6
+ import { agentDescription, agentInput, agentParametersSchema, agentResultSchema } from "./contract.js";
9
7
 
10
8
  export function agentTool(service: AgentService, access: CallAccess) {
11
- const validators = new Map(agentActionSchema.anyOf.map(action => [action.properties.action.const, schema(action)]));
12
9
  return defineTool({ name: "pbot_agent", label: "子会话",
13
10
  description: agentDescription,
14
11
  parameters: agentParametersSchema, outputSchema: agentResultSchema,
15
12
  async execute(_id, args, signal, _update, ctx) {
16
13
  access.check(); signal?.throwIfAborted();
17
- const valid = validators.get(args.action);
18
- requireThat(valid, "未知的子会话 action:" + args.action);
19
- requireThat(valid(args), "子会话 " + args.action + " 参数无效:" + JSON.stringify(valid.errors));
20
- return executeAgent(service, args, agentOwner(ctx), {
14
+ return executeAgent(service, agentInput(args), agentOwner(ctx), {
21
15
  ...(ctx.model ? { provider: ctx.model.provider, model: ctx.model.id } : {}),
22
16
  ...(ctx.thinkingLevel ? { thinking: ctx.thinkingLevel } : {}),
23
17
  });
@@ -5,5 +5,5 @@ import type { HistoryInput } from "./contract.js";
5
5
  export async function executeHistory(service: HistoryService, args: HistoryInput, location: HistoryLocation, signal?: AbortSignal) {
6
6
  signal?.throwIfAborted(); await service.refresh(location, signal); signal?.throwIfAborted();
7
7
  return args.action === "search" ? service.search(args) :
8
- { node: service.read(args.id!, args.offset, args.limit, args.childrenOffset, args.host), warnings: service.warnings };
8
+ { node: service.read(args.id, args.offset, args.limit, args.childrenOffset, args.host), warnings: service.warnings };
9
9
  }
@@ -1,6 +1,8 @@
1
- import { Type, type Static } from "typebox";
1
+ import { Type } from "typebox";
2
+ import type { HistoryHost } from "../../model/history.js";
2
3
  import { requireThat } from "../../common/validation.js";
3
4
  import { schema } from "../../common/schema.js";
5
+ import { inputObject, need, nullable, pick } from "../input.js";
4
6
 
5
7
  const host = Type.Union([Type.Literal("pi"), Type.Literal("dsh"), Type.Literal("claude")]);
6
8
  const nullableOffset = Type.Union([Type.Integer(), Type.Null()]);
@@ -16,20 +18,31 @@ export const historyResultSchema = Type.Object({
16
18
  });
17
19
  export const historyDescription = "查询当前工作区的 Pi/dsh 问答与已有 Claude 归档,host 可筛选来源。search 按不区分大小写的连续文本匹配原文和摘要;无匹配时换词,省略 query 列会话。read 按 parents/children 的 id 展开,nextOffset 续读正文,nextChildrenOffset 续读子节点。需要对话细节时读取 exchange 原文;需要工具日志时查所属宿主的原始会话。";
18
20
  export const historyParameters = Type.Object({ action: Type.Union([Type.Literal("search"), Type.Literal("read")]),
19
- host: Type.Optional(host), query: Type.Optional(Type.String()), sessionId: Type.Optional(Type.String()),
20
- from: Type.Optional(Type.String({ description: "search:ISO 日期或时间,下界。" })),
21
- to: Type.Optional(Type.String({ description: "search:ISO 日期或时间,上界。" })),
22
- id: Type.Optional(Type.String({ description: "read 必填:返回的节点 id。" })),
23
- offset: Type.Optional(Type.Integer({ minimum: 0 })),
24
- limit: Type.Optional(Type.Integer({ minimum: 1, maximum: 24000, description: "search:最多 50 条,默认 20;read:字符数,默认 12000。" })),
25
- childrenOffset: Type.Optional(Type.Integer({ minimum: 0 })),
21
+ host: nullable(host), query: nullable(Type.String()), sessionId: nullable(Type.String()),
22
+ from: nullable(Type.String({ description: "search:ISO 日期或时间,下界。" })),
23
+ to: nullable(Type.String({ description: "search:ISO 日期或时间,上界。" })),
24
+ id: nullable(Type.String({ description: "read 必填:返回的节点 id。" })),
25
+ offset: nullable(Type.Integer({ minimum: 0, description: "search:匹配结果偏移;read:正文 UTF-16 字符偏移。默认 0。" })),
26
+ limit: nullable(Type.Integer({ minimum: 1, maximum: 24000, description: "search:最多返回的匹配条数,默认 20,最多 50;read:正文 UTF-16 字符数,默认 12000。" })),
27
+ childrenOffset: nullable(Type.Integer({ minimum: 0 })),
26
28
  }, { additionalProperties: false });
27
- export type HistoryInput = Static<typeof historyParameters>;
29
+
30
+ export type HistoryInput =
31
+ | { action: "search"; host?: HistoryHost; query?: string; sessionId?: string; from?: string; to?: string; offset?: number; limit?: number }
32
+ | { action: "read"; id: string; host?: HistoryHost; offset?: number; limit?: number; childrenOffset?: number };
33
+
28
34
  const valid = schema(historyParameters);
35
+
29
36
  export function historyInput(value: unknown): HistoryInput {
30
- requireThat(valid(value), "历史参数无效。");
31
- const args = value as HistoryInput;
32
- requireThat(args.action !== "read" || args.id, "read 需要节点 id。");
33
- requireThat(args.action !== "search" || (args.limit ?? 20) <= 50, "search limit 最多 50。");
34
- return args;
37
+ const args = inputObject(value, valid, "历史参数格式无效");
38
+ if (args.action === "search") {
39
+ const limit = args.limit === undefined ? 20 : args.limit as number;
40
+ requireThat(limit <= 50, "search 的 limit 最多 50。");
41
+ return { action: "search", ...(pick(args, ["host", "query", "sessionId", "from", "to", "offset", "limit"]) as
42
+ Omit<Extract<HistoryInput, { action: "search" }>, "action">) };
43
+ }
44
+ if (args.action === "read") return { action: "read", id: need("read", args, "id"),
45
+ ...(pick(args, ["host", "offset", "limit", "childrenOffset"]) as
46
+ Omit<Extract<HistoryInput, { action: "read" }>, "action" | "id">) };
47
+ throw new Error("未知的历史 action。");
35
48
  }
@@ -0,0 +1,29 @@
1
+ import type { TSchema } from "typebox";
2
+ import { Type } from "typebox";
3
+ import type { ValidateFunction } from "ajv";
4
+ import { object } from "../common/json.js";
5
+ import { schemaDetail } from "../common/schema.js";
6
+ import { requireThat } from "../common/validation.js";
7
+
8
+ /** Optional tool field that also accepts null, the absence spelling of structured tool calls. */
9
+ export function nullable<T extends TSchema>(value: T) {
10
+ return Type.Optional(Type.Union([value, Type.Null()]));
11
+ }
12
+
13
+ /** Validate one record shape, then treat null as absent before action-specific projection. */
14
+ export function inputObject(value: unknown, validate: ValidateFunction, label: string): Record<string, unknown> {
15
+ requireThat(validate(value) && object(value), label + ":" + schemaDetail(validate));
16
+ return Object.fromEntries(Object.entries(value).filter(([, item]) => item !== null));
17
+ }
18
+
19
+ /** A mode names each missing input instead of reporting a generic invalid-argument error. */
20
+ export function need<T>(action: string, args: Record<string, unknown>, key: string): T {
21
+ const value = args[key];
22
+ requireThat(value !== undefined, action + " 需要 " + key + "。");
23
+ return value as T;
24
+ }
25
+
26
+ /** Copy only the fields a mode reads. Fields outside the projection never reach the service. */
27
+ export function pick(args: Record<string, unknown>, keys: readonly string[]): Record<string, unknown> {
28
+ return Object.fromEntries(keys.filter(key => args[key] !== undefined).map(key => [key, args[key]]));
29
+ }
@@ -1,38 +1,34 @@
1
- import type { Static } from "typebox";
2
1
  import type { TeamService } from "../../services/agents/team-service.js";
3
2
  import type { AgentService } from "../../services/agents/agent-service.js";
4
3
  import type { TeamActor } from "../../model/team.js";
5
4
  import type { AgentOwner } from "../../model/agent.js";
6
5
  import { teamMessagesMarkdown } from "../../integrations/team-context.js";
7
6
  import { markdownResult, result } from "../result.js";
8
- import type { teamParametersSchema, teamMessageParametersSchema } from "./contract.js";
7
+ import type { TeamInput, TeamMessageInput } from "./contract.js";
9
8
 
10
- export function executeTeam(service: TeamService, agents: AgentService, input: Static<typeof teamParametersSchema>, owner: AgentOwner) {
9
+ export function executeTeam(service: TeamService, agents: AgentService, input: TeamInput, owner: AgentOwner) {
11
10
  const views = agents.list(owner);
12
11
  service.recover(owner);
13
- let id = input.teamId!;
12
+ let id = "";
14
13
  switch (input.action) {
15
- case "create": id = service.create(owner, { name: input.name!, brief: input.brief!, members: input.members! }, views).teamId; break;
14
+ case "create": id = service.create(owner, { name: input.name, brief: input.brief, members: input.members }, views).teamId; break;
16
15
  case "list": return markdownResult({ teams: service.list(owner, views) }, "团队列表");
17
- case "read": {
18
- const page = service.read(id, { kind: "coordinator", owner }, input);
19
- return result(page, false, teamMessagesMarkdown(page));
20
- }
21
- case "run": service.run(id, owner, { instruction: input.instruction!, mode: input.mode!,
16
+ case "run": id = input.teamId; service.run(id, owner, { instruction: input.instruction, mode: input.mode,
22
17
  ...(input.rounds === undefined ? {} : { rounds: input.rounds }),
23
18
  ...(input.maxConcurrent === undefined ? {} : { maxConcurrent: input.maxConcurrent }),
24
19
  ...(input.timeoutSeconds === undefined ? {} : { timeoutSeconds: input.timeoutSeconds }) }, views); break;
25
- case "pause": service.pause(id, owner); break;
26
- case "close": service.close(id, owner, views); break;
20
+ case "pause": id = input.teamId; service.pause(id, owner); break;
21
+ case "close": id = input.teamId; service.close(id, owner, views); break;
22
+ case "status": id = input.teamId; break;
27
23
  }
28
24
  return markdownResult(service.status(id, owner, views), "团队状态");
29
25
  }
30
26
 
31
- export function executeTeamMessage(service: TeamService, input: Static<typeof teamMessageParametersSchema>, actor: TeamActor, requestId: string) {
27
+ export function executeTeamMessage(service: TeamService, input: TeamMessageInput, actor: TeamActor, requestId: string) {
32
28
  if (input.action === "read") {
33
29
  const page = service.read(input.teamId, actor, input);
34
30
  return result(page, false, teamMessagesMarkdown(page));
35
31
  }
36
- const message = service.post(input.teamId, actor, requestId, input.text!, input.replyTo);
32
+ const message = service.post(input.teamId, actor, requestId, input.text, input.replyTo);
37
33
  return markdownResult({ teamId: input.teamId, seq: message.seq, status: "stored" }, "团队消息已保存");
38
34
  }
@@ -1,10 +1,11 @@
1
1
  import { Type } from "typebox";
2
+ import type { TSchema } from "typebox";
3
+ import { schema } from "../../common/schema.js";
4
+ import { inputObject, need, nullable, pick } from "../input.js";
2
5
 
3
- export const teamDescription = "组织固定成员协作。create 建立团队,run 开始一批讨论。运行时占用成员;pause 停止新安排,允许在途工作收尾,之后可私人 send;中止成员用 pbot_agent stop。close 保留成员和历史;换成员须关闭旧团队再创建。继续讨论用 run 提交新要求。成员最终答复向团队公开。";
6
+ export const teamDescription = "组织固定成员协作。create 建立团队,run 开始一批讨论。运行时占用成员;pause 停止新安排,允许在途工作收尾,之后可私人 send;中止成员用 pbot_agent stop。close 保留成员和历史;换成员须关闭旧团队再创建。继续讨论用 run 提交新要求。团队状态与批次用 status;公共原文用 pbot_team_message read/post。";
4
7
  export const teamMessageDescription = "读写团队公共原文。post 向全体成员发布信息,需要安排回答时由主会话调用 pbot_team run;成员只能在自己的团队工作中 post。read 用 afterSeq 翻页,长正文按 seq/offset 续读,先补完 nextOffset 非空的消息。";
5
8
 
6
- import type { TSchema } from "typebox";
7
-
8
9
  const id = Type.String({ minLength: 1, maxLength: 128 });
9
10
  const text = Type.String({ minLength: 1, maxLength: 24000 });
10
11
  const name = Type.String({ minLength: 1, maxLength: 64, pattern: "^[^\\r\\n\\x00-\\x1f]+$" });
@@ -12,43 +13,80 @@ const mode = Type.Union([Type.Literal("roundRobin"), Type.Literal("parallel")],
12
13
  const members = Type.Array(Type.Object({ agentId: id, role: Type.String({ minLength: 1, maxLength: 4000 }) }, { additionalProperties: false }), {
13
14
  minItems: 1, maxItems: 50, description: "仅 create,必填:当前分支中的固定成员及职责;成员须版本兼容且未加入其他未关闭团队。",
14
15
  });
15
- const run = {
16
- instruction: Type.String({ ...text, description: "仅 run,必填:本批次的任务、约束与交付要求。" }), mode,
17
- rounds: Type.Optional(Type.Integer({ minimum: 1, maximum: 10, description: "仅 run:每位成员的轮数,默认 2。" })),
18
- maxConcurrent: Type.Optional(Type.Integer({ minimum: 1, maximum: 50, description: "仅 run:roundRobin 默认且只允许 1;parallel 默认 min(2, 成员数)。不得超过成员数;待处理和执行中的发言都计入上限。" })),
19
- timeoutSeconds: Type.Optional(Type.Integer({ minimum: 1, maximum: 86400, description: "仅 run:整批期限,默认 900 秒,包含所有轮次和等待。到期停止安排并尝试中止尚未人工接管的工作,之后仍须核对实际状态;pause 则允许在途工作收尾。" })),
20
- };
21
- const page = {
22
- afterSeq: Type.Optional(Type.Integer({ minimum: 0, description: "仅 read:读取序号大于此值的消息,默认 0,不得超过 lastSeq。返回 nextAfterSeq 用于继续消息列表;单条续读使用 seq/offset,省略 afterSeq。先补完被截断的正文,再继续列表。" })),
23
- seq: Type.Optional(Type.Integer({ minimum: 1, description: "仅 read:指定一条消息,配合该消息的 nextOffset 续读。未指定时从 afterSeq 后顺序读取。" })),
24
- offset: Type.Optional(Type.Integer({ minimum: 0, description: "仅 read:指定 seq 的正文 UTF-16 字符偏移,默认 0;非零时必须同时提供 seq。" })),
25
- limit: Type.Optional(Type.Integer({ minimum: 1, maximum: 16000, description: "仅 read:本页所有消息正文合计的 UTF-16 字符上限,默认 16000,不是消息条数或 token 数;不含元数据和 Markdown 格式开销。" })),
16
+ const runFields = {
17
+ instruction: nullable(Type.String({ ...text, description: "仅 run,必填:本批次的任务、约束与交付要求。" })),
18
+ mode: nullable(mode),
19
+ rounds: nullable(Type.Integer({ minimum: 1, maximum: 10, description: "仅 run:每位成员的轮数,默认 2。" })),
20
+ maxConcurrent: nullable(Type.Integer({ minimum: 1, maximum: 50, description: "仅 run:roundRobin 默认且只允许 1;parallel 默认 min(2, 成员数)。不得超过成员数;待处理和执行中的发言都计入上限。" })),
21
+ timeoutSeconds: nullable(Type.Integer({ minimum: 1, maximum: 86400, description: "仅 run:整批期限,默认 900 秒,包含所有轮次和等待。到期停止安排并尝试中止尚未人工接管的工作,之后仍须核对实际状态;pause 则允许在途工作收尾。" })),
26
22
  };
27
23
  const object = <T extends Record<string, TSchema>>(fields: T) => Type.Object(fields, { additionalProperties: false });
28
24
  export const teamParametersSchema = object({
29
- action: Type.Union(["create", "list", "status", "read", "run", "pause", "close"].map(v => Type.Literal(v)), {
30
- description: "create 必填 name/brief/members;list 只接受 action;status/pause/close 必填 teamId;read 必填 teamId,可选 afterSeq/seq/offset/limit;run 必填 teamId/instruction/mode,可选 rounds/maxConcurrent/timeoutSeconds。只传对应 action 的字段。run 前须确认所有成员存活、idle、可参与团队且无待处理私人输入,团队未关闭,也无运行批次或在途工作。close 前先暂停并等待在途工作结束。",
25
+ action: Type.Union(["create", "list", "status", "run", "pause", "close"].map(value => Type.Literal(value)), {
26
+ description: "create 必填 name/brief/members;list 只需 action;status/pause/close 必填 teamId;run 必填 teamId/instruction/mode,可选 rounds/maxConcurrent/timeoutSeconds。动作只读取自己的字段;其他已声明字段忽略。run 前须确认所有成员存活、idle、可参与团队且无待处理私人输入,团队未关闭,也无运行批次或在途工作。close 前先暂停并等待在途工作结束。",
31
27
  }),
32
- teamId: Type.Optional(id), name: Type.Optional(name), brief: Type.Optional(text), members: Type.Optional(members),
33
- instruction: Type.Optional(run.instruction), mode: Type.Optional(mode), rounds: run.rounds, maxConcurrent: run.maxConcurrent, timeoutSeconds: run.timeoutSeconds, ...page,
28
+ teamId: nullable(id), name: nullable(name), brief: nullable(text), members: nullable(members), ...runFields,
34
29
  });
35
- export const teamActionSchema = Type.Union([
36
- object({ action: Type.Literal("create"), name, brief: text, members }),
37
- object({ action: Type.Literal("list") }),
38
- ...["status", "pause", "close"].map(v => object({ action: Type.Literal(v), teamId: id })),
39
- object({ action: Type.Literal("run"), teamId: id, ...run }),
40
- object({ action: Type.Literal("read"), teamId: id, ...page }),
41
- ]);
30
+
31
+ export type TeamMember = { agentId: string; role: string };
32
+ export type TeamInput =
33
+ | { action: "create"; name: string; brief: string; members: TeamMember[] }
34
+ | { action: "list" }
35
+ | { action: "status" | "pause" | "close"; teamId: string }
36
+ | { action: "run"; teamId: string; instruction: string; mode: "roundRobin" | "parallel"; rounds?: number; maxConcurrent?: number; timeoutSeconds?: number };
37
+
38
+ const validTeam = schema(teamParametersSchema);
39
+
40
+ export function teamInput(value: unknown): TeamInput {
41
+ const args = inputObject(value, validTeam, "团队参数格式无效");
42
+ const action = args.action as TeamInput["action"];
43
+ switch (action) {
44
+ case "create":
45
+ return { action, name: need("create", args, "name"), brief: need("create", args, "brief"), members: need("create", args, "members") };
46
+ case "list":
47
+ return { action };
48
+ case "status":
49
+ case "pause":
50
+ case "close":
51
+ return { action, teamId: need(action, args, "teamId") };
52
+ case "run":
53
+ return { action, teamId: need("run", args, "teamId"), instruction: need("run", args, "instruction"), mode: need("run", args, "mode"),
54
+ ...(args.rounds === undefined ? {} : { rounds: args.rounds as number }),
55
+ ...(args.maxConcurrent === undefined ? {} : { maxConcurrent: args.maxConcurrent as number }),
56
+ ...(args.timeoutSeconds === undefined ? {} : { timeoutSeconds: args.timeoutSeconds as number }) };
57
+ default:
58
+ throw new Error("未知的团队 action。");
59
+ }
60
+ }
61
+
62
+ const page = {
63
+ afterSeq: nullable(Type.Integer({ minimum: 0, description: "仅 read:读取序号大于此值的消息,默认 0,不得超过 lastSeq。返回 nextAfterSeq 用于继续消息列表;单条续读使用 seq/offset,省略 afterSeq。先补完被截断的正文,再继续列表。" })),
64
+ seq: nullable(Type.Integer({ minimum: 1, description: "仅 read:指定一条消息,配合该消息的 nextOffset 续读。未指定时从 afterSeq 后顺序读取。" })),
65
+ offset: nullable(Type.Integer({ minimum: 0, description: "仅 read:指定 seq 的正文 UTF-16 字符偏移,默认 0;非零时必须同时提供 seq。" })),
66
+ limit: nullable(Type.Integer({ minimum: 1, maximum: 16000, description: "仅 read:本页所有消息正文合计的 UTF-16 字符上限,默认 16000,不是消息条数或 token 数;不含元数据和 Markdown 格式开销。" })),
67
+ };
42
68
  export const teamMessageParametersSchema = object({
43
69
  action: Type.Union([Type.Literal("read"), Type.Literal("post")], {
44
- description: "read 必填 teamId,只可选 afterSeq/seq/offset/limit,禁止 text/replyTo;post 必填 teamId/text,只可选 replyTo,禁止所有分页字段。",
45
- }), teamId: id, text: Type.Optional(Type.String({ ...text, description: "仅 post,必填:向全体成员公开的原文。" })),
46
- replyTo: Type.Optional(Type.Integer({ minimum: 1, description: "仅 post:回复已有消息的 seq。" })), ...page,
70
+ description: "read 必填 teamId,可选 afterSeq/seq/offset/limit;post 必填 teamId/text,可选 replyTo。动作只读取自己的字段;其他已声明字段忽略。",
71
+ }), teamId: nullable(id), text: nullable(Type.String({ ...text, description: "仅 post,必填:向全体成员公开的原文。" })),
72
+ replyTo: nullable(Type.Integer({ minimum: 1, description: "仅 post:回复已有消息的 seq。" })), ...page,
47
73
  });
48
- export const teamMessageActionSchema = Type.Union([
49
- object({ action: Type.Literal("read"), teamId: id, ...page }),
50
- object({ action: Type.Literal("post"), teamId: id, text, replyTo: Type.Optional(Type.Integer({ minimum: 1 })) }),
51
- ]);
74
+
75
+ export type TeamMessageInput =
76
+ | { action: "read"; teamId: string; afterSeq?: number; seq?: number; offset?: number; limit?: number }
77
+ | { action: "post"; teamId: string; text: string; replyTo?: number };
78
+
79
+ const validMessage = schema(teamMessageParametersSchema);
80
+
81
+ export function teamMessageInput(value: unknown): TeamMessageInput {
82
+ const args = inputObject(value, validMessage, "团队消息参数格式无效");
83
+ if (args.action === "read") return { action: "read", teamId: need("read", args, "teamId"),
84
+ ...(pick(args, ["afterSeq", "seq", "offset", "limit"]) as Pick<Extract<TeamMessageInput, { action: "read" }>, "afterSeq" | "seq" | "offset" | "limit">) };
85
+ if (args.action === "post") return { action: "post", teamId: need("post", args, "teamId"), text: need("post", args, "text"),
86
+ ...(args.replyTo === undefined ? {} : { replyTo: args.replyTo as number }) };
87
+ throw new Error("未知的团队消息 action。");
88
+ }
89
+
52
90
  const message = object({ seq: Type.Integer(), operationId: Type.String(),
53
91
  author: object({ kind: Type.Union([Type.Literal("user"), Type.Literal("coordinator"), Type.Literal("member")]), agentId: Type.Optional(id), name: Type.String() }),
54
92
  text: Type.String(), replyTo: Type.Optional(Type.Integer()), runId: Type.Optional(id),
@@ -67,5 +105,5 @@ const ownedView = object({ teamId: id, name: Type.String(), brief: Type.String()
67
105
  run: Type.Optional(object({ runId: id, instruction: Type.String(), mode, state: Type.Union([Type.Literal("running"), Type.Literal("paused"), Type.Literal("finished")]),
68
106
  rounds: Type.Integer(), maxConcurrent: Type.Integer(), deadlineAt: Type.Number(), scheduled: Type.Integer(), published: Type.Integer(), reason: Type.Optional(Type.String()) })) });
69
107
  const view = Type.Union([ownedView, object({ teamId: id, name: Type.String(), closed: Type.Boolean(), owned: Type.Literal(false) })]);
70
- export const teamResultSchema = Type.Union([view, object({ teams: Type.Array(view) }), teamPageSchema]);
108
+ export const teamResultSchema = Type.Union([view, object({ teams: Type.Array(view) })]);
71
109
  export const teamMessageResultSchema = Type.Union([teamPageSchema, object({ teamId: id, seq: Type.Integer(), status: Type.Literal("stored") })]);
@@ -1,7 +1,6 @@
1
1
  import type { Context } from "@deepseek-ai/cordis";
2
2
  import type { Agent } from "@deepseek-ai/dsh-agent";
3
3
  import { dshParameters, dshOutputSchema } from "../../integrations/dsh/schema.js";
4
- import type { Static } from "typebox";
5
4
  import type { TeamService } from "../../services/agents/team-service.js";
6
5
  import type { AgentService } from "../../services/agents/agent-service.js";
7
6
  import type { AgentOwner } from "../../model/agent.js";
@@ -12,31 +11,23 @@ import { schema } from "../../common/schema.js";
12
11
  import { markdownFields } from "../../common/markdown.js";
13
12
  import { teamMessagesMarkdown } from "../../integrations/team-context.js";
14
13
  import { executeTeam, executeTeamMessage } from "./actions.js";
15
- import { teamDescription, teamMessageDescription, teamParametersSchema, teamActionSchema, teamResultSchema,
16
- teamMessageParametersSchema, teamMessageActionSchema, teamMessageResultSchema } from "./contract.js";
14
+ import { teamDescription, teamMessageDescription, teamInput, teamMessageInput, teamParametersSchema, teamResultSchema,
15
+ teamMessageParametersSchema, teamMessageResultSchema } from "./contract.js";
17
16
 
18
17
  export function registerDshTeamTool(ctx: Context, agent: Agent, service: TeamService, agents: AgentService, owner: AgentOwner, check: () => void) {
19
- const validators = new Map(teamActionSchema.anyOf.map(row => [row.properties.action.const, schema(row)]));
20
18
  const validOutput = schema(teamResultSchema);
21
19
  ctx.tools.register({ name: "pbot_team", description: teamDescription, parameters: dshParameters(teamParametersSchema),
22
- output: {
23
- schema: dshOutputSchema(teamResultSchema),
24
- render: (args, value) => [{ type: "text", text: object(args) && args.action === "read"
25
- ? teamMessagesMarkdown(value as unknown as ReturnType<TeamService["read"]>) : "# 团队\n\n" + markdownFields(value) }],
26
- },
20
+ output: { schema: dshOutputSchema(teamResultSchema), render: (_args, value) => [{ type: "text", text: "# 团队\n\n" + markdownFields(value) }] },
27
21
  async execute(args, exec) {
28
22
  check(); exec.signal.throwIfAborted();
29
- requireThat(exec.agent === agent && object(args) && typeof args.action === "string", "团队工具会话或参数无效。");
30
- const valid = validators.get(args.action);
31
- requireThat(valid && valid(args), "团队参数无效。");
32
- const value = executeTeam(service, agents, args as Static<typeof teamParametersSchema>, owner).structuredContent;
23
+ requireThat(exec.agent === agent, "团队工具不属于当前会话。");
24
+ const value = executeTeam(service, agents, teamInput(args), owner).structuredContent;
33
25
  requireThat(validOutput(value), "团队输出不符合契约。"); return value;
34
26
  },
35
27
  });
36
28
  }
37
29
 
38
30
  export function registerDshTeamMessageTool(ctx: Context, agent: Agent, service: TeamService, actor: () => TeamActor, check: () => void) {
39
- const validators = new Map<string, ReturnType<typeof schema>>(teamMessageActionSchema.anyOf.map(row => [row.properties.action.const, schema(row)]));
40
31
  const validOutput = schema(teamMessageResultSchema);
41
32
  ctx.tools.register({ name: "pbot_team_message", description: teamMessageDescription, parameters: dshParameters(teamMessageParametersSchema),
42
33
  output: {
@@ -46,10 +37,8 @@ export function registerDshTeamMessageTool(ctx: Context, agent: Agent, service:
46
37
  },
47
38
  async execute(args, exec) {
48
39
  check(); exec.signal.throwIfAborted();
49
- requireThat(exec.agent === agent && object(args) && typeof args.action === "string", "团队消息会话或参数无效。");
50
- const valid = validators.get(args.action);
51
- requireThat(valid && valid(args), "团队消息参数无效。");
52
- const value = executeTeamMessage(service, args as Static<typeof teamMessageParametersSchema>, actor(), exec.callId).structuredContent;
40
+ requireThat(exec.agent === agent, "团队消息工具不属于当前会话。");
41
+ const value = executeTeamMessage(service, teamMessageInput(args), actor(), exec.callId).structuredContent;
53
42
  requireThat(validOutput(value), "团队消息输出不符合契约。"); return value;
54
43
  },
55
44
  });
@@ -3,37 +3,27 @@ import type { TeamService } from "../../services/agents/team-service.js";
3
3
  import type { AgentService } from "../../services/agents/agent-service.js";
4
4
  import type { TeamActor } from "../../model/team.js";
5
5
  import { agentOwner } from "../../integrations/pi/agent-context.js";
6
- import { requireThat } from "../../common/validation.js";
7
- import { schema } from "../../common/schema.js";
8
6
  import type { CallAccess } from "../access.js";
9
7
  import { executeTeam, executeTeamMessage } from "./actions.js";
10
- import { teamDescription, teamMessageDescription, teamParametersSchema, teamActionSchema, teamResultSchema, teamMessageParametersSchema, teamMessageActionSchema, teamMessageResultSchema } from "./contract.js";
8
+ import { teamDescription, teamMessageDescription, teamInput, teamMessageInput, teamParametersSchema, teamResultSchema, teamMessageParametersSchema, teamMessageResultSchema } from "./contract.js";
11
9
 
12
10
  export function teamTool(service: TeamService, agents: AgentService, access: Pick<CallAccess, "check">) {
13
- const validators = new Map(teamActionSchema.anyOf.map(action => [action.properties.action.const, schema(action)]));
14
11
  return defineTool({ name: "pbot_team", label: "团队",
15
12
  description: teamDescription,
16
13
  parameters: teamParametersSchema, outputSchema: teamResultSchema,
17
14
  async execute(_id, args, signal, _update, ctx) {
18
15
  access.check(); signal?.throwIfAborted();
19
- const valid = validators.get(args.action);
20
- requireThat(valid, "未知的团队 action:" + args.action);
21
- requireThat(valid(args), "团队 " + args.action + " 参数无效:" + JSON.stringify(valid.errors));
22
- return executeTeam(service, agents, args, agentOwner(ctx));
16
+ return executeTeam(service, agents, teamInput(args), agentOwner(ctx));
23
17
  },
24
18
  });
25
19
  }
26
20
  export function teamMessageTool(service: TeamService, access: Pick<CallAccess, "check">, actor: (ctx: ExtensionContext) => TeamActor) {
27
- const validators = new Map(teamMessageActionSchema.anyOf.map(action => [action.properties.action.const, schema(action)]));
28
21
  return defineTool({ name: "pbot_team_message", label: "团队消息",
29
22
  description: teamMessageDescription,
30
23
  parameters: teamMessageParametersSchema, outputSchema: teamMessageResultSchema,
31
24
  async execute(id, args, signal, _update, ctx) {
32
25
  access.check(); signal?.throwIfAborted();
33
- const valid = validators.get(args.action);
34
- requireThat(valid, "未知的团队消息 action:" + args.action);
35
- requireThat(valid(args), "团队消息 " + args.action + " 参数无效:" + JSON.stringify(valid.errors));
36
- return executeTeamMessage(service, args, actor(ctx), id);
26
+ return executeTeamMessage(service, teamMessageInput(args), actor(ctx), id);
37
27
  },
38
28
  });
39
29
  }
@@ -1,45 +0,0 @@
1
- import { Type } from "typebox";
2
- export declare const dshAgentParametersSchema: Type.TObject<{
3
- noExtensions: Type.TOptional<Type.TBoolean>;
4
- noPromptTemplates: Type.TOptional<Type.TBoolean>;
5
- noContextFiles: Type.TOptional<Type.TBoolean>;
6
- provider: Type.TOptional<Type.TString>;
7
- model: Type.TOptional<Type.TString>;
8
- thinking: Type.TOptional<Type.TString>;
9
- files: Type.TOptional<Type.TArray<Type.TString>>;
10
- tools: Type.TOptional<Type.TArray<Type.TString>>;
11
- noSkills: Type.TOptional<Type.TBoolean>;
12
- backend: Type.TOptional<Type.TUnion<[Type.TLiteral<"pi">, Type.TLiteral<"dsh">, Type.TLiteral<"claude">]>>;
13
- cycleId: Type.TOptional<Type.TString>;
14
- name: Type.TOptional<Type.TString>;
15
- agentId: Type.TOptional<Type.TString>;
16
- cwd: Type.TOptional<Type.TString>;
17
- message: Type.TOptional<Type.TString>;
18
- delivery: Type.TOptional<Type.TUnion<[Type.TLiteral<"steer">, Type.TLiteral<"followUp">]>>;
19
- prompts: Type.TOptional<Type.TArray<Type.TString>>;
20
- action: Type.TUnion<[Type.TLiteral<"start">, Type.TLiteral<"send">, Type.TLiteral<"rename">, Type.TLiteral<"status">, Type.TLiteral<"list">, Type.TLiteral<"read">, Type.TLiteral<"stop">, Type.TLiteral<"resume">]>;
21
- offset: Type.TOptional<Type.TInteger>;
22
- limit: Type.TOptional<Type.TInteger>;
23
- }>;
24
- export declare const dshAgentActionSchema: Type.TUnion<(Type.TObject<{
25
- backend: Type.TOptional<Type.TUnion<[Type.TLiteral<"pi">, Type.TLiteral<"dsh">, Type.TLiteral<"claude">]>>;
26
- name: Type.TOptional<Type.TString>;
27
- message: Type.TOptional<Type.TString>;
28
- files: Type.TOptional<Type.TArray<Type.TString>>;
29
- prompts: Type.TOptional<Type.TArray<Type.TString>>;
30
- provider: Type.TOptional<Type.TString>;
31
- model: Type.TOptional<Type.TString>;
32
- thinking: Type.TOptional<Type.TString>;
33
- cwd: Type.TOptional<Type.TString>;
34
- tools: Type.TOptional<Type.TArray<Type.TString>>;
35
- noExtensions: Type.TOptional<Type.TBoolean>;
36
- noSkills: Type.TOptional<Type.TBoolean>;
37
- noPromptTemplates: Type.TOptional<Type.TBoolean>;
38
- noContextFiles: Type.TOptional<Type.TBoolean>;
39
- action: Type.TLiteral<"start">;
40
- }> | Type.TObject<{
41
- action: Type.TLiteral<"list">;
42
- }> | Type.TObject<{
43
- action: Type.TLiteral<string>;
44
- agentId: Type.TString;
45
- }>)[]>;
@@ -1,19 +0,0 @@
1
- import { Type } from "../../common/typebox.js";
2
- import { agentActionSchema, agentParametersSchema, agentStartOptions } from "./contract.js";
3
- const options = Type.Omit(Type.Object(agentStartOptions), ["provider", "model", "thinking", "files", "tools", "noExtensions", "noPromptTemplates", "noContextFiles", "noSkills"]).properties;
4
- const native = {
5
- provider: Type.Optional(Type.String({ minLength: 1, maxLength: 128, description: "模型提供方,dsh 使用原生 provider route。省略 backend 时默认继承主会话;指定 backend 时,此参数须与 model 一起提供。Claude 子代理省略此参数。" })),
6
- model: Type.Optional(Type.String({ minLength: 1, maxLength: 128, description: "模型 ID;指定 backend 时也可使用目标公开的完整模型值。默认值:省略 backend 时继承主会话,指定时使用目标配置。" })),
7
- thinking: Type.Optional(Type.String({ minLength: 1, maxLength: 128, description: "思考等级 ID,dsh 使用原生 reasoning effort,可单独指定。默认值:省略 backend 时继承主会话,指定时使用目标配置。" })),
8
- files: Type.Optional(Type.Array(Type.String({ minLength: 1 }), { maxItems: 32, description: "附给子代理的文件;相对原工作区根目录。图片格式与大小遵循所选程序的支持范围。" })),
9
- tools: Type.Optional(Type.Array(Type.String({ minLength: 1 }), { minItems: 1, description: "dsh 子代理填 run_code 内的能力工具名,Pi 子代理填 Pi 工具名。省略时使用所选程序默认工具;未指定 backend 的专家另有 pbot_team_message。Claude 子代理省略此参数并沿用原生配置。" })),
10
- noSkills: Type.Optional(Type.Boolean({ description: "Pi/dsh 子代理关闭技能发现,默认 false;dsh 同时关闭 skill 工具。Claude 子代理省略此参数并沿用原生配置。" })),
11
- };
12
- const omitted = ["noExtensions", "noPromptTemplates", "noContextFiles", "provider", "model", "thinking", "files", "tools", "noSkills"];
13
- export const dshAgentParametersSchema = Type.Object({ ...Type.Omit(agentParametersSchema, omitted).properties, ...native,
14
- noExtensions: agentStartOptions.noExtensions, noPromptTemplates: agentStartOptions.noPromptTemplates, noContextFiles: agentStartOptions.noContextFiles,
15
- }, { additionalProperties: false });
16
- export const dshAgentActionSchema = Type.Union(agentActionSchema.anyOf.map(row => row.properties.action.const === "start"
17
- ? Type.Object({ action: Type.Literal("start"), ...options, ...native,
18
- noExtensions: agentStartOptions.noExtensions, noPromptTemplates: agentStartOptions.noPromptTemplates, noContextFiles: agentStartOptions.noContextFiles,
19
- }, { additionalProperties: false }) : row));
@@ -1,20 +0,0 @@
1
- import { Type } from "typebox";
2
- import { agentActionSchema, agentParametersSchema, agentStartOptions } from "./contract.js";
3
-
4
- const options = Type.Omit(Type.Object(agentStartOptions), ["provider", "model", "thinking", "files", "tools", "noExtensions", "noPromptTemplates", "noContextFiles", "noSkills"]).properties;
5
- const native = {
6
- provider: Type.Optional(Type.String({ minLength: 1, maxLength: 128, description: "模型提供方,dsh 使用原生 provider route。省略 backend 时默认继承主会话;指定 backend 时,此参数须与 model 一起提供。Claude 子代理省略此参数。" })),
7
- model: Type.Optional(Type.String({ minLength: 1, maxLength: 128, description: "模型 ID;指定 backend 时也可使用目标公开的完整模型值。默认值:省略 backend 时继承主会话,指定时使用目标配置。" })),
8
- thinking: Type.Optional(Type.String({ minLength: 1, maxLength: 128, description: "思考等级 ID,dsh 使用原生 reasoning effort,可单独指定。默认值:省略 backend 时继承主会话,指定时使用目标配置。" })),
9
- files: Type.Optional(Type.Array(Type.String({ minLength: 1 }), { maxItems: 32, description: "附给子代理的文件;相对原工作区根目录。图片格式与大小遵循所选程序的支持范围。" })),
10
- tools: Type.Optional(Type.Array(Type.String({ minLength: 1 }), { minItems: 1, description: "dsh 子代理填 run_code 内的能力工具名,Pi 子代理填 Pi 工具名。省略时使用所选程序默认工具;未指定 backend 的专家另有 pbot_team_message。Claude 子代理省略此参数并沿用原生配置。" })),
11
- noSkills: Type.Optional(Type.Boolean({ description: "Pi/dsh 子代理关闭技能发现,默认 false;dsh 同时关闭 skill 工具。Claude 子代理省略此参数并沿用原生配置。" })),
12
- };
13
- const omitted = ["noExtensions", "noPromptTemplates", "noContextFiles", "provider", "model", "thinking", "files", "tools", "noSkills"] as const;
14
- export const dshAgentParametersSchema = Type.Object({ ...Type.Omit(agentParametersSchema, omitted).properties, ...native,
15
- noExtensions: agentStartOptions.noExtensions, noPromptTemplates: agentStartOptions.noPromptTemplates, noContextFiles: agentStartOptions.noContextFiles,
16
- }, { additionalProperties: false });
17
- export const dshAgentActionSchema = Type.Union(agentActionSchema.anyOf.map(row => row.properties.action.const === "start"
18
- ? Type.Object({ action: Type.Literal("start"), ...options, ...native,
19
- noExtensions: agentStartOptions.noExtensions, noPromptTemplates: agentStartOptions.noPromptTemplates, noContextFiles: agentStartOptions.noContextFiles,
20
- }, { additionalProperties: false }) : row));