@flowy-agent-store/protocol 0.1.0-beta.2 → 0.1.0-beta.4

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.mjs CHANGED
@@ -5,7 +5,32 @@
5
5
  * The chat surface uses only opaque public conversation/message IDs. Internal
6
6
  * execution/session/step/attempt IDs never appear in this module.
7
7
  */
8
- const APP_SERVER_PROTOCOL_VERSION = "2026-08-26";
8
+ /**
9
+ * A contract **fingerprint**, not a version number: it must differ from the
10
+ * previous value on any wire change at all, additive included. The shape is
11
+ * `fp-<n>` — a plain counter, so a bump just increments it and no value is ever
12
+ * reused by accident.
13
+ *
14
+ * So it is a **label, not a version**: neither a release number nor a date.
15
+ * Until `fp-1` the values were date stamps (kept below as history), and those
16
+ * dates were **not** the day of the change: consecutive changes advanced the
17
+ * stamp a day each, so they ran ahead of the calendar. The list below is keyed
18
+ * by *value* — read it as "which wire change is this?", never as "when did this
19
+ * ship".
20
+ *
21
+ * `2026-09-16` carried `StoreItem.published_at`; `2026-09-17` added the two MCP
22
+ * declaration write methods (`config/set-mcp`, `config/set-mcp-enabled`);
23
+ * `2026-09-18` adds `config/get-mcp`, the file editor's read of the same file;
24
+ * `2026-09-19` adds the `conversation/list-changed` notification; `2026-09-20`
25
+ * adds the Skill **file tree** read face (`skill/files`, `skill/file`) so a
26
+ * Skill's companion files are readable, not just its bounded manifest summary;
27
+ * `2026-09-21` adds the connector **call proxy** (`connector/call`), so a third
28
+ * party can run an installed MCP tool while the connection and its credentials
29
+ * stay on the host. **`fp-1` changes the shape only** (date stamp → counter): a
30
+ * `2026-…` value invites being read as a release date, and no wire behaviour
31
+ * changed with the rename.
32
+ */
33
+ const APP_SERVER_PROTOCOL_VERSION = "fp-1";
9
34
  //#endregion
10
35
  //#region src/errors.ts
11
36
  var AppServerError = class extends Error {
@@ -77,4 +102,324 @@ function isRetryableError(error) {
77
102
  return (error instanceof AppServerError || error instanceof TransportError) && error.retryable;
78
103
  }
79
104
  //#endregion
80
- export { APP_SERVER_PROTOCOL_VERSION, AppServerError, ProtocolError, RequestTimeoutError, TransportError, formatError, isAppServerError, isRetryableError, isRetryableTransportError };
105
+ //#region src/localized.ts
106
+ /** The other language, i.e. the second link of every fallback chain. */
107
+ function otherLocalizedLang(lang) {
108
+ return lang === "zh" ? "en" : "zh";
109
+ }
110
+ /**
111
+ * Map an i18n language tag (`zh-CN`, `en-US`, `en`) to a variant language.
112
+ * Anything that is not English resolves to `zh`, matching the app default.
113
+ */
114
+ function toLocalizedLang(language) {
115
+ return (language ?? "").toLowerCase().startsWith("en") ? "en" : "zh";
116
+ }
117
+ /** `{field}_{lang}` → `{field}_{other}` → `""`. */
118
+ function pickLocalized(text, lang) {
119
+ if (!text) return "";
120
+ return text[lang]?.trim() || text[otherLocalizedLang(lang)]?.trim() || "";
121
+ }
122
+ /** Resolve a list of localized values, dropping the ones that resolve empty. */
123
+ function pickLocalizedList(list, lang) {
124
+ return (list ?? []).map((item) => pickLocalized(item, lang)).filter((value) => value.length > 0);
125
+ }
126
+ /** Non-empty string variants in fallback order. */
127
+ function variantKeys(field, lang) {
128
+ return [`${field}_${lang}`, `${field}_${otherLocalizedLang(lang)}`];
129
+ }
130
+ /** `{field}_{lang}` → `{field}_{other}` as a string, when either carries text. */
131
+ function pickVariantText(variants, field, lang) {
132
+ for (const key of variantKeys(field, lang)) {
133
+ const value = variants?.[key];
134
+ if (typeof value === "string" && value.trim().length > 0) return value;
135
+ }
136
+ }
137
+ /** `{field}_{lang}` → `{field}_{other}` as a string list, when either is non-empty. */
138
+ function pickVariantList(variants, field, lang) {
139
+ for (const key of variantKeys(field, lang)) {
140
+ const value = variants?.[key];
141
+ if (Array.isArray(value)) {
142
+ const items = value.filter((item) => typeof item === "string" && item.length > 0);
143
+ if (items.length > 0) return items;
144
+ }
145
+ }
146
+ }
147
+ /**
148
+ * Market entry tags. Per D8=A `tags_{lang}` outranks `legacy_tags_{lang}` —
149
+ * the legacy field only fills in when the current one is absent, in either
150
+ * language.
151
+ */
152
+ function pickEntryTags(variants, lang) {
153
+ return pickVariantList(variants, "tags", lang) ?? pickVariantList(variants, "legacy_tags", lang);
154
+ }
155
+ /**
156
+ * A market entry text field: variant first, then the baseline field
157
+ * (`entry.description`, `entry.name`, …) as the last link of the chain.
158
+ */
159
+ function pickEntryText(variants, field, lang, baseline) {
160
+ return pickVariantText(variants, field, lang) ?? baseline ?? void 0;
161
+ }
162
+ //#endregion
163
+ //#region src/conversation-events.ts
164
+ function stringValue(value) {
165
+ return typeof value === "string" ? value : null;
166
+ }
167
+ function numberValue(value) {
168
+ return typeof value === "number" ? value : null;
169
+ }
170
+ function isRecord(value) {
171
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
172
+ }
173
+ /**
174
+ * Strict boolean reader: a wire value that is not a real boolean reads as
175
+ * `null` (unknown) rather than being coerced — "retryable" must never be
176
+ * guessed from a truthy string.
177
+ */
178
+ function booleanValue(value) {
179
+ return typeof value === "boolean" ? value : null;
180
+ }
181
+ /** Flatten a message/activity body to plain text, JSON-encoding anything else. */
182
+ function contentToText(value) {
183
+ if (typeof value === "string") return value;
184
+ if (isRecord(value) && typeof value.content === "string") return value.content;
185
+ return value == null ? "" : JSON.stringify(value);
186
+ }
187
+ /** Activity bodies may be nested one level down or JSON-encoded as a string. */
188
+ function unwrapContent(value) {
189
+ if (typeof value === "string") try {
190
+ return JSON.parse(value);
191
+ } catch {
192
+ return value;
193
+ }
194
+ if (isRecord(value)) {
195
+ const content = value.content;
196
+ if (typeof content === "string") try {
197
+ return JSON.parse(content);
198
+ } catch {
199
+ return value;
200
+ }
201
+ }
202
+ return value;
203
+ }
204
+ /** Shape a thinking body (`message.thinking`, or `message.activity` kind `thinking`). */
205
+ function thinkingData(value) {
206
+ if (isRecord(value) && typeof value.content === "string") return {
207
+ content: value.content,
208
+ subject: stringValue(value.subject),
209
+ status: stringValue(value.status),
210
+ duration: numberValue(value.duration) ?? numberValue(value.duration_ms)
211
+ };
212
+ const candidate = unwrapContent(value);
213
+ if (!isRecord(candidate)) return {
214
+ content: typeof candidate === "string" ? candidate : "",
215
+ subject: null,
216
+ status: null,
217
+ duration: null
218
+ };
219
+ return {
220
+ content: stringValue(candidate.content) ?? "",
221
+ subject: stringValue(candidate.subject),
222
+ status: stringValue(candidate.status),
223
+ duration: numberValue(candidate.duration) ?? numberValue(candidate.duration_ms)
224
+ };
225
+ }
226
+ /** Shape a tips body; `type` is accepted as an alias of `tip_type`. */
227
+ function tipsData(value) {
228
+ const readTip = (data) => {
229
+ const content = stringValue(data.content);
230
+ const tipType = stringValue(data.tip_type) ?? stringValue(data.type);
231
+ if (content === null && tipType === null) return null;
232
+ return {
233
+ content: content ?? "",
234
+ tipType: tipType ?? "info"
235
+ };
236
+ };
237
+ if (isRecord(value)) {
238
+ const direct = readTip(value);
239
+ if (direct) return direct;
240
+ }
241
+ const candidate = unwrapContent(value);
242
+ if (isRecord(candidate)) {
243
+ const parsed = readTip(candidate);
244
+ if (parsed) return parsed;
245
+ }
246
+ if (candidate != null && !isRecord(candidate)) return {
247
+ content: String(candidate),
248
+ tipType: "info"
249
+ };
250
+ return {
251
+ content: "",
252
+ tipType: "info"
253
+ };
254
+ }
255
+ /** Shape a tool-call body; `null` when the body carries nothing tool-shaped. */
256
+ function toolCallData(value) {
257
+ const candidate = unwrapContent(value);
258
+ if (!isRecord(candidate)) return null;
259
+ const name = stringValue(candidate.name) ?? stringValue(candidate.tool_name) ?? stringValue(candidate.tool);
260
+ if (!name && candidate.args === void 0 && candidate.arguments === void 0 && candidate.output === void 0 && candidate.result === void 0) return null;
261
+ return {
262
+ name,
263
+ args: candidate.args ?? candidate.arguments ?? candidate.input,
264
+ output: candidate.output ?? candidate.result,
265
+ status: stringValue(candidate.status)
266
+ };
267
+ }
268
+ /**
269
+ * Shape a plan body; `null` when there is no usable step list.
270
+ *
271
+ * `step` is accepted as an alias of `content`: the tool's own argument is named
272
+ * `step`, and the producer normalizes it to `content` for the plan row — a row
273
+ * written before that normalization (or by a producer that skipped it) would
274
+ * otherwise render as an empty plan.
275
+ */
276
+ function planData(value) {
277
+ const candidate = unwrapContent(value);
278
+ if (!isRecord(candidate) || !Array.isArray(candidate.entries)) return null;
279
+ const entries = candidate.entries.flatMap((raw) => {
280
+ if (!isRecord(raw)) return [];
281
+ const content = stringValue(raw.content) ?? stringValue(raw.step);
282
+ if (!content) return [];
283
+ return [{
284
+ content,
285
+ status: planStepStatus(raw.status)
286
+ }];
287
+ });
288
+ return entries.length > 0 ? { entries } : null;
289
+ }
290
+ /** Unknown/absent step statuses read as `pending` — never as "done". */
291
+ function planStepStatus(value) {
292
+ return value === "completed" || value === "in_progress" ? value : "pending";
293
+ }
294
+ /** Shape a `context.usage` payload; `null` when nothing was actually measured. */
295
+ function parseContextUsage(value) {
296
+ if (!isRecord(value)) return null;
297
+ const used = typeof value.used_tokens === "number" ? value.used_tokens : 0;
298
+ const window = typeof value.window_tokens === "number" ? value.window_tokens : 0;
299
+ if (used <= 0 || window <= 0) return null;
300
+ const rawPercent = typeof value.percent === "number" ? value.percent : used / window * 100;
301
+ return {
302
+ used_tokens: used,
303
+ window_tokens: window,
304
+ percent: Math.min(100, Math.max(0, rawPercent)),
305
+ updated_at: typeof value.updated_at === "number" ? value.updated_at : Date.now(),
306
+ source: typeof value.source === "string" ? value.source : "measured"
307
+ };
308
+ }
309
+ /**
310
+ * Shape a per-turn `usage` body (W9 / R14); `null` when the runtime reported
311
+ * nothing usable.
312
+ *
313
+ * A missing side is **unknown**, not zero: half a bill is not a bill, so a
314
+ * frame that only carries `input_tokens` decodes to `null` rather than to a
315
+ * total that silently drops the output side. Same rule for an all-zero report —
316
+ * "no tokens reported" must never render as "this turn was free".
317
+ */
318
+ function parseTurnUsage(value) {
319
+ if (!isRecord(value)) return null;
320
+ const input = typeof value.input_tokens === "number" ? value.input_tokens : null;
321
+ const output = typeof value.output_tokens === "number" ? value.output_tokens : null;
322
+ if (input === null || output === null) return null;
323
+ if (input <= 0 && output <= 0) return null;
324
+ return {
325
+ input_tokens: input,
326
+ output_tokens: output,
327
+ total_tokens: typeof value.total_tokens === "number" ? value.total_tokens : input + output
328
+ };
329
+ }
330
+ /**
331
+ * Decode one `conversation/event` frame into a typed body.
332
+ *
333
+ * An activity frame whose `kind` is `thinking` is normalized to
334
+ * `message.thinking` (the server has two spellings for the same thing).
335
+ */
336
+ function decodeConversationEvent(event) {
337
+ const payload = event.payload ?? {};
338
+ const messageId = () => stringValue(payload.message_id);
339
+ const createdAt = () => numberValue(payload.created_at);
340
+ if (event.event_type === "message.activity" && stringValue(payload.kind) === "thinking") return {
341
+ kind: "message.thinking",
342
+ event,
343
+ messageId: messageId(),
344
+ createdAt: createdAt(),
345
+ thinking: thinkingData(payload),
346
+ replace: payload.replace === true
347
+ };
348
+ switch (event.event_type) {
349
+ case "message.created": return {
350
+ kind: "message.created",
351
+ event,
352
+ messageId: messageId(),
353
+ createdAt: createdAt(),
354
+ content: payload.content ?? ""
355
+ };
356
+ case "message.delta": return {
357
+ kind: "message.delta",
358
+ event,
359
+ messageId: messageId(),
360
+ createdAt: createdAt(),
361
+ delta: stringValue(payload.content) ?? "",
362
+ replace: payload.replace === true
363
+ };
364
+ case "message.thinking": return {
365
+ kind: "message.thinking",
366
+ event,
367
+ messageId: messageId(),
368
+ createdAt: createdAt(),
369
+ thinking: thinkingData(payload),
370
+ replace: payload.replace === true
371
+ };
372
+ case "message.tool": return {
373
+ kind: "message.tool",
374
+ event,
375
+ messageId: messageId(),
376
+ createdAt: createdAt(),
377
+ tool: toolCallData(payload)
378
+ };
379
+ case "message.tips": return {
380
+ kind: "message.tips",
381
+ event,
382
+ messageId: messageId(),
383
+ createdAt: createdAt(),
384
+ tips: tipsData(payload)
385
+ };
386
+ case "message.error": return {
387
+ kind: "message.error",
388
+ event,
389
+ messageId: messageId(),
390
+ createdAt: createdAt(),
391
+ message: stringValue(payload.message),
392
+ code: stringValue(payload.code),
393
+ retryable: booleanValue(payload.retryable)
394
+ };
395
+ case "message.activity": {
396
+ const activityKind = stringValue(payload.kind) ?? "activity";
397
+ return {
398
+ kind: "message.activity",
399
+ event,
400
+ messageId: messageId(),
401
+ createdAt: createdAt(),
402
+ activityKind,
403
+ content: payload.content ?? "",
404
+ usage: activityKind === "turn_completed" ? parseTurnUsage(payload.usage) : null
405
+ };
406
+ }
407
+ case "turn.status": return {
408
+ kind: "turn.status",
409
+ event,
410
+ running: stringValue(payload.status) === "running"
411
+ };
412
+ case "context.usage": return {
413
+ kind: "context.usage",
414
+ event,
415
+ usage: parseContextUsage(payload.context_usage)
416
+ };
417
+ default: return {
418
+ kind: "unknown",
419
+ event,
420
+ eventType: event.event_type
421
+ };
422
+ }
423
+ }
424
+ //#endregion
425
+ export { APP_SERVER_PROTOCOL_VERSION, AppServerError, ProtocolError, RequestTimeoutError, TransportError, booleanValue, contentToText, decodeConversationEvent, formatError, isAppServerError, isRecord, isRetryableError, isRetryableTransportError, numberValue, otherLocalizedLang, parseContextUsage, parseTurnUsage, pickEntryTags, pickEntryText, pickLocalized, pickLocalizedList, pickVariantList, pickVariantText, planData, stringValue, thinkingData, tipsData, toLocalizedLang, toolCallData };
package/package.json CHANGED
@@ -1,29 +1,38 @@
1
- {
2
- "name": "@flowy-agent-store/protocol",
3
- "version": "0.1.0-beta.2",
4
- "description": "Versioned App Server Protocol types and wire errors (no runtime, no DOM/Node deps)",
5
- "type": "module",
6
- "license": "Apache-2.0",
7
- "exports": {
8
- ".": {
9
- "types": "./dist/index.d.mts",
10
- "import": "./dist/index.mjs",
11
- "require": "./dist/index.cjs"
12
- },
13
- "./package.json": "./package.json"
14
- },
15
- "files": [
16
- "dist"
17
- ],
18
- "scripts": {
19
- "build": "tsdown",
20
- "prepack": "tsdown"
21
- },
22
- "publishConfig": {
23
- "access": "public",
24
- "registry": "https://registry.npmjs.org/"
25
- },
26
- "main": "./dist/index.cjs",
27
- "module": "./dist/index.mjs",
28
- "types": "./dist/index.d.mts"
29
- }
1
+ {
2
+ "name": "@flowy-agent-store/protocol",
3
+ "version": "0.1.0-beta.4",
4
+ "description": "Versioned App Server Protocol types and wire errors (no runtime, no DOM/Node deps)",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "engines": {
8
+ "node": ">=22"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/Michael-Lfx/allo.git",
13
+ "directory": "web/packages/protocol"
14
+ },
15
+ "sideEffects": false,
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.mts",
19
+ "import": "./dist/index.mjs",
20
+ "require": "./dist/index.cjs"
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "scripts": {
28
+ "build": "tsdown",
29
+ "prepack": "tsdown"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public",
33
+ "registry": "https://registry.npmjs.org/"
34
+ },
35
+ "main": "./dist/index.cjs",
36
+ "module": "./dist/index.mjs",
37
+ "types": "./dist/index.d.mts"
38
+ }