Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
refactor(lizi-mcps): 标量字符串数组参数统一套四形态容错
Zhang Yunjin24ae0318ee1a
diff --git a/packages/lizi-mcps/src/__tests__/_array-param.test.ts b/packages/lizi-mcps/src/__tests__/_array-param.test.tsnew file mode 100644index 0000000000..c4be38a056--- /dev/null+++ b/packages/lizi-mcps/src/__tests__/_array-param.test.ts@@ -0,0 +1,46 @@+import { describe, expect, it } from 'vitest';+import { z } from 'zod';+import { coerceStringArrayParam, stringArrayParam } from '../xdt-helper/_array-param.js';++// 与 imagesListSchema(PR #5649)/queued_message_ids(#6369)实测的四类误传形态+// 对齐;这里守住共享实现本身,各工具的接线用例在各自的测试文件里。+describe('coerceStringArrayParam', () => {+ const schema = stringArrayParam(z.array(z.string().min(1)).min(2));++ it('passes a flat array through unchanged', () => {+ expect(schema.parse(['a', 'b'])).toEqual(['a', 'b']);+ });++ it('flattens a one-level-nested array', () => {+ expect(schema.parse([['a', 'b']])).toEqual(['a', 'b']);+ expect(schema.parse(['a', ['b']])).toEqual(['a', 'b']);+ });++ it('unwraps the XML-style {item:[...]} wrapper', () => {+ expect(schema.parse({ item: ['a', 'b'] })).toEqual(['a', 'b']);+ });++ it('wraps a single-element-collapsed {item:string} and bare string', () => {+ const unbounded = stringArrayParam(z.array(z.string().min(1)));+ expect(unbounded.parse({ item: 'a' })).toEqual(['a']);+ expect(unbounded.parse('a')).toEqual(['a']);+ });++ it('keeps element and bound checks after coercion', () => {+ expect(() => schema.parse({ foo: ['a', 'b'] })).toThrow(); // 无 item 键:不是数组+ expect(() => schema.parse({ item: '' })).toThrow(); // 折叠出空串:元素 min(1)+ expect(() => schema.parse({ item: 'a' })).toThrow(); // 折叠出单元素:数组 min(2)+ expect(() => schema.parse(['a', { x: 1 }])).toThrow(); // 非字符串条目+ });++ it('coerces without breaking the optional wrapper', () => {+ const optional = stringArrayParam(z.array(z.string()).max(2)).optional();+ expect(optional.parse(undefined)).toBeUndefined();+ expect(optional.parse({ item: ['a', 'b'] })).toEqual(['a', 'b']);+ });++ it('is a no-op for non-coercible junk handed straight to zod', () => {+ expect(coerceStringArrayParam(42)).toBe(42);+ expect(coerceStringArrayParam(null)).toBe(null);+ });+});diff --git a/packages/lizi-mcps/src/__tests__/sessionTags.test.ts b/packages/lizi-mcps/src/__tests__/sessionTags.test.tsindex 36ad84abf5..0ac9f85bcd 100644--- a/packages/lizi-mcps/src/__tests__/sessionTags.test.ts+++ b/packages/lizi-mcps/src/__tests__/sessionTags.test.ts@@ -116,4 +116,15 @@ describe('task label tools', () => {).errorCode,).toBe('INVALID_CONFIRMATION');});++ it('unwraps the XML-style {item:[...]} folded tag id arrays', async () => {+ // glm 兼容层把数组参数序列化成 <item> 重复标签(images/queued_message_ids 同病)。+ const h = setup();+ await h.call('add_task_tags', { tag_ids: { item: ['red', 'blue'] } });+ expect(h.execute).toHaveBeenLastCalledWith('a', {+ action: 'attach',+ sessionIds: ['a'],+ tagIds: ['red', 'blue'],+ });+ });});diff --git a/packages/lizi-mcps/src/__tests__/skillhubTools.test.ts b/packages/lizi-mcps/src/__tests__/skillhubTools.test.tsindex 06ad3b5e94..2b6ccbf40a 100644--- a/packages/lizi-mcps/src/__tests__/skillhubTools.test.ts+++ b/packages/lizi-mcps/src/__tests__/skillhubTools.test.ts@@ -49,6 +49,20 @@ describe('SkillHub helper tools', () => {expect((await registry.call('publish_skill', { ...publication, sessionId: 'forged' })).isError).toBe(true);expect(execute).not.toHaveBeenCalled();});+ it('unwraps the XML-style {item:[...]} folded slug arrays on publish', async () => {+ // glm 兼容层把数组参数序列化成 <item> 重复标签(images/queued_message_ids 同病)。+ const { registry, execute } = fixture();+ const result = await registry.call('publish_skill', {+ ...publication,+ tags: { item: ['release-notes', 'team'] },+ });+ expect(result.isError).toBeUndefined();+ expect(execute).toHaveBeenCalledWith(+ { action: 'publish', input: { ...publication, tags: ['release-notes', 'team'] } },+ expect.objectContaining({ sessionId: 'task' }),+ );+ });+it.each([{ ...publication, visibility: undefined },{ ...publication, name: '../other' },diff --git a/packages/lizi-mcps/src/xdt-helper/_array-param.ts b/packages/lizi-mcps/src/xdt-helper/_array-param.tsnew file mode 100644index 0000000000..d3268b687e--- /dev/null+++ b/packages/lizi-mcps/src/xdt-helper/_array-param.ts@@ -0,0 +1,31 @@+import { z } from 'zod';++/**+ * 数组类参数的误传形态自愈,imagesListSchema(PR #5649)与 merge 的+ * queued_message_ids(#6369)同一套口径的共享版。glm 等 openai-completions 兼容层+ * 把数组参数序列化成 <item> 重复标签,单元素时折叠成标量,实测四类形态:+ * 多套一层 [[a],[b]] → 拍平;XML 风格 {item:[a,b]} → 取 item;单元素折叠+ * {item:a} 与裸标量 a → [a]。其余原样交给 zod 报错。+ * 只用于「顶层是标量字符串数组」的参数;对象数组(如 ask_user_question_async+ * 的 questions)的折叠发生在内层节点,本函数救不了,不要套。+ */+export function coerceStringArrayParam(value: unknown): unknown {+ if (Array.isArray(value)) {+ return value.some((item) => Array.isArray(item)) ? (value as unknown[]).flat(1) : value;+ }+ if (value && typeof value === 'object') {+ const item = (value as { item?: unknown }).item;+ if (item === undefined) return value;+ value = item;+ if (Array.isArray(value)) {+ return value.some((x) => Array.isArray(x)) ? (value as unknown[]).flat(1) : value;+ }+ }+ if (typeof value === 'string' && value.trim().length > 0) return [value];+ return value;+}++/** 标量字符串数组参数的统一入口:coerce + 原数组 schema,元素校验照常生效。 */+export function stringArrayParam<T extends z.ZodTypeAny>(schema: T) {+ return z.preprocess(coerceStringArrayParam, schema);+}diff --git a/packages/lizi-mcps/src/xdt-helper/merge_queued_messages.ts b/packages/lizi-mcps/src/xdt-helper/merge_queued_messages.tsindex 253f2b2a89..9ee5808b33 100644--- a/packages/lizi-mcps/src/xdt-helper/merge_queued_messages.ts+++ b/packages/lizi-mcps/src/xdt-helper/merge_queued_messages.ts@@ -3,6 +3,7 @@ import { z } from 'zod';import type { XdtHelperToolRegistry } from '../lizi_xdtHelperToolRegistry.js';import type { ControlResult } from '../types.js';import { errorPayload, okPayload } from './_payload.js';+import { stringArrayParam } from './_array-param.js';import type { QueuedMessageControlErrorCode } from './update_queued_message.js';import type { WorkerQueuedMessageEntry } from './get_worker_queue_status.js';@@ -25,28 +26,6 @@ export interface MergeQueuedMessagesDeps {>;}-/**- * 数组类参数的误传形态自愈,与 imagesListSchema(PR #5649)同一套口径。glm 等- * openai-completions 兼容层把数组参数序列化成 <item> 重复标签,单元素时折叠成- * 标量,实测四类形态:多套一层 [[a],[b]] → 拍平;XML 风格 {item:[a,b]} → 取- * item;单元素折叠 {item:a} 与裸标量 a → [a]。其余原样交给 zod 报错。- */-function coerceStringArrayParam(value: unknown): unknown {- if (Array.isArray(value)) {- return value.some((item) => Array.isArray(item)) ? (value as unknown[]).flat(1) : value;- }- if (value && typeof value === 'object') {- const item = (value as { item?: unknown }).item;- if (item === undefined) return value;- value = item;- if (Array.isArray(value)) {- return value.some((x) => Array.isArray(x)) ? (value as unknown[]).flat(1) : value;- }- }- if (typeof value === 'string' && value.trim().length > 0) return [value];- return value;-}-const DESCRIPTION ='原子合并至少两条连续、尚未消费且由当前 Lead 发出的 worker 排队消息。' +'最前一条保留原位置与 queued_message_id,其余条目一次移除并结清暂存回调。' +@@ -66,11 +45,7 @@ export function registerMergeQueuedMessagesTool(.string().min(1).describe('目标 worker 的 worker_id 或 session_id 任一'),- queued_message_ids: z- .preprocess(- coerceStringArrayParam,- z.array(z.string().min(1)).min(2),- )+ queued_message_ids: stringArrayParam(z.array(z.string().min(1)).min(2)).describe('按当前队列顺序排列的至少两个连续消息 id,不得重复; 必须是扁平字符串数组, 如 ["id_a", "id_b"]',),diff --git a/packages/lizi-mcps/src/xdt-helper/session_tags.ts b/packages/lizi-mcps/src/xdt-helper/session_tags.tsindex cc3c56a853..0f9a9a9e04 100644--- a/packages/lizi-mcps/src/xdt-helper/session_tags.ts+++ b/packages/lizi-mcps/src/xdt-helper/session_tags.ts@@ -3,6 +3,7 @@ import { TASK_TAG_COLORS, type TaskTagRequest, type TaskTagResult } from '@cindyimport type { LiziMcpSessionContext } from '../types.js';import type { XdtHelperToolRegistry } from '../lizi_xdtHelperToolRegistry.js';import { okPayload, errorPayload } from './_payload.js';+import { stringArrayParam } from './_array-param.js';import { encodeConfirmationToken, decodeConfirmationToken } from './_confirmation_token.js';export type SessionTagsCallback = (@@ -17,8 +18,8 @@ export function registerSessionTagTools(},) {const id = z.string().trim().min(1).max(128);- const sessionIds = z.array(id).min(1).max(100).optional();- const tagIds = z.array(id).min(1).max(32);+ const sessionIds = stringArrayParam(z.array(id).min(1).max(100)).optional();+ const tagIds = stringArrayParam(z.array(id).min(1).max(32));const invoke = async (request: TaskTagRequest) => {const caller = deps.getSessionContext().sessionId;if (!caller) throw new Error('[NO_SESSION_CONTEXT] No current Cindy task');@@ -137,8 +138,8 @@ export function registerSessionTagTools('control','reorder_task_tags(tag_ids: string[], expected_order: string[]): Reorder the complete label directory. Both arrays must contain every label ID exactly once. expected_order is the current order from list_task_tags; tag_ids is the requested order. Concurrent changes require rereading. The first seven labels appear in the task menu.',{- tag_ids: z.array(id).min(1).max(256),- expected_order: z.array(id).min(1).max(256),+ tag_ids: stringArrayParam(z.array(id).min(1).max(256)),+ expected_order: stringArrayParam(z.array(id).min(1).max(256)),},(a) =>invoke({diff --git a/packages/lizi-mcps/src/xdt-helper/skillhub.ts b/packages/lizi-mcps/src/xdt-helper/skillhub.tsindex 01508ab6e6..207331430c 100644--- a/packages/lizi-mcps/src/xdt-helper/skillhub.ts+++ b/packages/lizi-mcps/src/xdt-helper/skillhub.ts@@ -2,6 +2,7 @@ import { z } from 'zod';import type { XdtHelperToolRegistry } from '../lizi_xdtHelperToolRegistry.js';import type { ControlResult, LiziMcpSessionContext } from '../types.js';import { errorPayload, okPayload } from './_payload.js';+import { stringArrayParam } from './_array-param.js';const slug = z.string().trim().regex(/^[a-z0-9][a-z0-9-]{0,127}$/);const publishShape = {@@ -11,9 +12,9 @@ const publishShape = {visibility: z.enum(['public', 'private', 'shared']).optional(),display_name: z.string().trim().min(1).max(200).optional(),summary: z.string().trim().min(1).max(1000).optional(),- tags: z.array(slug).max(20).optional(),+ tags: stringArrayParam(z.array(slug).max(20)).optional(),changelog: z.string().trim().max(5000).optional(),- visible_slugs: z.array(slug).max(100).optional(),+ visible_slugs: stringArrayParam(z.array(slug).max(100)).optional(),};export type SkillhubPublishInput = z.infer<z.ZodObject<typeof publishShape>>;export type SkillhubAgentRequest =