pi-tsgw 1.3.0 → 1.3.2

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/README.md CHANGED
@@ -25,6 +25,6 @@ pi install npm:pi-tsgw
25
25
  将占位地址替换为实际网关地址,通过 `/login` 选择 `tsgw` 配置密钥,然后执行 `/reload`。
26
26
  使用 `/tsgw` 查看目录状态,`/tsgw-refresh` 强制刷新。图片与分类能力需要 Pi 1.2+。
27
27
 
28
- `/insagent` 显式安装标准子代理并自动重载当前会话:覆盖代理目录中的当前定义,再删除已退出插件清单的 `tsgw-*.md` 文件;其他文件不动。
28
+ `/insagent` 显式安装标准子代理:覆盖当前定义,删除已退出清单的 `tsgw-*.md`,在代理目录的 `settings.json` 中启用 Agent 并停用 Pi Web 内置代理,最后自动重载当前会话;其他自定义代理和无关设置保留。
29
29
 
30
30
  开发维护约定见 [AGENTS.md](https://github.com/tossp/pi-tsgw/blob/main/AGENTS.md)。
@@ -9,7 +9,7 @@ function report(ctx: ExtensionCommandContext, message: string, level: "info" | "
9
9
  /** 仅显式命令写入代理目录,不在启动或重载事件中同步。 */
10
10
  export function registerAgentInstallCommand(pi: ExtensionAPI): void {
11
11
  pi.registerCommand("insagent", {
12
- description: "覆盖安装 TSGW 标准子代理、清理旧 tsgw-*.md 定义并重载当前会话",
12
+ description: "覆盖安装 TSGW 标准子代理、清理旧定义、停用内置代理并重载当前会话",
13
13
  handler: async (args, ctx) => {
14
14
  if (args.trim()) {
15
15
  report(ctx, "用法:/insagent(不接收参数)。", "error");
@@ -25,7 +25,7 @@ export function registerAgentInstallCommand(pi: ExtensionAPI): void {
25
25
  return;
26
26
  }
27
27
  report(ctx, `已写入 ${result.installed.length} 个 TSGW 子代理,清理 ${result.removed.length} 个旧定义:` +
28
- `${result.directory}。正在重载当前会话;其他已打开的主会话需各自重载。`, "info");
28
+ `${result.directory}。已停用 Pi Web 内置代理,正在重载当前会话;其他已打开的主会话需各自重载。`, "info");
29
29
  // 重载使旧上下文失效,之后不再访问 ctx 或旧运行器。
30
30
  await ctx.reload();
31
31
  },
@@ -1,4 +1,4 @@
1
- import { mkdirSync, readdirSync, unlinkSync, writeFileSync } from "node:fs";
1
+ import { existsSync, mkdirSync, readFileSync, readdirSync, unlinkSync, writeFileSync } from "node:fs";
2
2
  import { join } from "node:path";
3
3
  import { standardAgentProfiles } from "./profiles.ts";
4
4
 
@@ -8,7 +8,31 @@ export interface AgentInstallation {
8
8
  removed: string[];
9
9
  }
10
10
 
11
- /** 先覆盖当前定义,全部写入成功后才清理退出清单的旧定义;不维护额外状态。 */
11
+ /** 沿用 Pi Web 的设置:启用 Agent 工具,按名称停用内置档案,不关闭整个功能。 */
12
+ function disableBuiltInProfiles(directory: string): void {
13
+ const path = join(directory, "settings.json");
14
+ let stored: unknown = {};
15
+ if (existsSync(path)) {
16
+ try {
17
+ stored = JSON.parse(readFileSync(path, "utf8"));
18
+ } catch (error) {
19
+ throw new Error("无法读取或解析子代理 settings.json", { cause: error });
20
+ }
21
+ }
22
+ if (stored === null || typeof stored !== "object" || Array.isArray(stored)) {
23
+ throw new Error("子代理 settings.json 必须是 JSON 对象");
24
+ }
25
+ const settings = stored as Record<string, unknown>;
26
+ const disabled = Array.isArray(settings.disabledBuiltIns) ? [...settings.disabledBuiltIns] : [];
27
+ for (const name of ["general-purpose", "explore", "plan"]) {
28
+ if (!disabled.some(entry => typeof entry === "string" && entry.trim().toLowerCase() === name)) disabled.push(name);
29
+ }
30
+ writeFileSync(path, `${JSON.stringify({
31
+ ...settings, version: 1, builtInEnabled: true, disabledBuiltIns: disabled,
32
+ }, null, 2)}\n`, { encoding: "utf8", mode: 0o600 });
33
+ }
34
+
35
+ /** 先写当前定义并清理旧定义,再更新宿主设置;不维护额外状态。 */
12
36
  export function installStandardAgents(agentDir: string): AgentInstallation {
13
37
  const directory = join(agentDir, "agents");
14
38
  const profiles = standardAgentProfiles();
@@ -23,5 +47,6 @@ export function installStandardAgents(agentDir: string): AgentInstallation {
23
47
  unlinkSync(join(directory, name));
24
48
  removed.push(name);
25
49
  }
50
+ disableBuiltInProfiles(directory);
26
51
  return { directory, installed: [...currentFiles], removed };
27
52
  }
@@ -41,10 +41,10 @@ function failureText(decision: Decision): string {
41
41
  (decision.selectionGuide ? `\n\n${decision.selectionGuide}` : "");
42
42
  }
43
43
 
44
- /** 保持原工具的执行与 UI 契约,不把其他同名但不同参数的工具当作 Pi Web Agent。 */
44
+ /** 按参数契约识别兼容 Agent,不把宿主的工具暴露方式作为选型前提。 */
45
45
  function hasAgent(pi: ExtensionAPI): boolean {
46
46
  const tool = (pi.getAllTools?.() ?? []).find(entry => entry.name === "Agent");
47
- if (!tool || tool.exposure !== "model-only" || !isPlainObject(tool.parameters)) return false;
47
+ if (!tool || !isPlainObject(tool.parameters)) return false;
48
48
  const properties = tool.parameters.properties;
49
49
  return isPlainObject(properties) && ["prompt", "model", "resume"].every(name =>
50
50
  isPlainObject(properties[name]) && properties[name].type === "string");
@@ -189,7 +189,7 @@ export function registerDelegation(pi: ExtensionAPI): void {
189
189
  }
190
190
 
191
191
  pi.on("tool_call", async (event, ctx): Promise<ToolCallEventResult | undefined> => {
192
- if (event.toolName !== "Agent" || event.parentToolCallId || supplied(event.input.resume) || supplied(event.input.model)) return;
192
+ if (event.toolName !== "Agent" || supplied(event.input.resume) || supplied(event.input.model)) return;
193
193
  if (!active || !hasAgent(pi)) return;
194
194
  const caller = ctx.signal;
195
195
  let attempt = attempts.get(event.toolCallId);
@@ -1,4 +1,5 @@
1
1
  import { Type } from "typebox";
2
+ import { StringEnum } from "@earendil-works/pi-ai";
2
3
  import { normalizeImageParameters, type ImageOperation } from "#tsgw/models/adapter/images/parameters";
3
4
 
4
5
  export const DEFAULT_IMAGE_MODEL = "gpt-image-2.5-sunburst";
@@ -13,16 +14,17 @@ export const imageSourceSchema = Type.Union([
13
14
  Type.Object({ file_id: Type.String({ minLength: 1, description: "上游已有图片的文件 ID。" }) }, { additionalProperties: false }),
14
15
  ], { description: "恰好一个来源:path、image_url 或 file_id;不可混用。" });
15
16
 
17
+ // Type.Enum 仅输出 enum;Moonshot 拒绝联合分支中缺 type 的枚举,使用宿主显式 string 表达。
16
18
  const common = {
17
19
  prompt: Type.String({ minLength: 1, maxLength: 32_000, description: "图片生成或编辑要求,最多32000字符。" }),
18
- model: Type.Optional(Type.Enum(IMAGE_MODELS, { description: "默认 gpt-image-2.5-sunburst,适合成稿;AI 草稿任务可选 gpt-image-2.5-flare,常配 quality=low。" })),
19
- quality: optional(Type.Enum(["auto", "low", "medium", "high", "xhigh", "max"], { description: "质量,默认 auto;选择 Flare 不会自动改为 low。" })),
20
+ model: Type.Optional(StringEnum(IMAGE_MODELS, { description: "默认 gpt-image-2.5-sunburst,适合成稿;AI 草稿任务可选 gpt-image-2.5-flare,常配 quality=low。" })),
21
+ quality: optional(StringEnum(["auto", "low", "medium", "high", "xhigh", "max"], { description: "质量,默认 auto;选择 Flare 不会自动改为 low。" })),
20
22
  size: optional(Type.String({ description: "默认 auto;也可宽x高,均为16倍数、边≤3840、宽高比≤3、像素655360..8294400。" })),
21
- background: optional(Type.Enum(["auto", "opaque", "transparent"], { description: "背景,默认 auto。透明背景需 PNG/WebP。" })),
22
- output_format: optional(Type.Enum(["png", "jpeg", "webp"], { description: "输出格式,默认 png;原始字节不转码。" })),
23
+ background: optional(StringEnum(["auto", "opaque", "transparent"], { description: "背景,默认 auto。透明背景需 PNG/WebP。" })),
24
+ output_format: optional(StringEnum(["png", "jpeg", "webp"], { description: "输出格式,默认 png;原始字节不转码。" })),
23
25
  output_compression: optional(Type.Integer({ minimum: 0, maximum: 100, description: "仅用于 JPEG/WebP 的压缩质量;省略采用上游默认100。" })),
24
26
  n: optional(Type.Integer({ minimum: 1, maximum: 10, description: "返回图片数量;默认1,最多10。" })),
25
- moderation: optional(Type.Enum(["auto", "low"], { description: "审核强度,默认 auto。" })),
27
+ moderation: optional(StringEnum(["auto", "low"], { description: "审核强度,默认 auto。" })),
26
28
  stream: optional(Type.Boolean({ description: "默认 false;支持解析流式响应,不承诺界面渐进预览。" })),
27
29
  partial_images: optional(Type.Integer({ minimum: 0, maximum: 3, description: "仅 stream=true 时使用;默认0。最终仅返回完成图。" })),
28
30
  safety_identifier: optional(Type.String({ maxLength: 128, description: "可选安全标识,最长128字符。" })),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-tsgw",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "面向 TOSSP AIH gateway 的 Pi 扩展:按厂商维护聊天与图片模型目录及思考策略,支持四种聊天协议、图片生成与编辑工具,以及原生 System One 分类模型。",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -2,7 +2,7 @@
2
2
  name: tsgw-delegate-model-selection
3
3
  description: 鼓励将适合的任务派给独立后台子代理。直接调用 Agent 并提供完整任务,TSGW 仅在新建且未指定模型时自动分类选型;不需要阅读选型表或编写分类脚本。
4
4
  license: MIT
5
- compatibility: 自动选型需要 Pi 1.2+、兼容的 model-only Agent 工具,以及可用的 tsgw 分类和聊天模型。
5
+ compatibility: 自动选型需要 Pi 1.2+、参数兼容的 Agent 工具,以及可用的 tsgw 分类和聊天模型。
6
6
  ---
7
7
 
8
8
  # 子代理派遣
@@ -11,10 +11,11 @@ compatibility: 自动选型需要 Pi 1.2+、兼容的 model-only Agent 工具,
11
11
 
12
12
  ## 调用
13
13
 
14
+ - 当前 PiWeb 将 `Agent`、`get_subagent_result`、`steer_subagent` 注册为 `model-only`,三者只能由主模型直接调用,不得放入 codemode 或其他工具包装器;`tools.get_subagent_result(...)` 等写法无效。出现工具不存在错误时,不猜名称、不重复搜索或重试嵌套调用。独立派遣可在同一回复中发起多个直接 `Agent` 调用。
14
15
  - 新建子代理:直接调用宿主 `Agent`,填写 `prompt`、`description`、适合的 `subagent_type` 和执行选项。没有明确模型要求时省略 `model`;插件在创建前完成一次分类选型并补入模型和思考档位,原 `Agent` 随即执行。
15
16
  - 独立任务优先 `run_in_background: true`;前置依赖未完成或存在并发修改冲突时不要假装独立。不要重复派遣已在执行的任务。
16
17
  - 用户指定模型、项目固定模型或审阅组合时,直接显式传入 `model`(`provider/modelId:level`),跳过分类。正式架构/设计文档独立审阅默认 `tsgw/gpt-6-astra:max`;固定双模型审阅分别使用 `tsgw/kimi-k3.1-pro:high` 与 `tsgw/gpt-6-astra:max`,不能把一份双审任务交给一次自动选择。
17
- - 续接已有会话时传 `resume`,不传新模型;查询结果、补充指令及子代理后续轮次不触发分类,也不运行中换模型。
18
+ - 自动选型只适用于创建子代理且未指定 `model`;续接已有会话时传 `resume`,不传新模型,即使续接调用没有 `model` 也不再选型。直接调用 `get_subagent_result` 查询结果、`steer_subagent` 补充指令及子代理后续轮次均不触发分类,也不运行中换模型。
18
19
  - 分类失败、结果未决或选定模型失效时,本次自动创建被阻止。工具返回具体失败类型和同源选型建议表;发起调用的主代理按指导自行选择当前可用模型,显式指定 `Agent.model` 后重新派遣,不重试分类器或自动更换分类模型。建议表标明最近核验的可用性,未核验时先查询当前目录与会话范围;用户指定和固定审阅要求优先。
19
20
  - 选型上限为 15 秒;超时只阻止本次自动创建,不暂停后续独立调用的自动选型。调用取消或会话结束不视为超时,不自动重新派遣。
20
21