@zenaigc/zenmcp 0.1.5 → 0.1.6

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.
@@ -207,7 +207,9 @@ export class DirectGeneration {
207
207
  }
208
208
  return assets;
209
209
  }
210
- async quote(input) {
210
+ async quote(input) { return (await this.quoteDetailed(input)).quote; }
211
+ /** quote() plus the request and catalog entry it was built from, so presentation needs no second catalog fetch or re-read. */
212
+ async quoteDetailed(input) {
211
213
  const secret = await this.options.tokens.token();
212
214
  const raw = { capability: input.capability, prompt: input.prompt, params: input.params, voice_id: input.voice_id };
213
215
  if (JSON.stringify(scrub(raw, secret)) !== JSON.stringify(raw) || [input.prompt, ...Object.values(input.params ?? {})].some(v => typeof v === 'string' && /zk_(?:live|test)_[A-Za-z0-9_-]+|Bearer\s+\S+/i.test(v)))
@@ -319,7 +321,7 @@ export class DirectGeneration {
319
321
  await writeJson(dir, 'request.json', req);
320
322
  if (!attempts.length)
321
323
  await writeJson(dir, 'state.json', { request_digest: digest, status: 'quoted' });
322
- return { ...req.quote, requestDigest: digest, markdown: `报价 ${quote.price_credits} 积分。请获得用户明确批准后,携带 requestDigest 调用 run_generation。` };
324
+ return { quote: { ...req.quote, requestDigest: digest, markdown: `报价 ${quote.price_credits} 积分。请获得用户明确批准后,携带 requestDigest 调用 run_generation。` }, request: req.request, capability: cap };
323
325
  }
324
326
  finally {
325
327
  await release();
@@ -296,7 +296,8 @@ export class Engine {
296
296
  return out({ path: result.path, checks: await inspectComposited(job, path('conform'), result.path, { effectRanges: result.ranges }) }, [result.path]);
297
297
  }
298
298
  case 'mix': {
299
- const audio = input.audio ?? ((await probe(input.reference)).has_audio ? input.reference : undefined);
299
+ // The analyze step already proved storyboard.source equals a fresh probe of this reference.
300
+ const audio = input.audio ?? (job.storyboard.source.has_audio ? input.reference : undefined);
300
301
  const result = await mixAudio(path('effects'), audio, await file('mixed.mp4'));
301
302
  return out({ ...result, note: result.silent ? '没有音源,静音输出。' : '音源裁至成片时长,短音源补静音、尾部淡出,目标 -14 LUFS;实测见导出检查。' }, [result.path]);
302
303
  }
@@ -143,7 +143,7 @@ export class CloudGateway {
143
143
  const response = await this.fetchImpl(putUrl, {
144
144
  method: "PUT",
145
145
  headers: { "Content-Type": contentType },
146
- body: new Uint8Array(bytes),
146
+ body: bytes,
147
147
  redirect: "error",
148
148
  signal: AbortSignal.timeout(this.uploadTimeoutMs),
149
149
  });
@@ -1,5 +1,6 @@
1
1
  import { execFile, spawn } from "node:child_process";
2
2
  import { promisify } from "node:util";
3
+ import { ByteQueue } from "../timeline/ffmpeg.js";
3
4
  const exec = promisify(execFile);
4
5
  export async function command(bin, args) {
5
6
  return exec(bin, args, { maxBuffer: 16 * 1024 * 1024 });
@@ -22,17 +23,16 @@ export async function* frames(path, bytes) {
22
23
  child.on("close", code => code === 0 ? resolve() : reject(new Error(`decode failed: ${stderr}`)));
23
24
  });
24
25
  void done.catch(() => undefined);
25
- let pending = Buffer.alloc(0);
26
+ // Re-concatenating the pending bytes per pipe chunk copied each frame dozens of times.
27
+ const queue = new ByteQueue();
26
28
  try {
27
29
  for await (const chunk of child.stdout) {
28
- pending = Buffer.concat([pending, chunk]);
29
- while (pending.length >= bytes) {
30
- yield pending.subarray(0, bytes);
31
- pending = pending.subarray(bytes);
32
- }
30
+ queue.push(chunk);
31
+ for (let frame = queue.take(bytes); frame !== null; frame = queue.take(bytes))
32
+ yield frame;
33
33
  }
34
34
  await done;
35
- if (pending.length)
35
+ if (queue.length)
36
36
  throw new Error("partial frame");
37
37
  }
38
38
  finally {
@@ -15,7 +15,7 @@ import { SpeechTools, transcribeInputSchema, cloneInputSchema } from '../speech/
15
15
  import { trimMedia } from '../acquire/trim.js';
16
16
  import { ACCOUNT_URL, ERROR_GUIDANCE, POLL_AFTER_SECONDS, LARGE_AMOUNT_CREDITS, LARGE_BALANCE_RATIO, errorGuidance } from './guidance.js';
17
17
  export const DEFAULT_BASE_URL = 'https://api.zenaigc.com/api/v1';
18
- export const CLIENT_VERSION = '0.1.5';
18
+ export const CLIENT_VERSION = '0.1.6';
19
19
  const inputSchema = z.object({
20
20
  job: jobSchema, reference: z.string().min(1),
21
21
  output: z.object({ duration_seconds: z.number().int(), resolution: z.enum(['480P', '720P', '1080P']), ratio: z.string(), seed: z.number().int().optional() }),
@@ -229,16 +229,11 @@ export function createServer(engine, socialOptions = {}, speechOptions = {}) {
229
229
  return { ...result, markdown: ['| 能力 id | 媒介 | 模型 | 必填素材 | 必填参数 |', '| --- | --- | --- | --- | --- |', ...rows.map(row => `| ${row} |`), '', '下一步:选择能力并调用 quote_generation 免费报价;本地文件上传前须取得用户同意。'].join('\n') };
230
230
  }));
231
231
  server.registerTool('quote_generation', { description: '免费报价,不提交生成;会上传本地素材,须先取得上传同意。报价后一次性展示模型、关键参数、上传文件数和积分,取得明确批准后携带 requestDigest 调用 run_generation。', inputSchema: { directory: z.string(), capability: z.string(), prompt: z.string().optional(), voice_id: z.string().min(1).optional(), voice_authorized: z.boolean().optional(), params: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()])).optional(), inputs: z.record(z.string(), z.string()).optional(), consent_confirmed: z.boolean().optional() } }, guarded(async (i) => {
232
- const result = await direct.quote(i);
233
- // Only read presentation metadata here; the engine remains the execution authority.
234
- const [{ request }, catalog, me] = await Promise.all([
235
- direct.get({ directory: i.directory }),
236
- engine.options.gateway.catalog().catch(() => null),
237
- engine.options.gateway.me().catch(() => null),
238
- ]);
239
- const model = catalog?.modified ? catalog.catalog.capabilities.find(c => c.id === request.capability) : undefined;
232
+ // The engine remains the execution authority; its request and catalog entry are reused for presentation only.
233
+ const { quote: result, request, capability: model } = await direct.quoteDetailed(i);
234
+ const me = await engine.options.gateway.me().catch(() => null);
240
235
  const params = Object.entries(request.params).filter(([key]) => key !== 'prompt').map(([key, value]) => {
241
- const spec = model?.params.find(p => p.key === key);
236
+ const spec = model.params.find(p => p.key === key);
242
237
  return { key, label: spec?.label ?? key, value: spec?.options?.find(o => o.value === value)?.label ?? (typeof value === 'boolean' ? value ? '开启' : '关闭' : value) };
243
238
  });
244
239
  const prompt = String(request.params.prompt ?? '');
@@ -250,7 +245,7 @@ export function createServer(engine, socialOptions = {}, speechOptions = {}) {
250
245
  const large = credits >= LARGE_AMOUNT_CREDITS || (balance !== null && credits > balance * LARGE_BALANCE_RATIO);
251
246
  const confirmation = { kind: 'spend', credits, balance_before: balance, balance_after: balance === null ? null : balance - credits, sufficient, large, params, uploads, prompt_preview: preview };
252
247
  const markdown = [
253
- '### 生成确认', `- 模型:${model?.name ?? request.capability}(${request.capability})`,
248
+ '### 生成确认', `- 模型:${model.name ?? request.capability}(${request.capability})`,
254
249
  `- 参数:${params.length ? params.map(p => `${p.label}=${p.value}`).join(' · ') : '无额外参数'}`,
255
250
  ...(preview ? [`- 提示词:${preview}`] : []),
256
251
  ...(uploads.length ? [`- 上传:本次报价已上传 ${uploads.length} 个本地文件(${uploads.join('、')})`] : []),
@@ -8,6 +8,7 @@ import { sha256File } from '../core/index.js';
8
8
  import { hash } from '../engine/steps.js';
9
9
  import { lockJob, readJson, writeJson } from '../engine/storage.js';
10
10
  import { directory } from '../assets/files.js';
11
+ import { GatewayError } from '../gateway/index.js';
11
12
  import { transcriptionCreateSchema, voiceCreateSchema } from '../contract/cloud-v1.js';
12
13
  import { extractAudio, mediaCommand, probeAudio } from './audio.js';
13
14
  export const transcribeInputSchema = z.object({ path: z.string().min(1), language: z.string().min(1).max(32).optional(), speaker_info: z.boolean().optional(), output: z.string().min(1).optional() });
@@ -102,6 +103,37 @@ export class SpeechTools {
102
103
  await rm(temp, { recursive: true, force: true });
103
104
  }
104
105
  }
106
+ /**
107
+ * Create the cloud task once per receipt, or re-read it by ID. A stored receipt whose upload has
108
+ * since expired answers `asset_not_found`: the server looks the key up before the asset, so that
109
+ * answer proves it holds no task for this key, and the upload is replaced once under a fresh key.
110
+ */
111
+ async submit(dir, body, get, post) {
112
+ const fresh = async () => {
113
+ const next = { key: randomUUID(), body: await body() };
114
+ await writeJson(dir, 'request.json', next);
115
+ return next;
116
+ };
117
+ const stored = await this.load(dir);
118
+ let receipt = stored ?? await fresh();
119
+ let created;
120
+ if (receipt.id)
121
+ created = await get(receipt.id);
122
+ else {
123
+ try {
124
+ created = await post(receipt);
125
+ }
126
+ catch (error) {
127
+ if (!stored || !(error instanceof GatewayError) || error.code !== 'asset_not_found')
128
+ throw error;
129
+ receipt = await fresh();
130
+ created = await post(receipt);
131
+ }
132
+ }
133
+ receipt.id = created.id;
134
+ await writeJson(dir, 'request.json', receipt);
135
+ return { receipt, created };
136
+ }
105
137
  async poll(initial, pending, get, timeoutMs) {
106
138
  const deadline = this.now() + timeoutMs;
107
139
  let current = initial;
@@ -124,15 +156,7 @@ export class SpeechTools {
124
156
  const dir = await this.receiptDir(source, dirname(output), 'transcription', params);
125
157
  const release = await lockJob(dir);
126
158
  try {
127
- let receipt = await this.load(dir);
128
- if (!receipt) {
129
- const asset_id = await this.upload(source, TRANSCRIPTION_MAX_BYTES, 1800);
130
- receipt = { key: randomUUID(), body: { asset_id, ...params } };
131
- await writeJson(dir, 'request.json', receipt);
132
- }
133
- const created = receipt.id ? await this.gateway.getTranscription(receipt.id) : await this.gateway.createTranscription(transcriptionCreateSchema.parse(receipt.body), receipt.key);
134
- receipt.id = created.id;
135
- await writeJson(dir, 'request.json', receipt);
159
+ const { receipt, created } = await this.submit(dir, async () => ({ asset_id: await this.upload(source, TRANSCRIPTION_MAX_BYTES, 1800), ...params }), id => this.gateway.getTranscription(id), r => this.gateway.createTranscription(transcriptionCreateSchema.parse(r.body), r.key));
136
160
  const result = await this.poll(created, t => t.status === 'queued' || t.status === 'running', () => this.gateway.getTranscription(created.id), this.options.transcriptionTimeoutMs ?? 300_000);
137
161
  if (result.status === 'failed')
138
162
  throw new Error(`transcription_failed:${result.error};任务 ${result.id}。`);
@@ -158,16 +182,7 @@ export class SpeechTools {
158
182
  const dir = await this.receiptDir(source, dirname(source), 'voice', { name: input.name, consent_confirmed: input.consent_confirmed });
159
183
  const release = await lockJob(dir);
160
184
  try {
161
- let receipt = await this.load(dir);
162
- if (!receipt) {
163
- const sample_asset_id = await this.upload(source, VOICE_MAX_BYTES, Number.POSITIVE_INFINITY);
164
- const body = { name: input.name, sample_asset_id, consent_confirmed: input.consent_confirmed };
165
- receipt = { key: randomUUID(), body };
166
- await writeJson(dir, 'request.json', receipt);
167
- }
168
- const created = receipt.id ? await this.gateway.getVoice(receipt.id) : await this.gateway.createVoice(voiceCreateSchema.parse(receipt.body), receipt.key);
169
- receipt.id = created.id;
170
- await writeJson(dir, 'request.json', receipt);
185
+ const { receipt, created } = await this.submit(dir, async () => ({ name: input.name, sample_asset_id: await this.upload(source, VOICE_MAX_BYTES, Number.POSITIVE_INFINITY), consent_confirmed: input.consent_confirmed }), id => this.gateway.getVoice(id), r => this.gateway.createVoice(voiceCreateSchema.parse(r.body), r.key));
171
186
  const voice = await this.poll(created, v => v.status === 'training', () => this.gateway.getVoice(created.id), this.options.voiceTimeoutMs ?? 60_000);
172
187
  // Keep provider error text out of tool responses; Voice.error is an open wire string.
173
188
  const safe = safeVoice(voice);
@@ -125,10 +125,15 @@ export async function readRawFrames(stream, frameBytes, onFrame) {
125
125
  }
126
126
  return index;
127
127
  }
128
- class ByteQueue {
128
+ /** FIFO of decoder chunks: each frame is copied once, however the pipe splits it. */
129
+ export class ByteQueue {
129
130
  chunks = [];
130
131
  head = 0;
131
132
  available = 0;
133
+ /** Bytes buffered and not yet taken. */
134
+ get length() {
135
+ return this.available;
136
+ }
132
137
  push(chunk) {
133
138
  if (chunk.length === 0) {
134
139
  return;
@@ -1,6 +1,56 @@
1
+ import { stat } from "node:fs/promises";
2
+ import { resolve } from "node:path";
1
3
  import { TimelineError } from "./errors.js";
2
4
  import { runExec } from "./ffmpeg.js";
5
+ const PROBE_CACHE_LIMIT = 32;
6
+ const probeCache = new Map();
7
+ /**
8
+ * Identity of the bytes currently at `path`. Any rewrite changes mtime/ctime, and a
9
+ * replacement changes the inode, so an equal key means the same content was probed.
10
+ */
11
+ async function fileIdentity(path) {
12
+ try {
13
+ const info = await stat(path, { bigint: true });
14
+ if (!info.isFile()) {
15
+ return null;
16
+ }
17
+ return [resolve(path), info.dev, info.ino, info.size, info.mtimeNs, info.ctimeNs].join("\0");
18
+ }
19
+ catch {
20
+ return null;
21
+ }
22
+ }
23
+ /**
24
+ * Counting frames decodes the whole stream, and one pipeline run asks about the same
25
+ * file from several modules. Results are remembered per file identity for this process;
26
+ * failures are never remembered.
27
+ */
3
28
  export async function probe(path) {
29
+ const key = await fileIdentity(path);
30
+ if (key === null) {
31
+ return probeFile(path);
32
+ }
33
+ let pending = probeCache.get(key);
34
+ if (pending === undefined) {
35
+ pending = probeFile(path).then(async (result) => {
36
+ // A file rewritten while ffprobe ran must not be remembered under its old identity.
37
+ if ((await fileIdentity(path)) !== key) {
38
+ probeCache.delete(key);
39
+ }
40
+ return result;
41
+ });
42
+ pending.catch(() => {
43
+ probeCache.delete(key);
44
+ });
45
+ probeCache.set(key, pending);
46
+ if (probeCache.size > PROBE_CACHE_LIMIT) {
47
+ probeCache.delete(probeCache.keys().next().value);
48
+ }
49
+ }
50
+ const result = await pending;
51
+ return { ...result, fps: { ...result.fps } };
52
+ }
53
+ async function probeFile(path) {
4
54
  // v:0 is ffprobe's specifier for the first video stream, the same stream
5
55
  // ffmpeg selects with -map 0:v:0. Audio is a separate selection so a
6
56
  // later video stream cannot replace the first one or hide the audio.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenaigc/zenmcp",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "zenmcp": "dist/src/mcp/server.js",