@capybara-hq/plugin-sdk 0.1.0-rc.9 → 0.1.0-rc.90

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.
Files changed (159) hide show
  1. package/README.md +3 -1
  2. package/agent-docs/agent-templates.md +14 -2
  3. package/agent-docs/concepts.md +14 -5
  4. package/agent-docs/connector-templates.md +11 -3
  5. package/agent-docs/extension-points/channel.md +1 -1
  6. package/agent-docs/extension-points/context.md +15 -0
  7. package/agent-docs/extension-points/device-provider.md +15 -0
  8. package/agent-docs/extension-points/file-preview.md +3 -1
  9. package/agent-docs/extension-points/identity-source.md +2 -0
  10. package/agent-docs/extension-points/model-provider.md +5 -1
  11. package/agent-docs/extension-points/object-store.md +2 -0
  12. package/agent-docs/extension-points/push-provider.md +17 -0
  13. package/agent-docs/extension-points/trigger-source.md +19 -0
  14. package/agent-docs/extensions.md +98 -4
  15. package/agent-docs/plugin-format.md +45 -10
  16. package/agent-docs/setup.md +54 -0
  17. package/api/plugin-sdk.api.md +5983 -0
  18. package/dist/agent-template.d.ts +15 -6
  19. package/dist/agent-template.js +32 -13
  20. package/dist/agents-ctx.d.ts +116 -0
  21. package/dist/agents-ctx.js +22 -0
  22. package/dist/avatar-presets.d.ts +8 -0
  23. package/dist/avatar-presets.js +17 -0
  24. package/dist/bootstrap.d.ts +109 -1
  25. package/dist/bootstrap.js +185 -2
  26. package/dist/cards.d.ts +121 -13
  27. package/dist/cards.js +161 -19
  28. package/dist/channel.d.ts +133 -47
  29. package/dist/channel.js +10 -5
  30. package/dist/client-css.d.ts +36 -0
  31. package/dist/client-css.js +326 -0
  32. package/dist/client-section.d.ts +35 -0
  33. package/dist/client-section.js +133 -0
  34. package/dist/config-ui.d.ts +28 -6
  35. package/dist/config-ui.js +88 -25
  36. package/dist/config-url-fetch.d.ts +19 -0
  37. package/dist/config-url-fetch.js +43 -0
  38. package/dist/connector-auth.d.ts +13 -2
  39. package/dist/connector-auth.js +82 -8
  40. package/dist/connector.d.ts +74 -32
  41. package/dist/context.d.ts +40 -0
  42. package/dist/context.js +2 -0
  43. package/dist/ctx.d.ts +344 -31
  44. package/dist/ctx.js +9 -1
  45. package/dist/define-extension.d.ts +18 -12
  46. package/dist/define-extension.js +18 -5
  47. package/dist/device-denied-paths.d.ts +20 -0
  48. package/dist/device-denied-paths.js +118 -0
  49. package/dist/device-operation.d.ts +348 -0
  50. package/dist/device-operation.js +312 -0
  51. package/dist/device-protocol.d.ts +286 -49
  52. package/dist/device-protocol.js +766 -89
  53. package/dist/device-provider.d.ts +820 -0
  54. package/dist/device-provider.js +432 -0
  55. package/dist/device-service.d.ts +204 -0
  56. package/dist/device-service.js +291 -0
  57. package/dist/dial-in.d.ts +286 -0
  58. package/dist/dial-in.js +213 -0
  59. package/dist/directory.d.ts +15 -12
  60. package/dist/extension-point.d.ts +2 -0
  61. package/dist/extension-point.js +8 -3
  62. package/dist/field-meta.d.ts +19 -0
  63. package/dist/field-meta.js +54 -0
  64. package/dist/file-preview.d.ts +46 -9
  65. package/dist/git-hosts.d.ts +53 -0
  66. package/dist/git-hosts.js +98 -0
  67. package/dist/hooks.d.ts +185 -22
  68. package/dist/hooks.js +6 -0
  69. package/dist/http-access.d.ts +71 -0
  70. package/dist/http-access.js +12 -0
  71. package/dist/identity.d.ts +70 -9
  72. package/dist/identity.js +46 -1
  73. package/dist/inbox.d.ts +103 -0
  74. package/dist/inbox.js +173 -0
  75. package/dist/index.d.ts +30 -4
  76. package/dist/index.js +30 -4
  77. package/dist/model-params.d.ts +41 -22
  78. package/dist/model-params.js +28 -15
  79. package/dist/model.d.ts +295 -77
  80. package/dist/model.js +249 -21
  81. package/dist/native-failure.d.ts +13 -0
  82. package/dist/native-failure.js +31 -0
  83. package/dist/net.d.ts +52 -0
  84. package/dist/net.js +34 -0
  85. package/dist/network-classes.d.ts +98 -0
  86. package/dist/network-classes.js +291 -0
  87. package/dist/permissions.d.ts +95 -25
  88. package/dist/permissions.js +175 -54
  89. package/dist/platform-only.d.ts +27 -0
  90. package/dist/platform-only.js +35 -0
  91. package/dist/plugin-manifest.d.ts +30 -6
  92. package/dist/plugin-manifest.js +147 -55
  93. package/dist/plugin-package.d.ts +82 -39
  94. package/dist/plugin-package.js +68 -41
  95. package/dist/push.d.ts +75 -0
  96. package/dist/push.js +87 -0
  97. package/dist/sandbox-linux-runtime-source.d.ts +1 -0
  98. package/dist/sandbox-linux-runtime-source.js +2343 -0
  99. package/dist/sandbox-native-runtime-source.d.ts +10 -0
  100. package/dist/sandbox-native-runtime-source.js +480 -0
  101. package/dist/sandbox-runtime-source.d.ts +47 -0
  102. package/dist/sandbox-runtime-source.js +73 -0
  103. package/dist/sandbox-write-denies.d.ts +139 -0
  104. package/dist/sandbox-write-denies.js +869 -0
  105. package/dist/secret.d.ts +8 -2
  106. package/dist/secret.js +11 -4
  107. package/dist/setup-check.d.ts +128 -0
  108. package/dist/setup-check.js +142 -0
  109. package/dist/setup-guide-parse.d.ts +121 -0
  110. package/dist/setup-guide-parse.js +356 -0
  111. package/dist/setup-guide-render.d.ts +45 -0
  112. package/dist/setup-guide-render.js +351 -0
  113. package/dist/setup-guide.d.ts +86 -72
  114. package/dist/setup-guide.js +340 -108
  115. package/dist/shell-capabilities.d.ts +216 -0
  116. package/dist/shell-capabilities.js +44 -0
  117. package/dist/shell-environment.d.ts +57 -0
  118. package/dist/shell-environment.js +21 -0
  119. package/dist/skill-invocation.d.ts +5 -0
  120. package/dist/skill-invocation.js +4 -3
  121. package/dist/skill.d.ts +27 -17
  122. package/dist/skill.js +40 -26
  123. package/dist/storage.d.ts +52 -25
  124. package/dist/svg-viewbox.d.ts +10 -0
  125. package/dist/svg-viewbox.js +24 -0
  126. package/dist/theme-assets.d.ts +25 -9
  127. package/dist/theme-assets.js +87 -47
  128. package/dist/theme-tokens.generated.d.ts +40 -12
  129. package/dist/theme-tokens.generated.js +36 -8
  130. package/dist/theme.d.ts +56 -10
  131. package/dist/theme.js +117 -23
  132. package/dist/tool.d.ts +242 -28
  133. package/dist/tool.js +135 -13
  134. package/dist/trigger-source.d.ts +35 -0
  135. package/dist/trigger-source.js +2 -0
  136. package/dist/triggers.d.ts +88 -0
  137. package/dist/triggers.js +71 -0
  138. package/dist/ui-parts.d.ts +23 -0
  139. package/dist/ui-parts.js +374 -0
  140. package/dist/ui-resolve.d.ts +2 -7
  141. package/dist/ui-resolve.js +7 -19
  142. package/dist/ui-slots.d.ts +27 -5
  143. package/dist/ui-slots.js +112 -19
  144. package/dist/ui.d.ts +203 -64
  145. package/dist/ui.js +117 -23
  146. package/dist/url.d.ts +4 -1
  147. package/dist/url.js +4 -1
  148. package/dist/web.d.ts +26 -20
  149. package/package.json +13 -1
  150. package/schemas/bootstrap-extra.v1.schema.json +186 -0
  151. package/schemas/bootstrap.v1.schema.json +20 -0
  152. package/schemas/connector-template.v1.schema.json +62 -4
  153. package/schemas/plugin.v1.schema.json +74 -143
  154. package/schemas/theme.v1.schema.json +77 -0
  155. package/agent-docs/extension-points/sandbox.md +0 -15
  156. package/dist/org-access.d.ts +0 -27
  157. package/dist/org-access.js +0 -35
  158. package/dist/sandbox.d.ts +0 -254
  159. package/dist/sandbox.js +0 -153
package/dist/cards.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { type FormAnswer, type InputForm } from "./tool.js";
2
2
  /**
3
- * 渠道中立的 IM 卡片:由内核定义与产出,渠道插件按各自 IM 的原生格式渲染;
4
- * 不支持卡片的渠道用 {@link renderCardAsText} 退回为文本。
3
+ * Channel-neutral IM cards: defined and produced by the kernel, rendered by channel plugins in each
4
+ * IM's native format;
5
+ * channels without card support fall back to text with {@link renderCardAsText}.
5
6
  */
6
7
  export type ChannelCard = {
7
8
  kind: "approval";
@@ -19,20 +20,40 @@ export type ChannelCard = {
19
20
  */
20
21
  inputPreview: string;
21
22
  scopes: ("once" | "session" | "always")[];
23
+ /**
24
+ * The kernel's names for the scopes when they mean something other than the default (a
25
+ * network ask: `once` = "Allow this time", `always` = "Always allow on this device"). Plain
26
+ * text, 1 to 40 characters; a channel that does not know the field renders its default
27
+ * scope labels. Kernel-written, never model-controlled.
28
+ */
29
+ scopeLabels?: Partial<Record<"once" | "session" | "always", string>>;
22
30
  expiresAt: string;
23
- state: "pending" | "approved" | "denied" | "expired" | "cancelled";
31
+ /** `skipped`: the person sent a message instead of deciding (the run went on with it). */
32
+ state: "pending" | "approved" | "denied" | "expired" | "cancelled" | "skipped";
24
33
  decidedBy?: string;
25
34
  /** A denied approval: the reason the person gave (plain text, never formatted). */
26
35
  denyReason?: string;
27
- /** 可决定该审批的会话网页链接。 */
36
+ /** Web link to the conversation that can decide this approval. */
28
37
  webUrl: string;
29
38
  } | {
30
39
  kind: "question";
31
40
  id: string;
32
41
  form: InputForm;
33
- state: "pending" | "answered" | "expired" | "cancelled";
42
+ /**
43
+ * `skipped`: the person sent a message instead of answering (the run went on with it).
44
+ * `woken`: a watched wait the tool's watcher ended (the condition it waited for held, or the
45
+ * wait could no longer be watched); nobody answered.
46
+ */
47
+ state: "pending" | "answered" | "skipped" | "expired" | "cancelled" | "woken";
34
48
  answerSummary?: string;
35
49
  webUrl: string;
50
+ /**
51
+ * The person may answer by replying with text instead (pending cards only): `plain` in a DM
52
+ * (any reply reaches the agent), `mention` in a group thread (the reply must mention the bot;
53
+ * the kernel ignores group messages without it). Absent: only the card's own controls work
54
+ * (a sub-agent's question). Render the hint with {@link questionTextReplyHint}.
55
+ */
56
+ textReply?: "plain" | "mention";
36
57
  } | {
37
58
  kind: "connect";
38
59
  appName: string;
@@ -41,8 +62,48 @@ export type ChannelCard = {
41
62
  kind: "bind";
42
63
  url: string;
43
64
  text: string;
65
+ } | {
66
+ /**
67
+ * One edited progress card per run (kernel-produced; the channel edits the same message in
68
+ * place). Sent natively only to a channel that declares `features.progress`; any other
69
+ * channel gets {@link renderCardAsText} of it as a text message, edited in place. The title,
70
+ * meta, pending texts and link text are written and localized by the kernel and rendered as
71
+ * is; `status` (model narration) and `pinned` (extension Markdown) are rendered as Markdown,
72
+ * like an answer; `steps` are built from tool labels that include model-written arguments,
73
+ * so {@link renderCardAsText} puts each one in a code span.
74
+ */
75
+ kind: "progress";
76
+ state: "running" | "waiting" | "finished" | "stopped" | "failed";
77
+ /** The headline, e.g. "Working", "Waiting for your approval", "Worked for 2m 43s". */
78
+ title: string;
79
+ /** A short detail after the title, e.g. "Elapsed 0:42" or "Todo 4/4 · 12 tools". */
80
+ meta?: string;
81
+ /**
82
+ * The latest model narration, its first line (cut short without breaking Markdown syntax):
83
+ * model-written Markdown, rendered like an answer.
84
+ */
85
+ status?: string;
86
+ /** The pinned card's Markdown text (a plan or todo list), written by an extension. */
87
+ pinned?: string;
88
+ /** The latest steps, at most 3, oldest first. May hold model-written arguments: plain text. */
89
+ steps: string[];
90
+ /**
91
+ * Approvals or questions waiting on the person, each with its web link. `text` is the
92
+ * kernel-written, localized handoff title (not model text), so it is rendered as is.
93
+ */
94
+ pending: {
95
+ text: string;
96
+ url: string;
97
+ }[];
98
+ /** The run on the web. */
99
+ url: string;
100
+ /** The link text for `url`, e.g. "See the full process on the web". */
101
+ urlText: string;
44
102
  };
45
- /** 用户在 IM 卡片上的操作,由渠道插件解析后经入站事件交给内核。 */
103
+ /**
104
+ * A user action on an IM card, parsed by the channel plugin and handed to the kernel through an
105
+ * inbound event.
106
+ */
46
107
  export interface CardAction {
47
108
  cardKind: "approval" | "question";
48
109
  cardId: string;
@@ -50,22 +111,69 @@ export interface CardAction {
50
111
  scope?: "once" | "session" | "always";
51
112
  answer?: FormAnswer;
52
113
  }
53
- /** 发往 IM 的一条消息:text 为 Markdown(渠道插件转换为原生富文本),可附卡片。 */
114
+ /**
115
+ * One message sent to an IM: `text` is Markdown (the channel plugin converts it to native rich
116
+ * text); a card may be attached.
117
+ */
54
118
  export interface OutboundMessage {
55
119
  text: string;
56
120
  cards?: ChannelCard[];
57
121
  }
58
122
  /**
59
- * 提问能否在 IM 内直接作答:仅当表单恰有一个枚举字段(单选 ≤ 5 项 / 多选 ≤ 10 项)且不允许「其他」时为真;
60
- * 否则卡片只给网页链接。
123
+ * Whether IM can draw the question's controls: exactly one enum field (single choice ≤ 5 options /
124
+ * multi-select ≤ 10). A field with `allowOther` qualifies only when the card has `textReply`
125
+ * (pass `{ textReply: card.textReply !== undefined }`): the Other path is then a plain reply, so
126
+ * never draw an Other button. Otherwise the card only links to the web.
61
127
  */
62
- export declare function questionRenderableInline(form: InputForm): boolean;
63
- /** 校验卡片结构,返回问题列表(空 = 合法)。 */
128
+ export declare function questionRenderableInline(form: InputForm, opts?: {
129
+ textReply?: boolean;
130
+ }): boolean;
131
+ /** One button of a question's `choices`, as {@link questionChoiceButtons} resolves it. */
132
+ export interface QuestionChoiceButton {
133
+ /** The answer the button submits for the form's single field. */
134
+ value: string | boolean;
135
+ /** The label in the locale. */
136
+ label: string;
137
+ /** True for the first choice only. */
138
+ primary: boolean;
139
+ }
140
+ /**
141
+ * The buttons of a form with `choices` (core#867): one per choice in order, the first primary; a
142
+ * click answers the form's single field `key` with the button's `value` at once. Undefined when
143
+ * the form has no choices (use {@link questionRenderableInline} for the enum controls).
144
+ */
145
+ export declare function questionChoiceButtons(form: InputForm, locale: Locale): {
146
+ key: string;
147
+ buttons: QuestionChoiceButton[];
148
+ } | undefined;
149
+ /** The form's body text in the locale (the explanation under its title); undefined without one. */
150
+ export declare function questionBody(form: InputForm, locale: Locale): string | undefined;
151
+ /** Validates the card structure and returns the list of problems (empty = valid). */
64
152
  export declare function validateChannelCard(card: unknown): string[];
65
153
  /** A UI locale. */
66
154
  export type Locale = "zh" | "en";
67
155
  /**
68
- * 不支持卡片的渠道的统一退回文本(含链接);固定话术按 locale 取 zh / en。结果是 Markdown(放进
69
- * `OutboundMessage.text`,由渠道插件转换):模型可控的字段放在代码里(见 `asCode`),不会产生链接。
156
+ * Puts model-controllable values (approval title, call summary, question title, answer summary)
157
+ * into code, so that in the fallback text they do not turn into links, emphasis or HTML:
158
+ * single-line values use inline code, multi-line values a fenced code block (starting with a
159
+ * newline); the delimiters are always longer than the longest backtick run in the value (CommonMark
160
+ * 6.1 / 4.5).
161
+ * Code is chosen over backslash-escaping punctuation because code is the only form CommonMark / GFM
162
+ * guarantee to display content verbatim (a backslash does not stop GFM bare-URL
163
+ * autolinks, and channel converters treat backslashes inconsistently).
164
+ */
165
+ export declare function asCode(s: string): string;
166
+ /** The hint line of a pending question card with `textReply`, as {@link renderCardAsText} writes it. */
167
+ export declare function questionTextReplyHint(mode: "plain" | "mention", locale: Locale): string;
168
+ /**
169
+ * The label of a question card's state, as {@link renderCardAsText} writes it. A state this SDK
170
+ * does not know (a newer kernel) returns the state itself, never `undefined`.
171
+ */
172
+ export declare function questionStateLabel(state: string, locale: Locale): string;
173
+ /**
174
+ * The uniform fallback text for channels without card support (links included); the fixed phrases
175
+ * are taken by locale, zh / en. The result is Markdown (put into
176
+ * `OutboundMessage.text`, converted by the channel plugin): model-controllable fields go into code
177
+ * (see `asCode`), so they never produce links.
70
178
  */
71
179
  export declare function renderCardAsText(card: ChannelCard, locale: Locale): string;
package/dist/cards.js CHANGED
@@ -3,27 +3,71 @@ import { isHttpUrl } from "./url.js";
3
3
  const MAX_INLINE_SINGLE = 5;
4
4
  const MAX_INLINE_MULTI = 10;
5
5
  /**
6
- * 提问能否在 IM 内直接作答:仅当表单恰有一个枚举字段(单选 ≤ 5 项 / 多选 ≤ 10 项)且不允许「其他」时为真;
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?.choices !== undefined && f.choices.length > 0)
16
+ return true;
17
+ if (!f || (f.allowOther && opts?.textReply !== true))
14
18
  return false;
15
19
  if (f.schema.type !== "string" || !("enum" in f.schema))
16
20
  return false;
17
21
  const n = f.schema.enum.length;
18
22
  return f.multiSelect ? n <= MAX_INLINE_MULTI : n <= MAX_INLINE_SINGLE;
19
23
  }
20
- const KINDS = ["approval", "question", "connect", "bind"];
24
+ const pickText = (t, locale) => typeof t === "string" ? t : (t[locale] ?? t.en);
25
+ /**
26
+ * The buttons of a form with `choices` (core#867): one per choice in order, the first primary; a
27
+ * click answers the form's single field `key` with the button's `value` at once. Undefined when
28
+ * the form has no choices (use {@link questionRenderableInline} for the enum controls).
29
+ */
30
+ export function questionChoiceButtons(form, locale) {
31
+ const f = form.fields[0];
32
+ if (form.fields.length !== 1 || !f?.choices || f.choices.length === 0)
33
+ return undefined;
34
+ return {
35
+ key: f.key,
36
+ buttons: f.choices.map((c, i) => ({
37
+ value: c.value,
38
+ label: pickText(c.label, locale),
39
+ primary: i === 0,
40
+ })),
41
+ };
42
+ }
43
+ /** The form's body text in the locale (the explanation under its title); undefined without one. */
44
+ export function questionBody(form, locale) {
45
+ return form.body === undefined ? undefined : pickText(form.body, locale);
46
+ }
47
+ const KINDS = ["approval", "question", "connect", "bind", "progress"];
48
+ const PROGRESS_STATES = ["running", "waiting", "finished", "stopped", "failed"];
21
49
  const SCOPES = ["once", "session", "always"];
22
- const APPROVAL_STATES = ["pending", "approved", "denied", "expired", "cancelled"];
23
- const QUESTION_STATES = ["pending", "answered", "expired", "cancelled"];
50
+ const APPROVAL_STATES = [
51
+ "pending",
52
+ "approved",
53
+ "denied",
54
+ "expired",
55
+ "cancelled",
56
+ "skipped",
57
+ ];
58
+ const MAX_SCOPE_LABEL = 40;
59
+ const QUESTION_STATES = [
60
+ "pending",
61
+ "answered",
62
+ "skipped",
63
+ "expired",
64
+ "cancelled",
65
+ "woken",
66
+ ];
67
+ const TEXT_REPLY = ["plain", "mention"];
24
68
  const ISO_8601 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})$/;
25
69
  const list = (xs) => xs.map((x) => JSON.stringify(x)).join(", ");
26
- /** 校验卡片结构,返回问题列表(空 = 合法)。 */
70
+ /** Validates the card structure and returns the list of problems (empty = valid). */
27
71
  export function validateChannelCard(card) {
28
72
  if (typeof card !== "object" || card === null || Array.isArray(card))
29
73
  return ["card must be an object"];
@@ -58,6 +102,16 @@ export function validateChannelCard(card) {
58
102
  new Set(s).size !== s.length ||
59
103
  s.some((x) => !SCOPES.includes(x)))
60
104
  out.push(`scopes must be a non-empty array of ${list(SCOPES)} without duplicates`);
105
+ const labels = c.scopeLabels;
106
+ if (labels !== undefined &&
107
+ (typeof labels !== "object" ||
108
+ labels === null ||
109
+ Array.isArray(labels) ||
110
+ Object.entries(labels).some(([k, v]) => !SCOPES.includes(k) ||
111
+ typeof v !== "string" ||
112
+ v.length === 0 ||
113
+ v.length > MAX_SCOPE_LABEL)))
114
+ out.push(`scopeLabels must map ${list(SCOPES)} to strings of 1 to ${MAX_SCOPE_LABEL} characters`);
61
115
  if (typeof c.expiresAt !== "string" ||
62
116
  !ISO_8601.test(c.expiresAt) ||
63
117
  Number.isNaN(Date.parse(c.expiresAt)))
@@ -74,7 +128,10 @@ export function validateChannelCard(card) {
74
128
  url("webUrl");
75
129
  if (c.answerSummary !== undefined && typeof c.answerSummary !== "string")
76
130
  out.push("answerSummary must be a string");
77
- // 不能在 IM 内作答的表单(见 questionRenderableInline)同样合法:渠道只渲染网页链接。
131
+ if (c.textReply !== undefined)
132
+ oneOf("textReply", TEXT_REPLY);
133
+ // A form that cannot be answered inside the IM (see questionRenderableInline) is equally
134
+ // valid: the channel renders only the web link.
78
135
  for (const p of validateInputForm(c.form))
79
136
  out.push(`form: ${p}`);
80
137
  return out;
@@ -87,17 +144,43 @@ export function validateChannelCard(card) {
87
144
  url("url");
88
145
  nonEmpty("text");
89
146
  return out;
147
+ case "progress": {
148
+ nonEmpty("title");
149
+ nonEmpty("urlText");
150
+ optNonEmpty("meta");
151
+ optNonEmpty("status");
152
+ optNonEmpty("pinned");
153
+ oneOf("state", PROGRESS_STATES);
154
+ url("url");
155
+ if (!Array.isArray(c.steps) ||
156
+ c.steps.length > 3 ||
157
+ c.steps.some((x) => typeof x !== "string"))
158
+ out.push("steps must be an array of at most 3 strings");
159
+ const p = c.pending;
160
+ if (!Array.isArray(p) ||
161
+ p.some((x) => typeof x !== "object" ||
162
+ x === null ||
163
+ typeof x.text !== "string" ||
164
+ x.text === "" ||
165
+ !isHttpUrl(x.url)))
166
+ out.push("pending must be an array of { text, url } with an http(s) url");
167
+ return out;
168
+ }
90
169
  default:
91
170
  return [`kind must be one of ${list(KINDS)}`];
92
171
  }
93
172
  }
94
173
  /**
95
- * 把模型可控的值(审批标题、调用摘要、提问标题、回答摘要)放进代码里,退回文本里它们不会变成链接、强调或 HTML:
96
- * 单行用行内代码,多行用围栏代码块(以换行开头),分隔符都比值里最长的反引号串更长(CommonMark 6.1 / 4.5)。
97
- * 选代码而不是给标点加反斜杠:代码是 CommonMark / GFM 里唯一保证内容按原样显示的写法(反斜杠挡不住 GFM 的裸网址
98
- * 自动链接,各渠道转换器对反斜杠的处理也不一致)。
174
+ * Puts model-controllable values (approval title, call summary, question title, answer summary)
175
+ * into code, so that in the fallback text they do not turn into links, emphasis or HTML:
176
+ * single-line values use inline code, multi-line values a fenced code block (starting with a
177
+ * newline); the delimiters are always longer than the longest backtick run in the value (CommonMark
178
+ * 6.1 / 4.5).
179
+ * Code is chosen over backslash-escaping punctuation because code is the only form CommonMark / GFM
180
+ * guarantee to display content verbatim (a backslash does not stop GFM bare-URL
181
+ * autolinks, and channel converters treat backslashes inconsistently).
99
182
  */
100
- function asCode(s) {
183
+ export function asCode(s) {
101
184
  const v = s.replace(/\r\n?/g, "\n");
102
185
  const longest = Math.max(0, ...[...v.matchAll(/`+/g)].map((m) => m[0].length));
103
186
  if (v.includes("\n")) {
@@ -105,11 +188,15 @@ function asCode(s) {
105
188
  return `\n${fence}\n${v}\n${fence}`;
106
189
  }
107
190
  const d = "`".repeat(longest + 1);
108
- // 值以反引号开头 / 结尾、或首尾都是空格时补一个空格(渲染时去掉),否则会与分隔符粘连或被吃掉空格
191
+ // When the value starts / ends with a backtick, or has spaces at both ends, add one space
192
+ // (stripped when rendered); otherwise it would stick to the delimiter or lose its spaces
109
193
  const pad = /^`|`$/.test(v) || (v.startsWith(" ") && v.endsWith(" ")) ? " " : "";
110
194
  return `${d}${pad}${v}${pad}${d}`;
111
195
  }
112
- /** 前缀 + 代码:代码是围栏块(以换行开头)时去掉前缀末尾的空格。 */
196
+ /**
197
+ * Prefix + code: when the code is a fenced block (starting with a newline), drop the trailing space
198
+ * of the prefix.
199
+ */
113
200
  const withCode = (prefix, code) => code.startsWith("\n") ? `${prefix.trimEnd()}${code}` : `${prefix}${code}`;
114
201
  const TEXT = {
115
202
  en: {
@@ -128,18 +215,27 @@ const TEXT = {
128
215
  answerOnWeb: "Answer on the web",
129
216
  details: "Details",
130
217
  connectLink: "Connect",
218
+ options: "Options",
219
+ scopes: { once: "Once", session: "This session", always: "Always" },
131
220
  approvalStates: {
132
221
  pending: "Pending",
133
222
  approved: "Approved",
134
223
  denied: "Denied",
135
224
  expired: "Expired",
136
225
  cancelled: "Cancelled",
226
+ skipped: "Skipped",
137
227
  },
138
228
  questionStates: {
139
229
  pending: "Pending",
140
230
  answered: "Answered",
231
+ skipped: "Skipped (replied instead)",
141
232
  expired: "Expired",
142
233
  cancelled: "Cancelled",
234
+ woken: "Wait ended",
235
+ },
236
+ textReply: {
237
+ plain: "You can also just reply with text",
238
+ mention: "You can also reply in this thread, mentioning me",
143
239
  },
144
240
  },
145
241
  zh: {
@@ -158,24 +254,47 @@ const TEXT = {
158
254
  answerOnWeb: "到网页上回答",
159
255
  details: "详情",
160
256
  connectLink: "连接",
257
+ options: "选项",
258
+ scopes: { once: "这次", session: "本会话", always: "一直" },
161
259
  approvalStates: {
162
260
  pending: "待审批",
163
261
  approved: "已批准",
164
262
  denied: "已拒绝",
165
263
  expired: "已过期",
166
264
  cancelled: "已取消",
265
+ skipped: "已跳过",
167
266
  },
168
267
  questionStates: {
169
268
  pending: "待回答",
170
269
  answered: "已回答",
270
+ skipped: "已跳过(已直接回复)",
171
271
  expired: "已过期",
172
272
  cancelled: "已取消",
273
+ woken: "等待已结束",
274
+ },
275
+ textReply: {
276
+ plain: "也可以直接回复文字",
277
+ mention: "也可以在本话题 @我 回复文字",
173
278
  },
174
279
  },
175
280
  };
281
+ /** The hint line of a pending question card with `textReply`, as {@link renderCardAsText} writes it. */
282
+ export function questionTextReplyHint(mode, locale) {
283
+ return (TEXT[locale] ?? TEXT.en).textReply[mode];
284
+ }
285
+ /**
286
+ * The label of a question card's state, as {@link renderCardAsText} writes it. A state this SDK
287
+ * does not know (a newer kernel) returns the state itself, never `undefined`.
288
+ */
289
+ export function questionStateLabel(state, locale) {
290
+ const states = (TEXT[locale] ?? TEXT.en).questionStates;
291
+ return states[state] ?? state;
292
+ }
176
293
  /**
177
- * 不支持卡片的渠道的统一退回文本(含链接);固定话术按 locale 取 zh / en。结果是 Markdown(放进
178
- * `OutboundMessage.text`,由渠道插件转换):模型可控的字段放在代码里(见 `asCode`),不会产生链接。
294
+ * The uniform fallback text for channels without card support (links included); the fixed phrases
295
+ * are taken by locale, zh / en. The result is Markdown (put into
296
+ * `OutboundMessage.text`, converted by the channel plugin): model-controllable fields go into code
297
+ * (see `asCode`), so they never produce links.
179
298
  */
180
299
  export function renderCardAsText(card, locale) {
181
300
  const t = TEXT[locale] ?? TEXT.en;
@@ -192,6 +311,12 @@ export function renderCardAsText(card, locale) {
192
311
  ];
193
312
  if (card.state === "denied" && card.denyReason)
194
313
  lines.push(line(t.reason, asCode(card.denyReason)));
314
+ // the scopes are listed only when the kernel named them (a network ask): the default
315
+ // approval text stays as it was
316
+ if (card.state === "pending" && card.scopeLabels) {
317
+ const labels = card.scopes.map((s) => card.scopeLabels?.[s] ?? t.scopes[s]);
318
+ lines.push(line(t.options, labels.join(" / ")));
319
+ }
195
320
  if (card.state === "pending")
196
321
  lines.push(line(t.expires, card.expiresAt), line(t.approveOnWeb, card.webUrl));
197
322
  else
@@ -202,16 +327,33 @@ export function renderCardAsText(card, locale) {
202
327
  const title = typeof card.form.title === "string" ? card.form.title : card.form.title[locale];
203
328
  const lines = [
204
329
  withCode(t.question, asCode(title)),
205
- line(t.status, t.questionStates[card.state]),
330
+ line(t.status, questionStateLabel(card.state, locale)),
206
331
  ];
332
+ const body = questionBody(card.form, locale);
333
+ if (body !== undefined)
334
+ lines.push(withCode("", asCode(body)));
335
+ const choices = card.state === "pending" ? questionChoiceButtons(card.form, locale) : undefined;
336
+ if (choices)
337
+ lines.push(line(t.options, choices.buttons.map((b) => asCode(b.label)).join(" / ")));
207
338
  if (card.answerSummary)
208
339
  lines.push(line(t.answer, asCode(card.answerSummary)));
209
340
  lines.push(line(card.state === "pending" ? t.answerOnWeb : t.details, card.webUrl));
341
+ if (card.state === "pending" && card.textReply !== undefined)
342
+ lines.push(questionTextReplyHint(card.textReply, locale));
210
343
  return lines.join("\n");
211
344
  }
212
345
  case "connect":
213
346
  return `${t.connect(card.appName)}\n${line(t.connectLink, card.url)}`;
214
347
  case "bind":
215
348
  return `${card.text}\n${card.url}`;
349
+ case "progress":
350
+ return [
351
+ card.meta ? `${card.title} · ${card.meta}` : card.title,
352
+ ...(card.status ? [card.status] : []),
353
+ ...(card.pinned ? [card.pinned] : []),
354
+ ...card.steps.map((s) => withCode("- ", asCode(s))),
355
+ ...card.pending.map((p) => `[${p.text}](${p.url})`),
356
+ `[${card.urlText}](${card.url})`,
357
+ ].join("\n");
216
358
  }
217
359
  }