@capybara-hq/plugin-sdk 0.1.0-rc.5 → 0.1.0-rc.50
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/agent-docs/agent-templates.md +14 -2
- package/agent-docs/concepts.md +14 -5
- package/agent-docs/connector-templates.md +4 -1
- package/agent-docs/extension-points/channel.md +1 -1
- package/agent-docs/extension-points/context.md +15 -0
- package/agent-docs/extension-points/file-preview.md +2 -0
- package/agent-docs/extension-points/model-provider.md +3 -1
- package/agent-docs/extension-points/object-store.md +2 -0
- package/agent-docs/extension-points/push-provider.md +17 -0
- package/agent-docs/extension-points/sandbox.md +2 -2
- package/agent-docs/extensions.md +29 -3
- package/agent-docs/plugin-format.md +48 -2
- package/agent-docs/setup.md +54 -0
- package/dist/agent-template.d.ts +15 -6
- package/dist/agent-template.js +32 -13
- package/dist/avatar-presets.d.ts +8 -0
- package/dist/avatar-presets.js +17 -0
- package/dist/bootstrap.d.ts +86 -1
- package/dist/bootstrap.js +161 -2
- package/dist/cards.d.ts +67 -13
- package/dist/cards.js +91 -21
- package/dist/channel.d.ts +148 -48
- package/dist/channel.js +10 -5
- package/dist/client-css.d.ts +36 -0
- package/dist/client-css.js +326 -0
- package/dist/client-section.d.ts +35 -0
- package/dist/client-section.js +133 -0
- package/dist/config-ui.d.ts +22 -0
- package/dist/config-ui.js +64 -4
- package/dist/connector-auth.d.ts +7 -2
- package/dist/connector-auth.js +29 -8
- package/dist/connector.d.ts +74 -32
- package/dist/context.d.ts +40 -0
- package/dist/context.js +2 -0
- package/dist/ctx.d.ts +120 -29
- package/dist/define-extension.d.ts +26 -12
- package/dist/define-extension.js +11 -4
- package/dist/device-denied-paths.d.ts +14 -0
- package/dist/device-denied-paths.js +41 -0
- package/dist/device-protocol.d.ts +489 -0
- package/dist/device-protocol.js +1116 -0
- package/dist/extension-point.d.ts +2 -0
- package/dist/extension-point.js +6 -2
- package/dist/file-preview.d.ts +23 -6
- package/dist/git-hosts.d.ts +53 -0
- package/dist/git-hosts.js +98 -0
- package/dist/hooks.d.ts +125 -20
- package/dist/hooks.js +5 -0
- package/dist/http-access.d.ts +62 -0
- package/dist/http-access.js +12 -0
- package/dist/identity.d.ts +38 -9
- package/dist/identity.js +21 -1
- package/dist/index.d.ts +28 -1
- package/dist/index.js +28 -1
- package/dist/marketplace.d.ts +26 -0
- package/dist/marketplace.js +43 -1
- package/dist/model-params.d.ts +41 -22
- package/dist/model-params.js +28 -15
- package/dist/model.d.ts +206 -71
- package/dist/model.js +152 -19
- package/dist/net.d.ts +52 -0
- package/dist/net.js +34 -0
- package/dist/network-classes.d.ts +98 -0
- package/dist/network-classes.js +291 -0
- package/dist/permissions.d.ts +41 -21
- package/dist/permissions.js +101 -42
- package/dist/plugin-manifest.d.ts +45 -5
- package/dist/plugin-manifest.js +169 -6
- package/dist/plugin-package.d.ts +82 -38
- package/dist/plugin-package.js +61 -29
- package/dist/push.d.ts +75 -0
- package/dist/push.js +87 -0
- package/dist/sandbox-linux-runtime-source.d.ts +1 -0
- package/dist/sandbox-linux-runtime-source.js +2318 -0
- package/dist/sandbox-native-runtime-source.d.ts +10 -0
- package/dist/sandbox-native-runtime-source.js +476 -0
- package/dist/sandbox-operation.d.ts +298 -0
- package/dist/sandbox-operation.js +282 -0
- package/dist/sandbox-runtime-source.d.ts +19 -0
- package/dist/sandbox-runtime-source.js +28 -0
- package/dist/sandbox-write-denies.d.ts +139 -0
- package/dist/sandbox-write-denies.js +869 -0
- package/dist/sandbox.d.ts +190 -16
- package/dist/sandbox.js +89 -14
- package/dist/secret.d.ts +4 -1
- package/dist/secret.js +4 -1
- package/dist/setup-check.d.ts +128 -0
- package/dist/setup-check.js +142 -0
- package/dist/setup-guide-parse.d.ts +121 -0
- package/dist/setup-guide-parse.js +350 -0
- package/dist/setup-guide-render.d.ts +45 -0
- package/dist/setup-guide-render.js +351 -0
- package/dist/setup-guide.d.ts +96 -0
- package/dist/setup-guide.js +348 -0
- package/dist/shell-capabilities.d.ts +231 -0
- package/dist/shell-capabilities.js +37 -0
- package/dist/shell-environment.d.ts +57 -0
- package/dist/shell-environment.js +22 -0
- package/dist/skill-invocation.d.ts +4 -3
- package/dist/skill-invocation.js +5 -4
- package/dist/skill.d.ts +34 -21
- package/dist/skill.js +48 -31
- package/dist/storage.d.ts +52 -25
- package/dist/theme-assets.d.ts +55 -0
- package/dist/theme-assets.js +507 -0
- package/dist/theme-readability.d.ts +37 -0
- package/dist/theme-readability.js +145 -0
- package/dist/theme-tokens.generated.d.ts +299 -0
- package/dist/theme-tokens.generated.js +184 -0
- package/dist/theme.d.ts +190 -0
- package/dist/theme.js +471 -0
- package/dist/tool.d.ts +69 -26
- package/dist/tool.js +29 -9
- package/dist/ui-parts.d.ts +23 -0
- package/dist/ui-parts.js +348 -0
- package/dist/ui-resolve.d.ts +35 -0
- package/dist/ui-resolve.js +42 -0
- package/dist/ui-slots.d.ts +65 -0
- package/dist/ui-slots.js +261 -0
- package/dist/ui.d.ts +212 -76
- package/dist/ui.js +101 -27
- package/dist/url.d.ts +4 -1
- package/dist/url.js +4 -1
- package/package.json +8 -1
- package/schemas/bootstrap-extra.v1.schema.json +172 -0
- package/schemas/bootstrap.v1.schema.json +6 -0
- package/schemas/connector-template.v1.schema.json +35 -0
- package/schemas/marketplace.v1.schema.json +14 -0
- package/schemas/plugin.v1.schema.json +116 -3
- package/schemas/theme.v1.schema.json +302 -0
package/dist/bootstrap.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import {
|
|
2
|
+
import { GIT_USERNAME_RE } from "./git-hosts.js";
|
|
3
|
+
import { CATALOG_ITEM_NAME_RE, isSafePackagePath, NPM_PACKAGE_NAME_RE, SEMVER_VERSION_RE, SHA512_INTEGRITY_RE, } from "./plugin-package.js";
|
|
3
4
|
/**
|
|
4
5
|
* The bootstrap manifest `packages/kernel/bootstrap.json` (package ecosystem spec §7): the plugins
|
|
5
6
|
* a deployment installs at init, each pinned to an exact version and the sha512 of its tgz. The
|
|
@@ -10,11 +11,19 @@ import { CATALOG_ITEM_NAME_RE, NPM_PACKAGE_NAME_RE, SEMVER_VERSION_RE, SHA512_IN
|
|
|
10
11
|
* the official plugins are published: `path` is packed with `packages/plugin-tools`, which is
|
|
11
12
|
* byte-reproducible, and `integrity` is that pack's sha512).
|
|
12
13
|
*
|
|
14
|
+
* `required` and `builtin` are two different promises. A required entry is imported in the first
|
|
15
|
+
* pass, before the extensions load, and the start fails without it (so it can ship no skills or
|
|
16
|
+
* templates: those need object storage, which is not running then). A `builtin` entry is imported
|
|
17
|
+
* once the extensions have loaded (object storage available), so it may ship skills; its failure
|
|
18
|
+
* is logged and retried on the next start. Both give the plugin a builtin catalog item (scope
|
|
19
|
+
* `all`, on in every org, not switchable) and are imported again when missing. An entry is never
|
|
20
|
+
* both.
|
|
21
|
+
*
|
|
13
22
|
* Objects are strict (unknown keys rejected, `$schema` allowed): the manifest ships with core, so
|
|
14
23
|
* there is no older reader to stay compatible with and a misspelt key is a mistake. The committed
|
|
15
24
|
* JSON Schema (`schemas/bootstrap.v1.schema.json`) is generated from {@link BootstrapManifestSchema}
|
|
16
25
|
* by `pnpm gen:package-schemas`; the cross-field rules (unique names, `marketplace` required with an
|
|
17
|
-
* npm entry) are decided only by {@link parseBootstrapManifest}.
|
|
26
|
+
* npm entry, not both `required` and `builtin`) are decided only by {@link parseBootstrapManifest}.
|
|
18
27
|
*/
|
|
19
28
|
/** Manifest format version. */
|
|
20
29
|
export const BOOTSTRAP_MANIFEST_SCHEMA_VERSION = 1;
|
|
@@ -35,6 +44,7 @@ const common = {
|
|
|
35
44
|
.string()
|
|
36
45
|
.regex(SHA512_INTEGRITY_RE, { message: "integrity must be sha512-<86 base64 chars>==" }),
|
|
37
46
|
required: z.boolean(),
|
|
47
|
+
builtin: z.boolean().optional(),
|
|
38
48
|
};
|
|
39
49
|
const entry = z.discriminatedUnion("source", [
|
|
40
50
|
z.strictObject({
|
|
@@ -74,6 +84,13 @@ const schema = z
|
|
|
74
84
|
});
|
|
75
85
|
}
|
|
76
86
|
seen.add(e.name);
|
|
87
|
+
if (e.required && e.builtin === true) {
|
|
88
|
+
ctx.addIssue({
|
|
89
|
+
code: "custom",
|
|
90
|
+
path: ["plugins", i, "builtin"],
|
|
91
|
+
message: "an entry is either required or builtin, not both",
|
|
92
|
+
});
|
|
93
|
+
}
|
|
77
94
|
});
|
|
78
95
|
if (m.marketplace === undefined && m.plugins.some((e) => e.source === "npm")) {
|
|
79
96
|
ctx.addIssue({
|
|
@@ -107,3 +124,145 @@ export function parseBootstrapManifest(json) {
|
|
|
107
124
|
export function bootstrapFileName(e) {
|
|
108
125
|
return `${e.name}-${e.version}.tgz`;
|
|
109
126
|
}
|
|
127
|
+
/** Extra manifest format version. */
|
|
128
|
+
export const BOOTSTRAP_EXTRA_MANIFEST_SCHEMA_VERSION = 1;
|
|
129
|
+
const RESERVED_MARKETPLACE_NAME = "official";
|
|
130
|
+
const extraCommon = {
|
|
131
|
+
name: common.name,
|
|
132
|
+
version: common.version,
|
|
133
|
+
integrity: common.integrity,
|
|
134
|
+
required: z.boolean().optional(),
|
|
135
|
+
builtin: common.builtin,
|
|
136
|
+
};
|
|
137
|
+
const extraEntry = z.discriminatedUnion("source", [
|
|
138
|
+
z.strictObject({
|
|
139
|
+
...extraCommon,
|
|
140
|
+
source: z.literal("npm"),
|
|
141
|
+
npm: z
|
|
142
|
+
.string()
|
|
143
|
+
.max(214)
|
|
144
|
+
.regex(NPM_PACKAGE_NAME_RE, { message: "npm must be an npm package name" }),
|
|
145
|
+
}),
|
|
146
|
+
z.strictObject({
|
|
147
|
+
...extraCommon,
|
|
148
|
+
source: z.literal("git"),
|
|
149
|
+
marketplace: common.name,
|
|
150
|
+
commit: z.string().regex(COMMIT_RE, { message: "commit must be 40 hex characters" }),
|
|
151
|
+
path: z
|
|
152
|
+
.string()
|
|
153
|
+
.max(512)
|
|
154
|
+
// "." / "./": the repository root (a single-plugin repository)
|
|
155
|
+
.refine((p) => p === "." || p === "./" || (p.startsWith("./") && isSafePackagePath(p.slice(2))), {
|
|
156
|
+
message: 'path must be "." (the repository root) or start with "./" and stay inside the repository',
|
|
157
|
+
}),
|
|
158
|
+
}),
|
|
159
|
+
]);
|
|
160
|
+
const extraMarketplace = z.strictObject({
|
|
161
|
+
name: common.name,
|
|
162
|
+
url: z
|
|
163
|
+
.string()
|
|
164
|
+
.max(URL_CHARS)
|
|
165
|
+
.regex(HTTPS_URL_RE, { message: "url must be an https URL" })
|
|
166
|
+
.refine((s) => {
|
|
167
|
+
try {
|
|
168
|
+
const u = new URL(s);
|
|
169
|
+
return u.username === "" && u.password === "";
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
}, { message: "url must be a valid URL without userinfo" }),
|
|
175
|
+
ref: z.string().min(1).max(256).optional(),
|
|
176
|
+
username: z
|
|
177
|
+
.string()
|
|
178
|
+
.regex(GIT_USERNAME_RE, { message: "username must match ^[A-Za-z0-9._-]{1,64}$" })
|
|
179
|
+
.optional(),
|
|
180
|
+
});
|
|
181
|
+
const extraSchema = z
|
|
182
|
+
.strictObject({
|
|
183
|
+
$schema: z.string().max(URL_CHARS).optional(),
|
|
184
|
+
schemaVersion: z.literal(BOOTSTRAP_EXTRA_MANIFEST_SCHEMA_VERSION),
|
|
185
|
+
marketplaces: z.array(extraMarketplace).max(50),
|
|
186
|
+
plugins: z.array(extraEntry).max(500),
|
|
187
|
+
defaultTheme: z
|
|
188
|
+
.strictObject({
|
|
189
|
+
plugin: common.name,
|
|
190
|
+
theme: z.string().min(1).max(256),
|
|
191
|
+
})
|
|
192
|
+
.optional(),
|
|
193
|
+
})
|
|
194
|
+
.superRefine((m, ctx) => {
|
|
195
|
+
const marketplaces = new Set();
|
|
196
|
+
m.marketplaces.forEach((mk, i) => {
|
|
197
|
+
if (mk.name === RESERVED_MARKETPLACE_NAME) {
|
|
198
|
+
ctx.addIssue({
|
|
199
|
+
code: "custom",
|
|
200
|
+
path: ["marketplaces", i, "name"],
|
|
201
|
+
message: `the name ${RESERVED_MARKETPLACE_NAME} is reserved`,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
if (marketplaces.has(mk.name)) {
|
|
205
|
+
ctx.addIssue({
|
|
206
|
+
code: "custom",
|
|
207
|
+
path: ["marketplaces", i, "name"],
|
|
208
|
+
message: `duplicate marketplace ${mk.name}`,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
marketplaces.add(mk.name);
|
|
212
|
+
});
|
|
213
|
+
const seen = new Set();
|
|
214
|
+
m.plugins.forEach((e, i) => {
|
|
215
|
+
if (seen.has(e.name)) {
|
|
216
|
+
ctx.addIssue({
|
|
217
|
+
code: "custom",
|
|
218
|
+
path: ["plugins", i, "name"],
|
|
219
|
+
message: `duplicate entry ${e.name}`,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
seen.add(e.name);
|
|
223
|
+
if (e.required === true && e.builtin === true) {
|
|
224
|
+
ctx.addIssue({
|
|
225
|
+
code: "custom",
|
|
226
|
+
path: ["plugins", i, "builtin"],
|
|
227
|
+
message: "an entry is either required or builtin, not both",
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
if (e.source === "git" && !marketplaces.has(e.marketplace)) {
|
|
231
|
+
ctx.addIssue({
|
|
232
|
+
code: "custom",
|
|
233
|
+
path: ["plugins", i, "marketplace"],
|
|
234
|
+
message: `marketplace "${e.marketplace}" is not declared`,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
if (m.defaultTheme !== undefined) {
|
|
239
|
+
const target = m.plugins.find((e) => e.name === m.defaultTheme?.plugin);
|
|
240
|
+
if (target === undefined || target.builtin !== true) {
|
|
241
|
+
ctx.addIssue({
|
|
242
|
+
code: "custom",
|
|
243
|
+
path: ["defaultTheme", "plugin"],
|
|
244
|
+
message: "must name an entry of this manifest with builtin: true",
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
/**
|
|
250
|
+
* The zod schema of the extra manifest (strict objects). The JSON Schema is generated with
|
|
251
|
+
* `z.toJSONSchema(BootstrapExtraManifestSchema, { io: "input" })`.
|
|
252
|
+
*/
|
|
253
|
+
export const BootstrapExtraManifestSchema = extraSchema;
|
|
254
|
+
/**
|
|
255
|
+
* Validates and parses the extra manifest. Invalid input returns `{ errors }` (`<path>: <reason>`,
|
|
256
|
+
* at most 50); never throws.
|
|
257
|
+
*/
|
|
258
|
+
export function parseBootstrapExtraManifest(json) {
|
|
259
|
+
const r = BootstrapExtraManifestSchema.safeParse(json);
|
|
260
|
+
if (r.success)
|
|
261
|
+
return { manifest: r.data };
|
|
262
|
+
return {
|
|
263
|
+
errors: r.error.issues.slice(0, MAX_ERRORS).map((i) => {
|
|
264
|
+
const path = i.path.map(String).join(".");
|
|
265
|
+
return path ? `${path}: ${i.message}` : i.message;
|
|
266
|
+
}),
|
|
267
|
+
};
|
|
268
|
+
}
|
package/dist/cards.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { type FormAnswer, type InputForm } from "./tool.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
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";
|
|
@@ -10,22 +11,45 @@ export type ChannelCard = {
|
|
|
10
11
|
toolName: string;
|
|
11
12
|
appName?: string;
|
|
12
13
|
agentName: string;
|
|
14
|
+
/**
|
|
15
|
+
* One line saying what the call does, as the web shows it: the tool's confirmation summary
|
|
16
|
+
* (e.g. a Platform operation's), else the tool's title and key argument; may be empty.
|
|
17
|
+
* Never the raw call input (it may hold a whole prompt, and a group sees the card); the
|
|
18
|
+
* full input is behind `webUrl`. Plain text written by the kernel from model-controlled
|
|
19
|
+
* values: render it as text, never as markup. The name is kept from when it held the input.
|
|
20
|
+
*/
|
|
13
21
|
inputPreview: string;
|
|
14
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>>;
|
|
15
30
|
expiresAt: string;
|
|
16
|
-
|
|
31
|
+
/** `skipped`: the person sent a message instead of deciding (the run went on with it). */
|
|
32
|
+
state: "pending" | "approved" | "denied" | "expired" | "cancelled" | "skipped";
|
|
17
33
|
decidedBy?: string;
|
|
18
34
|
/** A denied approval: the reason the person gave (plain text, never formatted). */
|
|
19
35
|
denyReason?: string;
|
|
20
|
-
/**
|
|
36
|
+
/** Web link to the conversation that can decide this approval. */
|
|
21
37
|
webUrl: string;
|
|
22
38
|
} | {
|
|
23
39
|
kind: "question";
|
|
24
40
|
id: string;
|
|
25
41
|
form: InputForm;
|
|
26
|
-
|
|
42
|
+
/** `skipped`: the person sent a message instead of answering (the run went on with it). */
|
|
43
|
+
state: "pending" | "answered" | "skipped" | "expired" | "cancelled";
|
|
27
44
|
answerSummary?: string;
|
|
28
45
|
webUrl: string;
|
|
46
|
+
/**
|
|
47
|
+
* The person may answer by replying with text instead (pending cards only): `plain` in a DM
|
|
48
|
+
* (any reply reaches the agent), `mention` in a group thread (the reply must mention the bot;
|
|
49
|
+
* the kernel ignores group messages without it). Absent: only the card's own controls work
|
|
50
|
+
* (a sub-agent's question). Render the hint with {@link questionTextReplyHint}.
|
|
51
|
+
*/
|
|
52
|
+
textReply?: "plain" | "mention";
|
|
29
53
|
} | {
|
|
30
54
|
kind: "connect";
|
|
31
55
|
appName: string;
|
|
@@ -35,7 +59,10 @@ export type ChannelCard = {
|
|
|
35
59
|
url: string;
|
|
36
60
|
text: string;
|
|
37
61
|
};
|
|
38
|
-
/**
|
|
62
|
+
/**
|
|
63
|
+
* A user action on an IM card, parsed by the channel plugin and handed to the kernel through an
|
|
64
|
+
* inbound event.
|
|
65
|
+
*/
|
|
39
66
|
export interface CardAction {
|
|
40
67
|
cardKind: "approval" | "question";
|
|
41
68
|
cardId: string;
|
|
@@ -43,22 +70,49 @@ export interface CardAction {
|
|
|
43
70
|
scope?: "once" | "session" | "always";
|
|
44
71
|
answer?: FormAnswer;
|
|
45
72
|
}
|
|
46
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* One message sent to an IM: `text` is Markdown (the channel plugin converts it to native rich
|
|
75
|
+
* text); a card may be attached.
|
|
76
|
+
*/
|
|
47
77
|
export interface OutboundMessage {
|
|
48
78
|
text: string;
|
|
49
79
|
cards?: ChannelCard[];
|
|
50
80
|
}
|
|
51
81
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
82
|
+
* Whether IM can draw the question's controls: exactly one enum field (single choice ≤ 5 options /
|
|
83
|
+
* multi-select ≤ 10). A field with `allowOther` qualifies only when the card has `textReply`
|
|
84
|
+
* (pass `{ textReply: card.textReply !== undefined }`): the Other path is then a plain reply, so
|
|
85
|
+
* never draw an Other button. Otherwise the card only links to the web.
|
|
54
86
|
*/
|
|
55
|
-
export declare function questionRenderableInline(form: InputForm
|
|
56
|
-
|
|
87
|
+
export declare function questionRenderableInline(form: InputForm, opts?: {
|
|
88
|
+
textReply?: boolean;
|
|
89
|
+
}): boolean;
|
|
90
|
+
/** Validates the card structure and returns the list of problems (empty = valid). */
|
|
57
91
|
export declare function validateChannelCard(card: unknown): string[];
|
|
58
92
|
/** A UI locale. */
|
|
59
93
|
export type Locale = "zh" | "en";
|
|
60
94
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
95
|
+
* Puts model-controllable values (approval title, call summary, question title, answer summary)
|
|
96
|
+
* into code, so that in the fallback text they do not turn into links, emphasis or HTML:
|
|
97
|
+
* single-line values use inline code, multi-line values a fenced code block (starting with a
|
|
98
|
+
* newline); the delimiters are always longer than the longest backtick run in the value (CommonMark
|
|
99
|
+
* 6.1 / 4.5).
|
|
100
|
+
* Code is chosen over backslash-escaping punctuation because code is the only form CommonMark / GFM
|
|
101
|
+
* guarantee to display content verbatim (a backslash does not stop GFM bare-URL
|
|
102
|
+
* autolinks, and channel converters treat backslashes inconsistently).
|
|
103
|
+
*/
|
|
104
|
+
export declare function asCode(s: string): string;
|
|
105
|
+
/** The hint line of a pending question card with `textReply`, as {@link renderCardAsText} writes it. */
|
|
106
|
+
export declare function questionTextReplyHint(mode: "plain" | "mention", locale: Locale): string;
|
|
107
|
+
/**
|
|
108
|
+
* The label of a question card's state, as {@link renderCardAsText} writes it. A state this SDK
|
|
109
|
+
* does not know (a newer kernel) returns the state itself, never `undefined`.
|
|
110
|
+
*/
|
|
111
|
+
export declare function questionStateLabel(state: string, locale: Locale): string;
|
|
112
|
+
/**
|
|
113
|
+
* The uniform fallback text for channels without card support (links included); the fixed phrases
|
|
114
|
+
* are taken by locale, zh / en. The result is Markdown (put into
|
|
115
|
+
* `OutboundMessage.text`, converted by the channel plugin): model-controllable fields go into code
|
|
116
|
+
* (see `asCode`), so they never produce links.
|
|
63
117
|
*/
|
|
64
118
|
export declare function renderCardAsText(card: ChannelCard, locale: Locale): string;
|
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;
|
|
@@ -19,11 +21,20 @@ export function questionRenderableInline(form) {
|
|
|
19
21
|
}
|
|
20
22
|
const KINDS = ["approval", "question", "connect", "bind"];
|
|
21
23
|
const SCOPES = ["once", "session", "always"];
|
|
22
|
-
const APPROVAL_STATES = [
|
|
23
|
-
|
|
24
|
+
const APPROVAL_STATES = [
|
|
25
|
+
"pending",
|
|
26
|
+
"approved",
|
|
27
|
+
"denied",
|
|
28
|
+
"expired",
|
|
29
|
+
"cancelled",
|
|
30
|
+
"skipped",
|
|
31
|
+
];
|
|
32
|
+
const MAX_SCOPE_LABEL = 40;
|
|
33
|
+
const QUESTION_STATES = ["pending", "answered", "skipped", "expired", "cancelled"];
|
|
34
|
+
const TEXT_REPLY = ["plain", "mention"];
|
|
24
35
|
const ISO_8601 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})$/;
|
|
25
36
|
const list = (xs) => xs.map((x) => JSON.stringify(x)).join(", ");
|
|
26
|
-
/**
|
|
37
|
+
/** Validates the card structure and returns the list of problems (empty = valid). */
|
|
27
38
|
export function validateChannelCard(card) {
|
|
28
39
|
if (typeof card !== "object" || card === null || Array.isArray(card))
|
|
29
40
|
return ["card must be an object"];
|
|
@@ -58,6 +69,16 @@ export function validateChannelCard(card) {
|
|
|
58
69
|
new Set(s).size !== s.length ||
|
|
59
70
|
s.some((x) => !SCOPES.includes(x)))
|
|
60
71
|
out.push(`scopes must be a non-empty array of ${list(SCOPES)} without duplicates`);
|
|
72
|
+
const labels = c.scopeLabels;
|
|
73
|
+
if (labels !== undefined &&
|
|
74
|
+
(typeof labels !== "object" ||
|
|
75
|
+
labels === null ||
|
|
76
|
+
Array.isArray(labels) ||
|
|
77
|
+
Object.entries(labels).some(([k, v]) => !SCOPES.includes(k) ||
|
|
78
|
+
typeof v !== "string" ||
|
|
79
|
+
v.length === 0 ||
|
|
80
|
+
v.length > MAX_SCOPE_LABEL)))
|
|
81
|
+
out.push(`scopeLabels must map ${list(SCOPES)} to strings of 1 to ${MAX_SCOPE_LABEL} characters`);
|
|
61
82
|
if (typeof c.expiresAt !== "string" ||
|
|
62
83
|
!ISO_8601.test(c.expiresAt) ||
|
|
63
84
|
Number.isNaN(Date.parse(c.expiresAt)))
|
|
@@ -74,7 +95,10 @@ export function validateChannelCard(card) {
|
|
|
74
95
|
url("webUrl");
|
|
75
96
|
if (c.answerSummary !== undefined && typeof c.answerSummary !== "string")
|
|
76
97
|
out.push("answerSummary must be a string");
|
|
77
|
-
|
|
98
|
+
if (c.textReply !== undefined)
|
|
99
|
+
oneOf("textReply", TEXT_REPLY);
|
|
100
|
+
// A form that cannot be answered inside the IM (see questionRenderableInline) is equally
|
|
101
|
+
// valid: the channel renders only the web link.
|
|
78
102
|
for (const p of validateInputForm(c.form))
|
|
79
103
|
out.push(`form: ${p}`);
|
|
80
104
|
return out;
|
|
@@ -92,12 +116,16 @@ export function validateChannelCard(card) {
|
|
|
92
116
|
}
|
|
93
117
|
}
|
|
94
118
|
/**
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
119
|
+
* Puts model-controllable values (approval title, call summary, question title, answer summary)
|
|
120
|
+
* into code, so that in the fallback text they do not turn into links, emphasis or HTML:
|
|
121
|
+
* single-line values use inline code, multi-line values a fenced code block (starting with a
|
|
122
|
+
* newline); the delimiters are always longer than the longest backtick run in the value (CommonMark
|
|
123
|
+
* 6.1 / 4.5).
|
|
124
|
+
* Code is chosen over backslash-escaping punctuation because code is the only form CommonMark / GFM
|
|
125
|
+
* guarantee to display content verbatim (a backslash does not stop GFM bare-URL
|
|
126
|
+
* autolinks, and channel converters treat backslashes inconsistently).
|
|
99
127
|
*/
|
|
100
|
-
function asCode(s) {
|
|
128
|
+
export function asCode(s) {
|
|
101
129
|
const v = s.replace(/\r\n?/g, "\n");
|
|
102
130
|
const longest = Math.max(0, ...[...v.matchAll(/`+/g)].map((m) => m[0].length));
|
|
103
131
|
if (v.includes("\n")) {
|
|
@@ -105,11 +133,15 @@ function asCode(s) {
|
|
|
105
133
|
return `\n${fence}\n${v}\n${fence}`;
|
|
106
134
|
}
|
|
107
135
|
const d = "`".repeat(longest + 1);
|
|
108
|
-
//
|
|
136
|
+
// When the value starts / ends with a backtick, or has spaces at both ends, add one space
|
|
137
|
+
// (stripped when rendered); otherwise it would stick to the delimiter or lose its spaces
|
|
109
138
|
const pad = /^`|`$/.test(v) || (v.startsWith(" ") && v.endsWith(" ")) ? " " : "";
|
|
110
139
|
return `${d}${pad}${v}${pad}${d}`;
|
|
111
140
|
}
|
|
112
|
-
/**
|
|
141
|
+
/**
|
|
142
|
+
* Prefix + code: when the code is a fenced block (starting with a newline), drop the trailing space
|
|
143
|
+
* of the prefix.
|
|
144
|
+
*/
|
|
113
145
|
const withCode = (prefix, code) => code.startsWith("\n") ? `${prefix.trimEnd()}${code}` : `${prefix}${code}`;
|
|
114
146
|
const TEXT = {
|
|
115
147
|
en: {
|
|
@@ -119,7 +151,7 @@ const TEXT = {
|
|
|
119
151
|
connect: (app) => `[Connect] ${app} needs to be connected.`,
|
|
120
152
|
tool: "Tool",
|
|
121
153
|
agent: "Agent",
|
|
122
|
-
|
|
154
|
+
summary: "Summary",
|
|
123
155
|
status: "Status",
|
|
124
156
|
reason: "Reason",
|
|
125
157
|
expires: "Expires",
|
|
@@ -128,19 +160,27 @@ const TEXT = {
|
|
|
128
160
|
answerOnWeb: "Answer on the web",
|
|
129
161
|
details: "Details",
|
|
130
162
|
connectLink: "Connect",
|
|
163
|
+
options: "Options",
|
|
164
|
+
scopes: { once: "Once", session: "This session", always: "Always" },
|
|
131
165
|
approvalStates: {
|
|
132
166
|
pending: "Pending",
|
|
133
167
|
approved: "Approved",
|
|
134
168
|
denied: "Denied",
|
|
135
169
|
expired: "Expired",
|
|
136
170
|
cancelled: "Cancelled",
|
|
171
|
+
skipped: "Skipped",
|
|
137
172
|
},
|
|
138
173
|
questionStates: {
|
|
139
174
|
pending: "Pending",
|
|
140
175
|
answered: "Answered",
|
|
176
|
+
skipped: "Skipped (replied instead)",
|
|
141
177
|
expired: "Expired",
|
|
142
178
|
cancelled: "Cancelled",
|
|
143
179
|
},
|
|
180
|
+
textReply: {
|
|
181
|
+
plain: "You can also just reply with text",
|
|
182
|
+
mention: "You can also reply in this thread, mentioning me",
|
|
183
|
+
},
|
|
144
184
|
},
|
|
145
185
|
zh: {
|
|
146
186
|
sep: ":",
|
|
@@ -149,7 +189,7 @@ const TEXT = {
|
|
|
149
189
|
connect: (app) => `【连接】需要连接 ${app}。`,
|
|
150
190
|
tool: "工具",
|
|
151
191
|
agent: "Agent",
|
|
152
|
-
|
|
192
|
+
summary: "摘要",
|
|
153
193
|
status: "状态",
|
|
154
194
|
reason: "原因",
|
|
155
195
|
expires: "过期时间",
|
|
@@ -158,24 +198,46 @@ const TEXT = {
|
|
|
158
198
|
answerOnWeb: "到网页上回答",
|
|
159
199
|
details: "详情",
|
|
160
200
|
connectLink: "连接",
|
|
201
|
+
options: "选项",
|
|
202
|
+
scopes: { once: "这次", session: "本会话", always: "一直" },
|
|
161
203
|
approvalStates: {
|
|
162
204
|
pending: "待审批",
|
|
163
205
|
approved: "已批准",
|
|
164
206
|
denied: "已拒绝",
|
|
165
207
|
expired: "已过期",
|
|
166
208
|
cancelled: "已取消",
|
|
209
|
+
skipped: "已跳过",
|
|
167
210
|
},
|
|
168
211
|
questionStates: {
|
|
169
212
|
pending: "待回答",
|
|
170
213
|
answered: "已回答",
|
|
214
|
+
skipped: "已跳过(已直接回复)",
|
|
171
215
|
expired: "已过期",
|
|
172
216
|
cancelled: "已取消",
|
|
173
217
|
},
|
|
218
|
+
textReply: {
|
|
219
|
+
plain: "也可以直接回复文字",
|
|
220
|
+
mention: "也可以在本话题 @我 回复文字",
|
|
221
|
+
},
|
|
174
222
|
},
|
|
175
223
|
};
|
|
224
|
+
/** The hint line of a pending question card with `textReply`, as {@link renderCardAsText} writes it. */
|
|
225
|
+
export function questionTextReplyHint(mode, locale) {
|
|
226
|
+
return (TEXT[locale] ?? TEXT.en).textReply[mode];
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* The label of a question card's state, as {@link renderCardAsText} writes it. A state this SDK
|
|
230
|
+
* does not know (a newer kernel) returns the state itself, never `undefined`.
|
|
231
|
+
*/
|
|
232
|
+
export function questionStateLabel(state, locale) {
|
|
233
|
+
const states = (TEXT[locale] ?? TEXT.en).questionStates;
|
|
234
|
+
return states[state] ?? state;
|
|
235
|
+
}
|
|
176
236
|
/**
|
|
177
|
-
*
|
|
178
|
-
*
|
|
237
|
+
* The uniform fallback text for channels without card support (links included); the fixed phrases
|
|
238
|
+
* are taken by locale, zh / en. The result is Markdown (put into
|
|
239
|
+
* `OutboundMessage.text`, converted by the channel plugin): model-controllable fields go into code
|
|
240
|
+
* (see `asCode`), so they never produce links.
|
|
179
241
|
*/
|
|
180
242
|
export function renderCardAsText(card, locale) {
|
|
181
243
|
const t = TEXT[locale] ?? TEXT.en;
|
|
@@ -187,11 +249,17 @@ export function renderCardAsText(card, locale) {
|
|
|
187
249
|
withCode(t.approval, asCode(card.title)),
|
|
188
250
|
line(t.tool, card.appName ? `${card.appName} / ${card.toolName}` : card.toolName),
|
|
189
251
|
line(t.agent, card.agentName),
|
|
190
|
-
line(t.
|
|
252
|
+
...(card.inputPreview !== "" ? [line(t.summary, asCode(card.inputPreview))] : []),
|
|
191
253
|
line(t.status, card.decidedBy ? `${state} (${card.decidedBy})` : state),
|
|
192
254
|
];
|
|
193
255
|
if (card.state === "denied" && card.denyReason)
|
|
194
256
|
lines.push(line(t.reason, asCode(card.denyReason)));
|
|
257
|
+
// the scopes are listed only when the kernel named them (a network ask): the default
|
|
258
|
+
// approval text stays as it was
|
|
259
|
+
if (card.state === "pending" && card.scopeLabels) {
|
|
260
|
+
const labels = card.scopes.map((s) => card.scopeLabels?.[s] ?? t.scopes[s]);
|
|
261
|
+
lines.push(line(t.options, labels.join(" / ")));
|
|
262
|
+
}
|
|
195
263
|
if (card.state === "pending")
|
|
196
264
|
lines.push(line(t.expires, card.expiresAt), line(t.approveOnWeb, card.webUrl));
|
|
197
265
|
else
|
|
@@ -202,11 +270,13 @@ export function renderCardAsText(card, locale) {
|
|
|
202
270
|
const title = typeof card.form.title === "string" ? card.form.title : card.form.title[locale];
|
|
203
271
|
const lines = [
|
|
204
272
|
withCode(t.question, asCode(title)),
|
|
205
|
-
line(t.status,
|
|
273
|
+
line(t.status, questionStateLabel(card.state, locale)),
|
|
206
274
|
];
|
|
207
275
|
if (card.answerSummary)
|
|
208
276
|
lines.push(line(t.answer, asCode(card.answerSummary)));
|
|
209
277
|
lines.push(line(card.state === "pending" ? t.answerOnWeb : t.details, card.webUrl));
|
|
278
|
+
if (card.state === "pending" && card.textReply !== undefined)
|
|
279
|
+
lines.push(questionTextReplyHint(card.textReply, locale));
|
|
210
280
|
return lines.join("\n");
|
|
211
281
|
}
|
|
212
282
|
case "connect":
|