@capybara-hq/plugin-sdk 0.1.0-rc.11 → 0.1.0-rc.13
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/cards.d.ts +23 -4
- package/dist/cards.js +35 -6
- package/dist/config-ui.d.ts +22 -0
- package/dist/config-ui.js +64 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/theme.d.ts +1 -1
- package/dist/theme.js +1 -1
- package/package.json +1 -1
package/dist/cards.d.ts
CHANGED
|
@@ -30,9 +30,17 @@ export type ChannelCard = {
|
|
|
30
30
|
kind: "question";
|
|
31
31
|
id: string;
|
|
32
32
|
form: InputForm;
|
|
33
|
-
|
|
33
|
+
/** `skipped`: the person sent a message instead of answering (the run went on with it). */
|
|
34
|
+
state: "pending" | "answered" | "skipped" | "expired" | "cancelled";
|
|
34
35
|
answerSummary?: string;
|
|
35
36
|
webUrl: string;
|
|
37
|
+
/**
|
|
38
|
+
* The person may answer by replying with text instead (pending cards only): `plain` in a DM
|
|
39
|
+
* (any reply reaches the agent), `mention` in a group thread (the reply must mention the bot;
|
|
40
|
+
* the kernel ignores group messages without it). Absent: only the card's own controls work
|
|
41
|
+
* (a sub-agent's question). Render the hint with {@link questionTextReplyHint}.
|
|
42
|
+
*/
|
|
43
|
+
textReply?: "plain" | "mention";
|
|
36
44
|
} | {
|
|
37
45
|
kind: "connect";
|
|
38
46
|
appName: string;
|
|
@@ -56,14 +64,25 @@ export interface OutboundMessage {
|
|
|
56
64
|
cards?: ChannelCard[];
|
|
57
65
|
}
|
|
58
66
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
67
|
+
* Whether IM can draw the question's controls: exactly one enum field (single choice ≤ 5 options /
|
|
68
|
+
* multi-select ≤ 10). A field with `allowOther` qualifies only when the card has `textReply`
|
|
69
|
+
* (pass `{ textReply: card.textReply !== undefined }`): the Other path is then a plain reply, so
|
|
70
|
+
* never draw an Other button. Otherwise the card only links to the web.
|
|
61
71
|
*/
|
|
62
|
-
export declare function questionRenderableInline(form: InputForm
|
|
72
|
+
export declare function questionRenderableInline(form: InputForm, opts?: {
|
|
73
|
+
textReply?: boolean;
|
|
74
|
+
}): boolean;
|
|
63
75
|
/** 校验卡片结构,返回问题列表(空 = 合法)。 */
|
|
64
76
|
export declare function validateChannelCard(card: unknown): string[];
|
|
65
77
|
/** A UI locale. */
|
|
66
78
|
export type Locale = "zh" | "en";
|
|
79
|
+
/** The hint line of a pending question card with `textReply`, as {@link renderCardAsText} writes it. */
|
|
80
|
+
export declare function questionTextReplyHint(mode: "plain" | "mention", locale: Locale): string;
|
|
81
|
+
/**
|
|
82
|
+
* The label of a question card's state, as {@link renderCardAsText} writes it. A state this SDK
|
|
83
|
+
* does not know (a newer kernel) returns the state itself, never `undefined`.
|
|
84
|
+
*/
|
|
85
|
+
export declare function questionStateLabel(state: string, locale: Locale): string;
|
|
67
86
|
/**
|
|
68
87
|
* 不支持卡片的渠道的统一退回文本(含链接);固定话术按 locale 取 zh / en。结果是 Markdown(放进
|
|
69
88
|
* `OutboundMessage.text`,由渠道插件转换):模型可控的字段放在代码里(见 `asCode`),不会产生链接。
|
package/dist/cards.js
CHANGED
|
@@ -3,14 +3,16 @@ import { isHttpUrl } from "./url.js";
|
|
|
3
3
|
const MAX_INLINE_SINGLE = 5;
|
|
4
4
|
const MAX_INLINE_MULTI = 10;
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* Whether IM can draw the question's controls: exactly one enum field (single choice ≤ 5 options /
|
|
7
|
+
* multi-select ≤ 10). A field with `allowOther` qualifies only when the card has `textReply`
|
|
8
|
+
* (pass `{ textReply: card.textReply !== undefined }`): the Other path is then a plain reply, so
|
|
9
|
+
* never draw an Other button. Otherwise the card only links to the web.
|
|
8
10
|
*/
|
|
9
|
-
export function questionRenderableInline(form) {
|
|
11
|
+
export function questionRenderableInline(form, opts) {
|
|
10
12
|
if (form.fields.length !== 1)
|
|
11
13
|
return false;
|
|
12
14
|
const f = form.fields[0];
|
|
13
|
-
if (!f || f.allowOther)
|
|
15
|
+
if (!f || (f.allowOther && opts?.textReply !== true))
|
|
14
16
|
return false;
|
|
15
17
|
if (f.schema.type !== "string" || !("enum" in f.schema))
|
|
16
18
|
return false;
|
|
@@ -20,7 +22,8 @@ export function questionRenderableInline(form) {
|
|
|
20
22
|
const KINDS = ["approval", "question", "connect", "bind"];
|
|
21
23
|
const SCOPES = ["once", "session", "always"];
|
|
22
24
|
const APPROVAL_STATES = ["pending", "approved", "denied", "expired", "cancelled"];
|
|
23
|
-
const QUESTION_STATES = ["pending", "answered", "expired", "cancelled"];
|
|
25
|
+
const QUESTION_STATES = ["pending", "answered", "skipped", "expired", "cancelled"];
|
|
26
|
+
const TEXT_REPLY = ["plain", "mention"];
|
|
24
27
|
const ISO_8601 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})$/;
|
|
25
28
|
const list = (xs) => xs.map((x) => JSON.stringify(x)).join(", ");
|
|
26
29
|
/** 校验卡片结构,返回问题列表(空 = 合法)。 */
|
|
@@ -74,6 +77,8 @@ export function validateChannelCard(card) {
|
|
|
74
77
|
url("webUrl");
|
|
75
78
|
if (c.answerSummary !== undefined && typeof c.answerSummary !== "string")
|
|
76
79
|
out.push("answerSummary must be a string");
|
|
80
|
+
if (c.textReply !== undefined)
|
|
81
|
+
oneOf("textReply", TEXT_REPLY);
|
|
77
82
|
// 不能在 IM 内作答的表单(见 questionRenderableInline)同样合法:渠道只渲染网页链接。
|
|
78
83
|
for (const p of validateInputForm(c.form))
|
|
79
84
|
out.push(`form: ${p}`);
|
|
@@ -138,9 +143,14 @@ const TEXT = {
|
|
|
138
143
|
questionStates: {
|
|
139
144
|
pending: "Pending",
|
|
140
145
|
answered: "Answered",
|
|
146
|
+
skipped: "Skipped (replied instead)",
|
|
141
147
|
expired: "Expired",
|
|
142
148
|
cancelled: "Cancelled",
|
|
143
149
|
},
|
|
150
|
+
textReply: {
|
|
151
|
+
plain: "You can also just reply with text",
|
|
152
|
+
mention: "You can also reply in this thread, mentioning me",
|
|
153
|
+
},
|
|
144
154
|
},
|
|
145
155
|
zh: {
|
|
146
156
|
sep: ":",
|
|
@@ -168,11 +178,28 @@ const TEXT = {
|
|
|
168
178
|
questionStates: {
|
|
169
179
|
pending: "待回答",
|
|
170
180
|
answered: "已回答",
|
|
181
|
+
skipped: "已跳过(已直接回复)",
|
|
171
182
|
expired: "已过期",
|
|
172
183
|
cancelled: "已取消",
|
|
173
184
|
},
|
|
185
|
+
textReply: {
|
|
186
|
+
plain: "也可以直接回复文字",
|
|
187
|
+
mention: "也可以在本话题 @我 回复文字",
|
|
188
|
+
},
|
|
174
189
|
},
|
|
175
190
|
};
|
|
191
|
+
/** The hint line of a pending question card with `textReply`, as {@link renderCardAsText} writes it. */
|
|
192
|
+
export function questionTextReplyHint(mode, locale) {
|
|
193
|
+
return (TEXT[locale] ?? TEXT.en).textReply[mode];
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* The label of a question card's state, as {@link renderCardAsText} writes it. A state this SDK
|
|
197
|
+
* does not know (a newer kernel) returns the state itself, never `undefined`.
|
|
198
|
+
*/
|
|
199
|
+
export function questionStateLabel(state, locale) {
|
|
200
|
+
const states = (TEXT[locale] ?? TEXT.en).questionStates;
|
|
201
|
+
return states[state] ?? state;
|
|
202
|
+
}
|
|
176
203
|
/**
|
|
177
204
|
* 不支持卡片的渠道的统一退回文本(含链接);固定话术按 locale 取 zh / en。结果是 Markdown(放进
|
|
178
205
|
* `OutboundMessage.text`,由渠道插件转换):模型可控的字段放在代码里(见 `asCode`),不会产生链接。
|
|
@@ -202,11 +229,13 @@ export function renderCardAsText(card, locale) {
|
|
|
202
229
|
const title = typeof card.form.title === "string" ? card.form.title : card.form.title[locale];
|
|
203
230
|
const lines = [
|
|
204
231
|
withCode(t.question, asCode(title)),
|
|
205
|
-
line(t.status,
|
|
232
|
+
line(t.status, questionStateLabel(card.state, locale)),
|
|
206
233
|
];
|
|
207
234
|
if (card.answerSummary)
|
|
208
235
|
lines.push(line(t.answer, asCode(card.answerSummary)));
|
|
209
236
|
lines.push(line(card.state === "pending" ? t.answerOnWeb : t.details, card.webUrl));
|
|
237
|
+
if (card.state === "pending" && card.textReply !== undefined)
|
|
238
|
+
lines.push(questionTextReplyHint(card.textReply, locale));
|
|
210
239
|
return lines.join("\n");
|
|
211
240
|
}
|
|
212
241
|
case "connect":
|
package/dist/config-ui.d.ts
CHANGED
|
@@ -10,6 +10,10 @@ import type { LocalizedName } from "./plugin-package.js";
|
|
|
10
10
|
export type JsonSchema = Record<string, unknown>;
|
|
11
11
|
/** The input widget of a field. */
|
|
12
12
|
export type ConfigWidget = "text" | "textarea" | "url" | "number" | "select" | "radio" | "switch" | "checkbox" | "tristate" | "tags" | "json";
|
|
13
|
+
/** How a channel connection receives events: a long connection (default) or webhooks. */
|
|
14
|
+
export type ChannelInboundMode = "long_connection" | "webhook";
|
|
15
|
+
/** Every channel inbound mode, in the order forms list them. */
|
|
16
|
+
export declare const CHANNEL_INBOUND_MODES: readonly ChannelInboundMode[];
|
|
13
17
|
/** A labelled value of an enum (or boolean) field. */
|
|
14
18
|
export interface ConfigUiOption {
|
|
15
19
|
value: string | number | boolean;
|
|
@@ -73,6 +77,13 @@ export interface ConfigUiHints {
|
|
|
73
77
|
sections?: ConfigUiSection[];
|
|
74
78
|
/** Root only: localized messages for custom issues (`params: { ui: key }`). */
|
|
75
79
|
messages?: Record<string, LocalizedName>;
|
|
80
|
+
/**
|
|
81
|
+
* Channel connection config only, top-level fields only: the inbound modes the field belongs to.
|
|
82
|
+
* Forms draw it only in those modes and require it there; the kernel refuses a connection whose
|
|
83
|
+
* mode requires it without a value (channel inbound mode fields spec §2, §3). The zod schema keeps
|
|
84
|
+
* the field optional, since the other modes do not use it.
|
|
85
|
+
*/
|
|
86
|
+
inboundModes?: ChannelInboundMode[];
|
|
76
87
|
}
|
|
77
88
|
/** Limits of config UI hints and of the JSON Schemas a plugin package carries. */
|
|
78
89
|
export declare const CONFIG_UI_LIMITS: {
|
|
@@ -129,3 +140,14 @@ export interface ConfigUiProblem {
|
|
|
129
140
|
* problem (empty = valid); never throws. Problem paths are dot paths of the node holding the hint.
|
|
130
141
|
*/
|
|
131
142
|
export declare function validateConfigUi(schema: JsonSchema, level: "deployment" | "org" | "connection"): ConfigUiProblem[];
|
|
143
|
+
/**
|
|
144
|
+
* The top-level fields of a connection config's JSON Schema ({@link configJsonSchema}) that carry
|
|
145
|
+
* an `inboundModes` hint, with their modes (unknown modes dropped). Pure; fields without the hint
|
|
146
|
+
* belong to every mode and are not listed.
|
|
147
|
+
*/
|
|
148
|
+
export declare function inboundModeFields(schema: JsonSchema): Record<string, ChannelInboundMode[]>;
|
|
149
|
+
/**
|
|
150
|
+
* The fields `mode` requires ({@link inboundModeFields}) for which `has(field)` is false, in schema
|
|
151
|
+
* order: what the kernel refuses on a channel connection create or config / mode change.
|
|
152
|
+
*/
|
|
153
|
+
export declare function missingInboundModeFields(schema: JsonSchema, mode: ChannelInboundMode, has: (field: string) => boolean): string[];
|
package/dist/config-ui.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
/** Every channel inbound mode, in the order forms list them. */
|
|
3
|
+
export const CHANNEL_INBOUND_MODES = ["long_connection", "webhook"];
|
|
2
4
|
/** Limits of config UI hints and of the JSON Schemas a plugin package carries. */
|
|
3
5
|
export const CONFIG_UI_LIMITS = {
|
|
4
6
|
/** Characters per language of a `label` (also `unit`, `item.addLabel`). */
|
|
@@ -163,9 +165,18 @@ const HINT_KEYS = new Set([
|
|
|
163
165
|
"secrets",
|
|
164
166
|
"sections",
|
|
165
167
|
"messages",
|
|
168
|
+
"inboundModes",
|
|
166
169
|
]);
|
|
167
170
|
/** The only hints a secret node may carry. */
|
|
168
|
-
const SECRET_HINT_KEYS = new Set([
|
|
171
|
+
const SECRET_HINT_KEYS = new Set([
|
|
172
|
+
"label",
|
|
173
|
+
"help",
|
|
174
|
+
"placeholder",
|
|
175
|
+
"order",
|
|
176
|
+
"section",
|
|
177
|
+
"advanced",
|
|
178
|
+
"inboundModes",
|
|
179
|
+
]);
|
|
169
180
|
const ITEM_KEYS = new Set(["title", "subtitle", "addLabel", "reorder"]);
|
|
170
181
|
const utf8Bytes = (s) => new TextEncoder().encode(s).length;
|
|
171
182
|
/** The JSON types a node accepts (`type`, a type list, or the branches of a nullable `anyOf`). */
|
|
@@ -326,7 +337,7 @@ export function validateConfigUi(schema, level) {
|
|
|
326
337
|
if (!isObj(raw))
|
|
327
338
|
report("x-ui must be an object");
|
|
328
339
|
else
|
|
329
|
-
checkHints(raw, node, isRoot, report, text);
|
|
340
|
+
checkHints(raw, node, isRoot, depth, report, text);
|
|
330
341
|
}
|
|
331
342
|
const shape = shapeOf(node);
|
|
332
343
|
const props = shape.properties;
|
|
@@ -343,7 +354,7 @@ export function validateConfigUi(schema, level) {
|
|
|
343
354
|
visit(shape.additionalProperties, path === "" ? "additionalProperties" : `${path}.additionalProperties`, false, depth + 1);
|
|
344
355
|
}
|
|
345
356
|
};
|
|
346
|
-
const checkHints = (h, node, isRoot, report, text) => {
|
|
357
|
+
const checkHints = (h, node, isRoot, depth, report, text) => {
|
|
347
358
|
const shape = shapeOf(node);
|
|
348
359
|
const types = typesOf(node);
|
|
349
360
|
const isArray = types.has("array");
|
|
@@ -356,7 +367,7 @@ export function validateConfigUi(schema, level) {
|
|
|
356
367
|
if (node["x-secret"] === true) {
|
|
357
368
|
const bad = Object.keys(h).filter((k) => HINT_KEYS.has(k) && !SECRET_HINT_KEYS.has(k));
|
|
358
369
|
if (bad.length > 0) {
|
|
359
|
-
report(`a secret field allows only label, help, placeholder, order, section and
|
|
370
|
+
report(`a secret field allows only label, help, placeholder, order, section, advanced and inboundModes hints, not ${bad.join(", ")}`);
|
|
360
371
|
}
|
|
361
372
|
}
|
|
362
373
|
text(h.label, "label", L.labelChars);
|
|
@@ -369,6 +380,28 @@ export function validateConfigUi(schema, level) {
|
|
|
369
380
|
if (h.advanced !== undefined && typeof h.advanced !== "boolean") {
|
|
370
381
|
report("advanced must be a boolean");
|
|
371
382
|
}
|
|
383
|
+
if (h.inboundModes !== undefined) {
|
|
384
|
+
const modes = h.inboundModes;
|
|
385
|
+
if (level !== "connection") {
|
|
386
|
+
report(`inboundModes is only allowed at the connection level, not ${level}`);
|
|
387
|
+
}
|
|
388
|
+
else if (depth !== 1) {
|
|
389
|
+
report("inboundModes is only allowed on a top-level field of the config");
|
|
390
|
+
}
|
|
391
|
+
else if (!Array.isArray(modes) || modes.length === 0) {
|
|
392
|
+
report("inboundModes must be a non-empty array");
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
modes.forEach((m, i) => {
|
|
396
|
+
if (!CHANNEL_INBOUND_MODES.includes(m)) {
|
|
397
|
+
report(`inboundModes[${i}] must be one of ${CHANNEL_INBOUND_MODES.join(", ")}`);
|
|
398
|
+
}
|
|
399
|
+
else if (modes.indexOf(m) !== i) {
|
|
400
|
+
report(`inboundModes[${i}] "${String(m)}" is listed twice`);
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
}
|
|
372
405
|
if (h.section !== undefined) {
|
|
373
406
|
if (typeof h.section !== "string" || !sectionIds.has(h.section)) {
|
|
374
407
|
report(`section "${String(h.section)}" is not an id of the root's sections`);
|
|
@@ -600,3 +633,30 @@ export function validateConfigUi(schema, level) {
|
|
|
600
633
|
}
|
|
601
634
|
return problems;
|
|
602
635
|
}
|
|
636
|
+
/**
|
|
637
|
+
* The top-level fields of a connection config's JSON Schema ({@link configJsonSchema}) that carry
|
|
638
|
+
* an `inboundModes` hint, with their modes (unknown modes dropped). Pure; fields without the hint
|
|
639
|
+
* belong to every mode and are not listed.
|
|
640
|
+
*/
|
|
641
|
+
export function inboundModeFields(schema) {
|
|
642
|
+
const out = {};
|
|
643
|
+
for (const [key, node] of Object.entries(properties(schema))) {
|
|
644
|
+
const hints = isObj(node) ? node["x-ui"] : undefined;
|
|
645
|
+
const modes = isObj(hints) ? hints.inboundModes : undefined;
|
|
646
|
+
if (!Array.isArray(modes))
|
|
647
|
+
continue;
|
|
648
|
+
const known = CHANNEL_INBOUND_MODES.filter((m) => modes.includes(m));
|
|
649
|
+
if (known.length > 0)
|
|
650
|
+
out[key] = known;
|
|
651
|
+
}
|
|
652
|
+
return out;
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* The fields `mode` requires ({@link inboundModeFields}) for which `has(field)` is false, in schema
|
|
656
|
+
* order: what the kernel refuses on a channel connection create or config / mode change.
|
|
657
|
+
*/
|
|
658
|
+
export function missingInboundModeFields(schema, mode, has) {
|
|
659
|
+
return Object.entries(inboundModeFields(schema))
|
|
660
|
+
.filter(([key, modes]) => modes.includes(mode) && !has(key))
|
|
661
|
+
.map(([key]) => key);
|
|
662
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/theme.d.ts
CHANGED
|
@@ -108,7 +108,7 @@ export interface ParseThemeOptions {
|
|
|
108
108
|
*/
|
|
109
109
|
readability?: boolean;
|
|
110
110
|
}
|
|
111
|
-
/** `themes/
|
|
111
|
+
/** `themes/neon-grid.json` → `neon-grid`. */
|
|
112
112
|
export declare function themeIdOf(path: string): string;
|
|
113
113
|
/**
|
|
114
114
|
* The zod schema of a theme file's shape (strict objects). Token names and values are only
|
package/dist/theme.js
CHANGED
|
@@ -42,7 +42,7 @@ export const THEME_LIMITS = {
|
|
|
42
42
|
/** Characters of the wordmark. */
|
|
43
43
|
wordmark: 24,
|
|
44
44
|
};
|
|
45
|
-
/** `themes/
|
|
45
|
+
/** `themes/neon-grid.json` → `neon-grid`. */
|
|
46
46
|
export function themeIdOf(path) {
|
|
47
47
|
return (path.split("/").pop() ?? "").replace(/\.json$/, "");
|
|
48
48
|
}
|