@flowy-agent-store/protocol 0.1.0-beta.3 → 0.1.0-beta.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/dist/index.cjs CHANGED
@@ -6,7 +6,59 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
6
  * The chat surface uses only opaque public conversation/message IDs. Internal
7
7
  * execution/session/step/attempt IDs never appear in this module.
8
8
  */
9
- const APP_SERVER_PROTOCOL_VERSION = "2026-08-26";
9
+ /**
10
+ * A contract **fingerprint**, not a version number: it must differ from the
11
+ * previous value on any wire change at all, additive included. The shape is
12
+ * `fp-<n>` — a plain counter, so a bump just increments it and no value is ever
13
+ * reused by accident.
14
+ *
15
+ * So it is a **label, not a version**: neither a release number nor a date.
16
+ * Until `fp-1` the values were date stamps (kept below as history), and those
17
+ * dates were **not** the day of the change: consecutive changes advanced the
18
+ * stamp a day each, so they ran ahead of the calendar. The list below is keyed
19
+ * by *value* — read it as "which wire change is this?", never as "when did this
20
+ * ship".
21
+ *
22
+ * `2026-09-16` carried `StoreItem.published_at`; `2026-09-17` added the two MCP
23
+ * declaration write methods (`config/set-mcp`, `config/set-mcp-enabled`);
24
+ * `2026-09-18` adds `config/get-mcp`, the file editor's read of the same file;
25
+ * `2026-09-19` adds the `conversation/list-changed` notification; `2026-09-20`
26
+ * adds the Skill **file tree** read face (`skill/files`, `skill/file`) so a
27
+ * Skill's companion files are readable, not just its bounded manifest summary;
28
+ * `2026-09-21` adds the connector **call proxy** (`connector/call`), so a third
29
+ * party can run an installed MCP tool while the connection and its credentials
30
+ * stay on the host. **`fp-1` changes the shape only** (date stamp → counter): a
31
+ * `2026-…` value invites being read as a release date, and no wire behaviour
32
+ * changed with the rename. **`fp-2` carries the tools' parameters**
33
+ * (`ConnectorTool.input_schema`, plus `tools_truncated` on `ConnectorDetail`
34
+ * and `ConnectorProbeResult`): a caller that must *name* a tool to be granted it
35
+ * should be able to read what that tool takes. It is the counterpart of the
36
+ * host's `[connector_proxy]` grant moving from one tool at a time to the
37
+ * connector (doc `26`). **`fp-3` makes a Skill selectable per turn**:
38
+ * `conversation/send` gains an optional `mentions` list whose only honoured kind
39
+ * is `skill`, so one turn can mount a Skill's instructions without rewriting the
40
+ * conversation's create-time snapshot (doc `27` 阶段 1). **`fp-4` lets a
41
+ * conversation be created as an installed expert**: `conversation/create` gains
42
+ * an optional `agent_id`, whose Definition supplies the chat's preset identity
43
+ * plus its own Skill and Connector fences — frozen at creation, since none of
44
+ * those keys is mutable afterwards (doc `27` 阶段 2a). **`fp-5` opens a Team's
45
+ * Leader the same way**: an optional `team_id` runs the `team/run` orchestration
46
+ * (members, template, fences) but stops before the goal turn, so the client
47
+ * speaks first; `agent_id` and `team_id` are mutually exclusive (阶段 2b).
48
+ * **`fp-6` makes the model and the reasoning level selectable per call**:
49
+ * `conversation/send` and `agent/run` each gain an optional `model` and
50
+ * `reasoning_effort`. The scope is deliberately the **conversation** (send) and
51
+ * the **run** (agent/run), not "just this turn": the Nomi runtime is built from
52
+ * the persisted row, so a send-carried value is a sticky switch that takes
53
+ * effect on that very turn (doc `29` §4). `ConversationView` also gains
54
+ * `reasoning_effort`, because a setting that can be written by three methods but
55
+ * read back by none is not a setting a client can honour (doc `29` §5.5).
56
+ * **`fp-7` adds the `zip` marketplace source kind**: `market/add` accepts
57
+ * `source_kind: "zip"`, one archive whose root *is* the market root. The
58
+ * official bundles move to it because the old `url` form made a first fetch
59
+ * mirror 14,714 files (611 MiB) for `experts` alone (doc `30`).
60
+ */
61
+ const APP_SERVER_PROTOCOL_VERSION = "fp-7";
10
62
  //#endregion
11
63
  //#region src/errors.ts
12
64
  var AppServerError = class extends Error {
@@ -78,12 +130,352 @@ function isRetryableError(error) {
78
130
  return (error instanceof AppServerError || error instanceof TransportError) && error.retryable;
79
131
  }
80
132
  //#endregion
133
+ //#region src/localized.ts
134
+ /** The other language, i.e. the second link of every fallback chain. */
135
+ function otherLocalizedLang(lang) {
136
+ return lang === "zh" ? "en" : "zh";
137
+ }
138
+ /**
139
+ * Map an i18n language tag (`zh-CN`, `en-US`, `en`) to a variant language.
140
+ * Anything that is not English resolves to `zh`, matching the app default.
141
+ */
142
+ function toLocalizedLang(language) {
143
+ return (language ?? "").toLowerCase().startsWith("en") ? "en" : "zh";
144
+ }
145
+ /** `{field}_{lang}` → `{field}_{other}` → `""`. */
146
+ function pickLocalized(text, lang) {
147
+ if (!text) return "";
148
+ return text[lang]?.trim() || text[otherLocalizedLang(lang)]?.trim() || "";
149
+ }
150
+ /** Resolve a list of localized values, dropping the ones that resolve empty. */
151
+ function pickLocalizedList(list, lang) {
152
+ return (list ?? []).map((item) => pickLocalized(item, lang)).filter((value) => value.length > 0);
153
+ }
154
+ /** Non-empty string variants in fallback order. */
155
+ function variantKeys(field, lang) {
156
+ return [`${field}_${lang}`, `${field}_${otherLocalizedLang(lang)}`];
157
+ }
158
+ /** `{field}_{lang}` → `{field}_{other}` as a string, when either carries text. */
159
+ function pickVariantText(variants, field, lang) {
160
+ for (const key of variantKeys(field, lang)) {
161
+ const value = variants?.[key];
162
+ if (typeof value === "string" && value.trim().length > 0) return value;
163
+ }
164
+ }
165
+ /** `{field}_{lang}` → `{field}_{other}` as a string list, when either is non-empty. */
166
+ function pickVariantList(variants, field, lang) {
167
+ for (const key of variantKeys(field, lang)) {
168
+ const value = variants?.[key];
169
+ if (Array.isArray(value)) {
170
+ const items = value.filter((item) => typeof item === "string" && item.length > 0);
171
+ if (items.length > 0) return items;
172
+ }
173
+ }
174
+ }
175
+ /**
176
+ * Market entry tags. Per D8=A `tags_{lang}` outranks `legacy_tags_{lang}` —
177
+ * the legacy field only fills in when the current one is absent, in either
178
+ * language.
179
+ */
180
+ function pickEntryTags(variants, lang) {
181
+ return pickVariantList(variants, "tags", lang) ?? pickVariantList(variants, "legacy_tags", lang);
182
+ }
183
+ /**
184
+ * A market entry text field: variant first, then the baseline field
185
+ * (`entry.description`, `entry.name`, …) as the last link of the chain.
186
+ */
187
+ function pickEntryText(variants, field, lang, baseline) {
188
+ return pickVariantText(variants, field, lang) ?? baseline ?? void 0;
189
+ }
190
+ //#endregion
191
+ //#region src/conversation-events.ts
192
+ function stringValue(value) {
193
+ return typeof value === "string" ? value : null;
194
+ }
195
+ function numberValue(value) {
196
+ return typeof value === "number" ? value : null;
197
+ }
198
+ function isRecord(value) {
199
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
200
+ }
201
+ /**
202
+ * Strict boolean reader: a wire value that is not a real boolean reads as
203
+ * `null` (unknown) rather than being coerced — "retryable" must never be
204
+ * guessed from a truthy string.
205
+ */
206
+ function booleanValue(value) {
207
+ return typeof value === "boolean" ? value : null;
208
+ }
209
+ /** Flatten a message/activity body to plain text, JSON-encoding anything else. */
210
+ function contentToText(value) {
211
+ if (typeof value === "string") return value;
212
+ if (isRecord(value) && typeof value.content === "string") return value.content;
213
+ return value == null ? "" : JSON.stringify(value);
214
+ }
215
+ /** Activity bodies may be nested one level down or JSON-encoded as a string. */
216
+ function unwrapContent(value) {
217
+ if (typeof value === "string") try {
218
+ return JSON.parse(value);
219
+ } catch {
220
+ return value;
221
+ }
222
+ if (isRecord(value)) {
223
+ const content = value.content;
224
+ if (typeof content === "string") try {
225
+ return JSON.parse(content);
226
+ } catch {
227
+ return value;
228
+ }
229
+ }
230
+ return value;
231
+ }
232
+ /** Shape a thinking body (`message.thinking`, or `message.activity` kind `thinking`). */
233
+ function thinkingData(value) {
234
+ if (isRecord(value) && typeof value.content === "string") return {
235
+ content: value.content,
236
+ subject: stringValue(value.subject),
237
+ status: stringValue(value.status),
238
+ duration: numberValue(value.duration) ?? numberValue(value.duration_ms)
239
+ };
240
+ const candidate = unwrapContent(value);
241
+ if (!isRecord(candidate)) return {
242
+ content: typeof candidate === "string" ? candidate : "",
243
+ subject: null,
244
+ status: null,
245
+ duration: null
246
+ };
247
+ return {
248
+ content: stringValue(candidate.content) ?? "",
249
+ subject: stringValue(candidate.subject),
250
+ status: stringValue(candidate.status),
251
+ duration: numberValue(candidate.duration) ?? numberValue(candidate.duration_ms)
252
+ };
253
+ }
254
+ /** Shape a tips body; `type` is accepted as an alias of `tip_type`. */
255
+ function tipsData(value) {
256
+ const readTip = (data) => {
257
+ const content = stringValue(data.content);
258
+ const tipType = stringValue(data.tip_type) ?? stringValue(data.type);
259
+ if (content === null && tipType === null) return null;
260
+ return {
261
+ content: content ?? "",
262
+ tipType: tipType ?? "info"
263
+ };
264
+ };
265
+ if (isRecord(value)) {
266
+ const direct = readTip(value);
267
+ if (direct) return direct;
268
+ }
269
+ const candidate = unwrapContent(value);
270
+ if (isRecord(candidate)) {
271
+ const parsed = readTip(candidate);
272
+ if (parsed) return parsed;
273
+ }
274
+ if (candidate != null && !isRecord(candidate)) return {
275
+ content: String(candidate),
276
+ tipType: "info"
277
+ };
278
+ return {
279
+ content: "",
280
+ tipType: "info"
281
+ };
282
+ }
283
+ /** Shape a tool-call body; `null` when the body carries nothing tool-shaped. */
284
+ function toolCallData(value) {
285
+ const candidate = unwrapContent(value);
286
+ if (!isRecord(candidate)) return null;
287
+ const name = stringValue(candidate.name) ?? stringValue(candidate.tool_name) ?? stringValue(candidate.tool);
288
+ if (!name && candidate.args === void 0 && candidate.arguments === void 0 && candidate.output === void 0 && candidate.result === void 0) return null;
289
+ return {
290
+ name,
291
+ args: candidate.args ?? candidate.arguments ?? candidate.input,
292
+ output: candidate.output ?? candidate.result,
293
+ status: stringValue(candidate.status)
294
+ };
295
+ }
296
+ /**
297
+ * Shape a plan body; `null` when there is no usable step list.
298
+ *
299
+ * `step` is accepted as an alias of `content`: the tool's own argument is named
300
+ * `step`, and the producer normalizes it to `content` for the plan row — a row
301
+ * written before that normalization (or by a producer that skipped it) would
302
+ * otherwise render as an empty plan.
303
+ */
304
+ function planData(value) {
305
+ const candidate = unwrapContent(value);
306
+ if (!isRecord(candidate) || !Array.isArray(candidate.entries)) return null;
307
+ const entries = candidate.entries.flatMap((raw) => {
308
+ if (!isRecord(raw)) return [];
309
+ const content = stringValue(raw.content) ?? stringValue(raw.step);
310
+ if (!content) return [];
311
+ return [{
312
+ content,
313
+ status: planStepStatus(raw.status)
314
+ }];
315
+ });
316
+ return entries.length > 0 ? { entries } : null;
317
+ }
318
+ /** Unknown/absent step statuses read as `pending` — never as "done". */
319
+ function planStepStatus(value) {
320
+ return value === "completed" || value === "in_progress" ? value : "pending";
321
+ }
322
+ /** Shape a `context.usage` payload; `null` when nothing was actually measured. */
323
+ function parseContextUsage(value) {
324
+ if (!isRecord(value)) return null;
325
+ const used = typeof value.used_tokens === "number" ? value.used_tokens : 0;
326
+ const window = typeof value.window_tokens === "number" ? value.window_tokens : 0;
327
+ if (used <= 0 || window <= 0) return null;
328
+ const rawPercent = typeof value.percent === "number" ? value.percent : used / window * 100;
329
+ return {
330
+ used_tokens: used,
331
+ window_tokens: window,
332
+ percent: Math.min(100, Math.max(0, rawPercent)),
333
+ updated_at: typeof value.updated_at === "number" ? value.updated_at : Date.now(),
334
+ source: typeof value.source === "string" ? value.source : "measured"
335
+ };
336
+ }
337
+ /**
338
+ * Shape a per-turn `usage` body (W9 / R14); `null` when the runtime reported
339
+ * nothing usable.
340
+ *
341
+ * A missing side is **unknown**, not zero: half a bill is not a bill, so a
342
+ * frame that only carries `input_tokens` decodes to `null` rather than to a
343
+ * total that silently drops the output side. Same rule for an all-zero report —
344
+ * "no tokens reported" must never render as "this turn was free".
345
+ */
346
+ function parseTurnUsage(value) {
347
+ if (!isRecord(value)) return null;
348
+ const input = typeof value.input_tokens === "number" ? value.input_tokens : null;
349
+ const output = typeof value.output_tokens === "number" ? value.output_tokens : null;
350
+ if (input === null || output === null) return null;
351
+ if (input <= 0 && output <= 0) return null;
352
+ return {
353
+ input_tokens: input,
354
+ output_tokens: output,
355
+ total_tokens: typeof value.total_tokens === "number" ? value.total_tokens : input + output
356
+ };
357
+ }
358
+ /**
359
+ * Decode one `conversation/event` frame into a typed body.
360
+ *
361
+ * An activity frame whose `kind` is `thinking` is normalized to
362
+ * `message.thinking` (the server has two spellings for the same thing).
363
+ */
364
+ function decodeConversationEvent(event) {
365
+ const payload = event.payload ?? {};
366
+ const messageId = () => stringValue(payload.message_id);
367
+ const createdAt = () => numberValue(payload.created_at);
368
+ if (event.event_type === "message.activity" && stringValue(payload.kind) === "thinking") return {
369
+ kind: "message.thinking",
370
+ event,
371
+ messageId: messageId(),
372
+ createdAt: createdAt(),
373
+ thinking: thinkingData(payload),
374
+ replace: payload.replace === true
375
+ };
376
+ switch (event.event_type) {
377
+ case "message.created": return {
378
+ kind: "message.created",
379
+ event,
380
+ messageId: messageId(),
381
+ createdAt: createdAt(),
382
+ content: payload.content ?? ""
383
+ };
384
+ case "message.delta": return {
385
+ kind: "message.delta",
386
+ event,
387
+ messageId: messageId(),
388
+ createdAt: createdAt(),
389
+ delta: stringValue(payload.content) ?? "",
390
+ replace: payload.replace === true
391
+ };
392
+ case "message.thinking": return {
393
+ kind: "message.thinking",
394
+ event,
395
+ messageId: messageId(),
396
+ createdAt: createdAt(),
397
+ thinking: thinkingData(payload),
398
+ replace: payload.replace === true
399
+ };
400
+ case "message.tool": return {
401
+ kind: "message.tool",
402
+ event,
403
+ messageId: messageId(),
404
+ createdAt: createdAt(),
405
+ tool: toolCallData(payload)
406
+ };
407
+ case "message.tips": return {
408
+ kind: "message.tips",
409
+ event,
410
+ messageId: messageId(),
411
+ createdAt: createdAt(),
412
+ tips: tipsData(payload)
413
+ };
414
+ case "message.error": return {
415
+ kind: "message.error",
416
+ event,
417
+ messageId: messageId(),
418
+ createdAt: createdAt(),
419
+ message: stringValue(payload.message),
420
+ code: stringValue(payload.code),
421
+ retryable: booleanValue(payload.retryable)
422
+ };
423
+ case "message.activity": {
424
+ const activityKind = stringValue(payload.kind) ?? "activity";
425
+ return {
426
+ kind: "message.activity",
427
+ event,
428
+ messageId: messageId(),
429
+ createdAt: createdAt(),
430
+ activityKind,
431
+ content: payload.content ?? "",
432
+ usage: activityKind === "turn_completed" ? parseTurnUsage(payload.usage) : null
433
+ };
434
+ }
435
+ case "turn.status": return {
436
+ kind: "turn.status",
437
+ event,
438
+ running: stringValue(payload.status) === "running"
439
+ };
440
+ case "context.usage": return {
441
+ kind: "context.usage",
442
+ event,
443
+ usage: parseContextUsage(payload.context_usage)
444
+ };
445
+ default: return {
446
+ kind: "unknown",
447
+ event,
448
+ eventType: event.event_type
449
+ };
450
+ }
451
+ }
452
+ //#endregion
81
453
  exports.APP_SERVER_PROTOCOL_VERSION = APP_SERVER_PROTOCOL_VERSION;
82
454
  exports.AppServerError = AppServerError;
83
455
  exports.ProtocolError = ProtocolError;
84
456
  exports.RequestTimeoutError = RequestTimeoutError;
85
457
  exports.TransportError = TransportError;
458
+ exports.booleanValue = booleanValue;
459
+ exports.contentToText = contentToText;
460
+ exports.decodeConversationEvent = decodeConversationEvent;
86
461
  exports.formatError = formatError;
87
462
  exports.isAppServerError = isAppServerError;
463
+ exports.isRecord = isRecord;
88
464
  exports.isRetryableError = isRetryableError;
89
465
  exports.isRetryableTransportError = isRetryableTransportError;
466
+ exports.numberValue = numberValue;
467
+ exports.otherLocalizedLang = otherLocalizedLang;
468
+ exports.parseContextUsage = parseContextUsage;
469
+ exports.parseTurnUsage = parseTurnUsage;
470
+ exports.pickEntryTags = pickEntryTags;
471
+ exports.pickEntryText = pickEntryText;
472
+ exports.pickLocalized = pickLocalized;
473
+ exports.pickLocalizedList = pickLocalizedList;
474
+ exports.pickVariantList = pickVariantList;
475
+ exports.pickVariantText = pickVariantText;
476
+ exports.planData = planData;
477
+ exports.stringValue = stringValue;
478
+ exports.thinkingData = thinkingData;
479
+ exports.tipsData = tipsData;
480
+ exports.toLocalizedLang = toLocalizedLang;
481
+ exports.toolCallData = toolCallData;