@capybara-hq/kernel 0.1.0-rc.59 → 0.1.0-rc.60

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 (139) hide show
  1. package/dist/admin/admin.controller.d.ts +10 -5
  2. package/dist/admin/admin.controller.js +16 -7
  3. package/dist/admin/admin.schemas.js +2 -1
  4. package/dist/admin/audit-log.service.d.ts +1 -0
  5. package/dist/admin/audit-log.service.js +4 -1
  6. package/dist/admin/extensions.schemas.js +2 -0
  7. package/dist/agents/admin-agents.d.ts +1 -0
  8. package/dist/agents/admin-agents.js +4 -1
  9. package/dist/agents/agents.controller.d.ts +1 -10
  10. package/dist/agents/agents.controller.js +2 -14
  11. package/dist/agents/agents.schemas.d.ts +10 -0
  12. package/dist/agents/agents.schemas.js +19 -0
  13. package/dist/background/tasks.service.d.ts +5 -0
  14. package/dist/background/tasks.service.js +10 -3
  15. package/dist/catalog/admin-catalog.schemas.js +2 -0
  16. package/dist/catalog/component-scope.schemas.js +2 -0
  17. package/dist/catalog/marketplaces.service.d.ts +1 -1
  18. package/dist/catalog/org-plugin-uploads.schemas.js +2 -0
  19. package/dist/catalog/requests.service.d.ts +2 -0
  20. package/dist/catalog/requests.service.js +4 -1
  21. package/dist/channels/cards.d.ts +9 -0
  22. package/dist/channels/cards.js +32 -1
  23. package/dist/channels/phrases.d.ts +5 -0
  24. package/dist/channels/phrases.js +5 -0
  25. package/dist/conversations/attachments.service.d.ts +34 -0
  26. package/dist/conversations/attachments.service.js +124 -21
  27. package/dist/conversations/conversations.controller.d.ts +5 -1
  28. package/dist/conversations/conversations.controller.js +11 -5
  29. package/dist/conversations/conversations.service.d.ts +5 -0
  30. package/dist/conversations/conversations.service.js +10 -0
  31. package/dist/conversations/event-people.d.ts +12 -0
  32. package/dist/conversations/event-people.js +28 -0
  33. package/dist/db/kernel-schema.d.ts +18 -0
  34. package/dist/devices/devices.service.d.ts +3 -2
  35. package/dist/devices/devices.service.js +17 -2
  36. package/dist/errors.d.ts +1 -0
  37. package/dist/errors.js +1 -0
  38. package/dist/evals/trajectory.service.d.ts +3 -0
  39. package/dist/evals/trajectory.service.js +2 -0
  40. package/dist/extensions/installs.d.ts +3 -0
  41. package/dist/extensions/installs.js +1 -0
  42. package/dist/http/api.module.js +2 -0
  43. package/dist/http/meta.controller.d.ts +1 -0
  44. package/dist/http/meta.controller.js +6 -1
  45. package/dist/identity/account-avatar.controller.d.ts +30 -0
  46. package/dist/identity/account-avatar.controller.js +117 -0
  47. package/dist/identity/account-avatar.d.ts +67 -0
  48. package/dist/identity/account-avatar.js +136 -0
  49. package/dist/identity/account-avatars.service.d.ts +77 -0
  50. package/dist/identity/account-avatars.service.js +321 -0
  51. package/dist/identity/accounts.d.ts +26 -0
  52. package/dist/identity/accounts.js +70 -2
  53. package/dist/identity/app-auth.controller.d.ts +2 -1
  54. package/dist/identity/app-auth.controller.js +2 -1
  55. package/dist/identity/app-auth.d.ts +24 -7
  56. package/dist/identity/app-auth.js +19 -9
  57. package/dist/identity/app-auth.schemas.d.ts +16 -3
  58. package/dist/identity/app-auth.schemas.js +5 -2
  59. package/dist/identity/external.js +7 -1
  60. package/dist/identity/identity.module.d.ts +2 -1
  61. package/dist/identity/identity.module.js +7 -1
  62. package/dist/identity/me.controller.d.ts +29 -5
  63. package/dist/identity/me.controller.js +57 -6
  64. package/dist/identity/me.schemas.d.ts +36 -0
  65. package/dist/identity/me.schemas.js +30 -0
  66. package/dist/identity/principal.d.ts +2 -2
  67. package/dist/identity/principal.js +2 -2
  68. package/dist/local-devices/local-device-gateway.d.ts +2 -0
  69. package/dist/local-devices/local-device-gateway.js +8 -1
  70. package/dist/local-devices/relay.d.ts +10 -9
  71. package/dist/local-devices/relay.js +32 -9
  72. package/dist/local-devices/targets.d.ts +12 -0
  73. package/dist/local-devices/targets.js +18 -2
  74. package/dist/orgs/members.service.d.ts +3 -1
  75. package/dist/orgs/members.service.js +9 -2
  76. package/dist/platform/exclusions.js +33 -57
  77. package/dist/platform/file-ref.d.ts +87 -0
  78. package/dist/platform/file-ref.js +164 -0
  79. package/dist/platform/operation.d.ts +26 -0
  80. package/dist/platform/ops/agent-skills.ops.d.ts +2 -0
  81. package/dist/platform/ops/agent-skills.ops.js +156 -4
  82. package/dist/platform/ops/agents.ops.d.ts +3 -1
  83. package/dist/platform/ops/agents.ops.js +75 -17
  84. package/dist/platform/ops/approvals.ops.js +5 -0
  85. package/dist/platform/ops/catalog-admin.ops.d.ts +9 -1
  86. package/dist/platform/ops/catalog-admin.ops.js +86 -3
  87. package/dist/platform/ops/components.ops.d.ts +3 -1
  88. package/dist/platform/ops/components.ops.js +61 -2
  89. package/dist/platform/ops/connectors.ops.js +15 -1
  90. package/dist/platform/ops/devices.ops.js +6 -1
  91. package/dist/platform/ops/extensions-admin.ops.d.ts +6 -4
  92. package/dist/platform/ops/extensions-admin.ops.js +109 -6
  93. package/dist/platform/ops/inbox.ops.js +10 -0
  94. package/dist/platform/ops/list-handoff.d.ts +10 -0
  95. package/dist/platform/ops/list-handoff.js +11 -0
  96. package/dist/platform/ops/marketplaces.ops.js +2 -1
  97. package/dist/platform/ops/org-plugins.ops.d.ts +12 -3
  98. package/dist/platform/ops/org-plugins.ops.js +97 -6
  99. package/dist/platform/ops/requests.ops.js +9 -0
  100. package/dist/platform/ops/skills.ops.d.ts +5 -1
  101. package/dist/platform/ops/skills.ops.js +98 -4
  102. package/dist/platform/ops/summary.d.ts +5 -0
  103. package/dist/platform/ops/summary.js +10 -0
  104. package/dist/platform/parity.d.ts +6 -4
  105. package/dist/platform/parity.js +94 -68
  106. package/dist/roles/worker.module.js +5 -0
  107. package/dist/sandbox/request-not-sent.d.ts +13 -0
  108. package/dist/sandbox/request-not-sent.js +18 -0
  109. package/dist/schedules/scheduled-tasks.service.d.ts +10 -6
  110. package/dist/schedules/scheduled-tasks.service.js +13 -16
  111. package/dist/shares/shares.service.d.ts +1 -0
  112. package/dist/shares/shares.service.js +9 -3
  113. package/dist/skills/changes.service.d.ts +1 -0
  114. package/dist/skills/changes.service.js +4 -7
  115. package/dist/skills/skills.controller.d.ts +3 -7
  116. package/dist/skills/skills.controller.js +3 -16
  117. package/dist/skills/skills.schemas.d.ts +5 -0
  118. package/dist/skills/skills.schemas.js +17 -0
  119. package/dist/skills/skills.service.d.ts +3 -1
  120. package/dist/skills/skills.service.js +13 -4
  121. package/dist/tools/admin-connectors.d.ts +1 -0
  122. package/dist/tools/admin-connectors.js +4 -1
  123. package/dist/tools/approvals.service.d.ts +16 -2
  124. package/dist/tools/approvals.service.js +13 -0
  125. package/dist/tools/builtins/builtin-tools.d.ts +3 -0
  126. package/dist/tools/builtins/builtin-tools.js +29 -9
  127. package/dist/tools/builtins/platform.d.ts +23 -0
  128. package/dist/tools/builtins/platform.js +95 -6
  129. package/dist/tools/builtins/sandbox/common.d.ts +5 -0
  130. package/dist/tools/builtins/sandbox/common.js +11 -0
  131. package/dist/tools/builtins/skill-load.js +4 -4
  132. package/dist/tools/pipeline.js +10 -3
  133. package/dist/tools/questions.controller.js +2 -2
  134. package/dist/tools/questions.service.d.ts +7 -1
  135. package/dist/tools/questions.service.js +9 -2
  136. package/dist/tools/registry.d.ts +6 -0
  137. package/dist/tools/tools.module.js +3 -0
  138. package/package.json +3 -3
  139. package/src/migrations/0171_account_avatar.sql +26 -0
@@ -10,6 +10,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
10
10
  import { Inject, Injectable } from "@nestjs/common";
11
11
  import { z } from "zod";
12
12
  import { AgentsService } from "../../agents/agents.service.js";
13
+ import { SKILL_PACKAGE_CONTENT_TYPE, SKILL_PACKAGE_UPLOAD_MAX_BYTES, } from "../../conversations/attachments.service.js";
13
14
  import { ApiError } from "../../errors.js";
14
15
  import { KernelDb } from "../../infra/database/kernel-db.js";
15
16
  import { rootConversationOf } from "../../runtime/subagents/child.js";
@@ -20,10 +21,11 @@ import { SkillInvoker } from "../../skills/invoke.js";
20
21
  import { SKILL_PACKAGE_MAX_BYTES } from "../../skills/package.js";
21
22
  import { AgentSkillPathParams, AgentSkillsListParams, InvocableSkillsQuerySchema, SkillChangePathParams, } from "../../skills/skills.schemas.js";
22
23
  import { SkillsService } from "../../skills/skills.service.js";
24
+ import { FileRefSchema } from "../file-ref.js";
23
25
  import { defineOperation, } from "../operation.js";
24
26
  import { PlatformOperations } from "../registry.js";
25
27
  import { precheckBy, viaAgent } from "./errors.js";
26
- import { named, orElse } from "./summary.js";
28
+ import { named, orElse, skillPackageName } from "./summary.js";
27
29
  const AgentId = AgentSkillsListParams.shape.agentId;
28
30
  const DESCRIPTION_MAX = 200;
29
31
  const clip = (s) => s.length > DESCRIPTION_MAX ? `${s.slice(0, DESCRIPTION_MAX)}…` : s;
@@ -32,6 +34,7 @@ const FILE_PATH_MAX = 4096;
32
34
  const PATH_SHOWN_MAX = 200;
33
35
  const shownPath = (p) => p.length > PATH_SHOWN_MAX ? `${p.slice(0, PATH_SHOWN_MAX)}…` : p;
34
36
  const subagentRefusal = () => new ApiError("skill.subagent_register", "A sub-agent cannot register skills on its agent; only the agent the person talks with can.");
37
+ const subagentUploadRefusal = () => new ApiError("skill.subagent_register", "A sub-agent cannot add skills to an agent; only the agent the person talks with can.");
35
38
  const subagentShareRefusal = () => new ApiError("skill.subagent_register", "A sub-agent cannot share skills; only the agent the person talks with can.");
36
39
  /** File paths shown on a share card (the rest are counted). */
37
40
  const SHARE_FILES_SHOWN = 10;
@@ -41,6 +44,16 @@ function sharedText(o, name) {
41
44
  ? `Shared skill "${name}" with the organization: it is now an org skill (the person maintains it) that every agent of the org has unless it turns it off; this agent keeps using it.`
42
45
  : `Asked the org admins to share skill "${name}" with the organization; it waits for their decision in the inbox. Until then it stays this agent's own skill.`;
43
46
  }
47
+ /** The model-facing result line of `agents.upload-skill` (another conversation's catalog). */
48
+ function uploadedText(o, agent) {
49
+ if (o.kind === "proposed") {
50
+ return `Proposed version ${o.version} of the org skill "${o.name}"; it waits for an org admin to approve it in the inbox.`;
51
+ }
52
+ const head = `${o.created ? "Created" : "Updated"} skill "${o.name}" of agent "${agent}", version ${o.version}.`;
53
+ return o.manualOnly
54
+ ? `${head} It is manual-only: a person invokes it with /${o.name} in a conversation with that agent.`
55
+ : `${head} Conversations with that agent use it from their next run.`;
56
+ }
44
57
  /** The model-facing result line of `agents.register-skill`. */
45
58
  function registeredText(o, files) {
46
59
  const counted = files === null ? "" : ` (${files} file${files === 1 ? "" : "s"})`;
@@ -91,8 +104,8 @@ let AgentSkillsOperations = class AgentSkillsOperations {
91
104
  * Also `org` when the registration may apply a version to a shared org skill at once (the actor
92
105
  * is an org admin maintaining one; the package name is not known before the card).
93
106
  */
94
- async registerClass(ctx) {
95
- const a = await this.agents.get(ctx.orgId, ctx.agentId, ctx.actorAccountId);
107
+ async registerClass(ctx, agentId = ctx.agentId) {
108
+ const a = await this.agents.get(ctx.orgId, agentId, ctx.actorAccountId);
96
109
  const personal = a.visibility === "private" &&
97
110
  a.ownerAccountId === ctx.actorAccountId &&
98
111
  a.definition.skills?.writePolicy !== "ask";
@@ -112,6 +125,13 @@ let AgentSkillsOperations = class AgentSkillsOperations {
112
125
  }
113
126
  await this.agentSkills.assertOwner(ctx.orgId, ctx.agentId, ctx.actorAccountId);
114
127
  }
128
+ /** The refusals of `agents.upload-skill`: a sub-agent conversation, then an actor not the owner. */
129
+ async uploadCheck(agentId, ctx) {
130
+ if ((await rootConversationOf(this.db.db, ctx.conversationId)) !== ctx.conversationId) {
131
+ throw subagentUploadRefusal();
132
+ }
133
+ await this.agentSkills.assertOwner(ctx.orgId, agentId, ctx.actorAccountId);
134
+ }
115
135
  /**
116
136
  * The refusals of sharing read before the card: a sub-agent conversation, an actor who is not
117
137
  * the agent's current owner, a skill that is not one of the agent's own.
@@ -263,6 +283,118 @@ let AgentSkillsOperations = class AgentSkillsOperations {
263
283
  return { id: input.skillId, deleted: true };
264
284
  },
265
285
  }),
286
+ defineOperation({
287
+ id: "agents.upload-skill",
288
+ description: "Upload a skill package onto an agent you own: a zip file of this conversation (an attachment, or a zip you built in the sandbox and delivered with Deliver), passed as file: { fileId }. The zip has SKILL.md at its root or in one top-level folder (frontmatter name and description, then the instructions), plus any scripts or references, at most 50 MiB. It becomes a new skill of the agent, or the next version of its skill with the same name; when you maintain a shared org skill of that name, the version goes to that skill (an org admin's applies at once, anyone else's is proposed to the org admins). Only the agent's owner can; the person sees the file on the card. Takes the agent id from agents.list.",
289
+ input: z
290
+ .object({ agentId: AgentSkillsListParams.shape.agentId, file: FileRefSchema })
291
+ .strict(),
292
+ example: {
293
+ agentId: "7c1a5b0e-9a53-4c6b-8d64-2f5a1b0c3e77",
294
+ file: { fileId: "0b9f2c4e-1d3a-4c55-9e0a-7a2f6d1c8b11" },
295
+ },
296
+ authz: { orgAction: "agents.create" },
297
+ mirrors: ["POST /api/v1/orgs/:orgId/agents/:agentId/skills/uploads"],
298
+ // the upload registration's fields come from the referenced file
299
+ omits: ["name", "size"],
300
+ extras: ["file"],
301
+ confirm: "personal",
302
+ confirmFor: (i, ctx) => this.registerClass(ctx, i.agentId),
303
+ errors: [
304
+ {
305
+ code: "skill.subagent_register",
306
+ next: "Hand the skill package back to your parent agent; only the agent the person talks with uploads skills.",
307
+ },
308
+ {
309
+ code: "forbidden",
310
+ next: "Only the agent's owner can add or update its skills; tell the person, and the owner can upload the package.",
311
+ },
312
+ {
313
+ code: "agent.not_found",
314
+ next: "Check the agent id with agents.list: only agents you can see are readable.",
315
+ },
316
+ {
317
+ code: "skill.invalid_package",
318
+ next: "Fix the package as the message says (SKILL.md at the zip's root with name and description frontmatter, allowed paths and sizes), rebuild the zip, deliver it and retry.",
319
+ },
320
+ {
321
+ code: "skill.agent_cap",
322
+ next: "Upload a new version of an existing skill (same name) instead, or ask the agent's owner to delete one of its skills (agents.skills lists them).",
323
+ },
324
+ {
325
+ code: "skill.name_exists",
326
+ next: "The agent uses an org skill with this name that only its maintainer can update; change the name in SKILL.md, rebuild the zip and upload again, or ask the skill's maintainer.",
327
+ },
328
+ {
329
+ code: "catalog.custom_disabled",
330
+ next: "This deployment does not allow custom org skills, so this version of a shared skill cannot be uploaded; tell the person.",
331
+ },
332
+ {
333
+ code: "skill.proposal_pending_elsewhere",
334
+ next: "Another conversation has a proposed version of this skill waiting for an org admin; wait for that decision.",
335
+ },
336
+ {
337
+ code: "storage.unavailable",
338
+ next: "File storage is unavailable; try again later, or tell the person to contact the super admin.",
339
+ },
340
+ ],
341
+ // as agents.register-skill: only the agent the person talks with writes skills
342
+ precheck: (i, ctx) => precheckBy(() => this.uploadCheck(i.agentId, ctx)),
343
+ fileRef: (i) => ({
344
+ ref: i.file,
345
+ limits: {
346
+ maxBytes: SKILL_PACKAGE_UPLOAD_MAX_BYTES,
347
+ contentTypes: [SKILL_PACKAGE_CONTENT_TYPE],
348
+ },
349
+ }),
350
+ summary: async (i, ctx) => {
351
+ const agent = await orElse(async () => named((await this.agents.get(ctx.orgId, i.agentId, ctx.actorAccountId)).definition.name), i.agentId);
352
+ const f = named(ctx.file?.name ?? "?");
353
+ // the name SKILL.md declares, and whether the agent has its own skill of that name
354
+ const name = await skillPackageName(ctx.file?.bytes);
355
+ if (name === null) {
356
+ return {
357
+ zh: `把技能包「${f}」上传为智能体「${agent}」的技能`,
358
+ en: `Upload the skill package "${f}" as a skill of agent "${agent}"`,
359
+ };
360
+ }
361
+ const n = named(name);
362
+ const exists = await orElse(async () => (await this.skills.listOwnForAgent(ctx.orgId, i.agentId)).some((s) => s.name === name && !s.shared), false);
363
+ return exists
364
+ ? {
365
+ zh: `给智能体「${agent}」的技能「${n}」新增一个版本(技能包「${f}」)`,
366
+ en: `Add a new version of skill "${n}" of agent "${agent}" from the package "${f}"`,
367
+ }
368
+ : {
369
+ zh: `为智能体「${agent}」添加技能「${n}」(技能包「${f}」)`,
370
+ en: `Add skill "${n}" to agent "${agent}" from the package "${f}"`,
371
+ };
372
+ },
373
+ run: async (input, ctx) => {
374
+ const f = ctx.file;
375
+ if (f === undefined)
376
+ throw new Error("agents.upload-skill: the file is missing");
377
+ await this.uploadCheck(input.agentId, ctx);
378
+ // the route's register step on the bytes the person confirmed (no upload row needed)
379
+ const o = await this.agentSkills.register({
380
+ orgId: ctx.orgId,
381
+ agentId: input.agentId,
382
+ accountId: ctx.actorAccountId,
383
+ auditDetail: viaAgent("agents.upload-skill", ctx),
384
+ }, f.bytes);
385
+ const agent = await orElse(async () => (await this.agents.get(ctx.orgId, input.agentId, ctx.actorAccountId)).definition.name, input.agentId);
386
+ return {
387
+ skill: {
388
+ id: o.skillId,
389
+ name: o.name,
390
+ state: o.kind === "proposed" ? "pending" : o.manualOnly ? "manual" : "active",
391
+ version: o.version,
392
+ ...(o.kind === "proposed" && { requestId: o.requestId }),
393
+ },
394
+ message: uploadedText(o, named(agent)),
395
+ };
396
+ },
397
+ }),
266
398
  defineOperation({
267
399
  id: "agents.register-skill",
268
400
  description: "Register a zip package from your sandbox as one of this agent's own skills: a new skill, or the next version of this agent's skill with the same name. Build the package in the sandbox first: a zip with SKILL.md at its root (frontmatter name and description, then the instructions) plus any scripts or references, then pass its absolute path. Only the agent's owner can, and only from the conversation the person talks in (not a sub-agent). The new version applies to every conversation with this agent; in this run, load it with Skill. When the person maintains a shared org skill of the same name (and this agent has no own skill of it), the package is that skill's next version: an org admin applies it at once, anyone else's is proposed to the org admins and only this conversation uses it until approved.",
@@ -473,6 +605,8 @@ let AgentSkillsOperations = class AgentSkillsOperations {
473
605
  ],
474
606
  run: async (input, ctx) => {
475
607
  const d = await this.changes.get(ctx.orgId, input.changeId, ctx.actorAccountId);
608
+ // org admins decide a change; the person who asked only reads it
609
+ const canDecide = d.status === "pending" && (await isOrgAdmin(this.db.db, ctx.orgId, ctx.actorAccountId));
476
610
  return {
477
611
  skillChange: {
478
612
  id: d.id,
@@ -488,12 +622,30 @@ let AgentSkillsOperations = class AgentSkillsOperations {
488
622
  conversationPath: d.conversationPath,
489
623
  decidedAt: d.decidedAt,
490
624
  createdAt: d.createdAt,
625
+ canDecide,
491
626
  ...(d.status === "pending" && {
492
- next: "Review the files and decide it on the web, in the inbox.",
627
+ next: canDecide
628
+ ? "Review the files and decide it on the web, in the inbox."
629
+ : "It waits for an org admin's decision in their inbox.",
493
630
  }),
494
631
  },
495
632
  };
496
633
  },
634
+ // the link card only for whoever can decide it (an org admin), on a pending change
635
+ handoff: (_i, result) => {
636
+ const c = result
637
+ ?.skillChange;
638
+ if (c?.state !== "pending" || c.canDecide !== true)
639
+ return null;
640
+ return {
641
+ kind: "handoff",
642
+ title: {
643
+ zh: "在收件箱审阅并决定这项技能变更",
644
+ en: "Review and decide the skill change in the inbox",
645
+ },
646
+ target: { type: "link", url: "/inbox" },
647
+ };
648
+ },
497
649
  }),
498
650
  ]);
499
651
  }
@@ -6,6 +6,7 @@ import { OrgAgentTemplates } from "../../agents/org-templates.js";
6
6
  import { PendingConnectorsService } from "../../agents/pending-connectors.service.js";
7
7
  import { ReadinessService } from "../../agents/readiness.service.js";
8
8
  import { CatalogRequestsService } from "../../catalog/requests.service.js";
9
+ import { AttachmentsService } from "../../conversations/attachments.service.js";
9
10
  import { KernelDb } from "../../infra/database/kernel-db.js";
10
11
  import { ConnectorsService } from "../../tools/connectors.service.js";
11
12
  import { PlatformOperations } from "../registry.js";
@@ -21,7 +22,8 @@ export declare class AgentsOperations implements OnModuleInit {
21
22
  private readonly connectors;
22
23
  private readonly readiness;
23
24
  private readonly kdb;
24
- constructor(registry: PlatformOperations, agents: AgentsService, profiles: AgentProfileService, fromTemplate: AgentsFromTemplate, orgTemplates: OrgAgentTemplates, pending: PendingConnectorsService, requests: CatalogRequestsService, connectors: ConnectorsService, readiness: ReadinessService, kdb: KernelDb);
25
+ private readonly attachments;
26
+ constructor(registry: PlatformOperations, agents: AgentsService, profiles: AgentProfileService, fromTemplate: AgentsFromTemplate, orgTemplates: OrgAgentTemplates, pending: PendingConnectorsService, requests: CatalogRequestsService, connectors: ConnectorsService, readiness: ReadinessService, kdb: KernelDb, attachments: AttachmentsService);
25
27
  /**
26
28
  * The edit rule the service enforces, read before the card: the agent is visible, and the actor
27
29
  * owns it or is an org admin. Returns the agent too for the checks that follow.
@@ -20,10 +20,12 @@ import { PendingConnectorsService } from "../../agents/pending-connectors.servic
20
20
  import { PresetAvatarSchema } from "../../agents/profile.js";
21
21
  import { ReadinessService } from "../../agents/readiness.service.js";
22
22
  import { CatalogRequestsService } from "../../catalog/requests.service.js";
23
+ import { AGENT_AVATAR_CONTENT_TYPES, AGENT_AVATAR_MAX_BYTES, AttachmentsService, } from "../../conversations/attachments.service.js";
23
24
  import { KernelDb } from "../../infra/database/kernel-db.js";
24
25
  import { roleAtLeast } from "../../orgs/roles.js";
25
26
  import { rootConversationOf } from "../../runtime/subagents/child.js";
26
27
  import { ConnectorsService } from "../../tools/connectors.service.js";
28
+ import { FileRefSchema } from "../file-ref.js";
27
29
  import { defineOperation, } from "../operation.js";
28
30
  import { PlatformOperations } from "../registry.js";
29
31
  import { precheckBy, viaAgent } from "./errors.js";
@@ -61,20 +63,31 @@ const Fields = {
61
63
  subagents: Def.subagents.unwrap(),
62
64
  };
63
65
  /**
64
- * The profile fields the tool sets: a preset avatar (or none) and the quick actions. An uploaded
65
- * image is the web's (byte upload); the description moves into the agent definition (run-context
66
- * spec §3.5), so it is not mirrored here.
66
+ * The profile fields the tool sets: an avatar (a preset, an image of the conversation, or none)
67
+ * and the quick actions. The description moves into the agent definition (run-context spec §3.5),
68
+ * so it is not mirrored here.
67
69
  */
68
70
  const Profile = AgentProfileBody.shape;
69
71
  const PresetAvatar = PresetAvatarSchema;
72
+ /**
73
+ * An image of the conversation as the avatar (platform web parity spec §5): in place of the
74
+ * route's `upload` kind, the kernel stores the file as an avatar upload and saves that.
75
+ */
76
+ const FileAvatar = z.object({ kind: z.literal("file"), file: FileRefSchema }).strict();
70
77
  /** One change of `agents.update-profile` as the card says it. */
71
- function profileChanges(i) {
78
+ function profileChanges(i, fileName) {
72
79
  const zh = [];
73
80
  const en = [];
74
81
  if (i.avatar === null) {
75
82
  zh.push("移除头像");
76
83
  en.push("remove the avatar");
77
84
  }
85
+ else if (i.avatar?.kind === "file") {
86
+ // the card's file row shows the image itself; the name is the file's, clipped
87
+ const n = named(fileName ?? "?");
88
+ zh.push(`头像设为图片「${n}」`);
89
+ en.push(`set the avatar to the image "${n}"`);
90
+ }
78
91
  else if (i.avatar !== undefined) {
79
92
  zh.push(`头像设为预设「${i.avatar.id}」`);
80
93
  en.push(`set the avatar to preset "${i.avatar.id}"`);
@@ -115,7 +128,8 @@ let AgentsOperations = class AgentsOperations {
115
128
  connectors;
116
129
  readiness;
117
130
  kdb;
118
- constructor(registry, agents, profiles, fromTemplate, orgTemplates, pending, requests, connectors, readiness, kdb) {
131
+ attachments;
132
+ constructor(registry, agents, profiles, fromTemplate, orgTemplates, pending, requests, connectors, readiness, kdb, attachments) {
119
133
  this.registry = registry;
120
134
  this.agents = agents;
121
135
  this.profiles = profiles;
@@ -126,6 +140,7 @@ let AgentsOperations = class AgentsOperations {
126
140
  this.connectors = connectors;
127
141
  this.readiness = readiness;
128
142
  this.kdb = kdb;
143
+ this.attachments = attachments;
129
144
  }
130
145
  /**
131
146
  * The edit rule the service enforces, read before the card: the agent is visible, and the actor
@@ -336,12 +351,11 @@ let AgentsOperations = class AgentsOperations {
336
351
  }),
337
352
  defineOperation({
338
353
  id: "agents.update-profile",
339
- description: "Change how an agent you may edit looks to people: its avatar (one of the preset animals, or null for none) and its quick actions (the prompts offered when a conversation starts; the list you send replaces the current one, [] clears it). Send at least one of the two. The current profile is in agents.get. The description is part of the definition: change it with agents.update. A custom avatar image is uploaded on the agents page of the web app. A change to your own private agent may be remembered by the person; a change to a shared agent is confirmed every time.",
354
+ description: 'Change how an agent you may edit looks to people: its avatar (one of the preset animals; an image of this conversation as { kind: "file", file: { fileId } }, e.g. a generated image, an attachment or a delivered file, PNG, JPEG or WebP up to 2 MiB, which the person sees on the card; or null for none) and its quick actions (the prompts offered when a conversation starts; the list you send replaces the current one, [] clears it). Send at least one of the two. The current profile is in agents.get. The description is part of the definition: change it with agents.update. A change to your own private agent may be remembered by the person (never one that takes an image); a change to a shared agent is confirmed every time.',
340
355
  input: z
341
356
  .object({
342
357
  agentId: AgentId,
343
- // a preset only: the route's `upload` kind needs a byte upload, which stays on the web
344
- avatar: PresetAvatar.nullable().optional(),
358
+ avatar: z.union([PresetAvatar, FileAvatar]).nullable().optional(),
345
359
  quickActions: Profile.quickActions,
346
360
  })
347
361
  .strict()
@@ -356,11 +370,17 @@ let AgentsOperations = class AgentsOperations {
356
370
  ],
357
371
  },
358
372
  authz: { orgAction: "agents.create" },
359
- mirrors: ["PATCH /api/v1/orgs/:orgId/agents/:agentId/profile"],
373
+ mirrors: [
374
+ "PATCH /api/v1/orgs/:orgId/agents/:agentId/profile",
375
+ "POST /api/v1/orgs/:orgId/agents/:agentId/avatar/uploads",
376
+ ],
360
377
  // `expectedUpdatedAt` is read by the operation itself (the route takes no description: it
361
- // is saved with the definition, agents.update). The narrower avatar (no `upload` kind)
362
- // needs no declaration: an input that accepts less than the route passes the parity test.
363
- omits: ["expectedUpdatedAt"],
378
+ // is saved with the definition, agents.update); the upload registration's fields come
379
+ // from the referenced file
380
+ omits: ["expectedUpdatedAt", "name", "contentType", "size"],
381
+ loosens: {
382
+ avatar: "the `file` kind takes an image of the conversation in place of the route's `upload` kind (spec §5): the kernel stores it as an avatar upload and saves that",
383
+ },
364
384
  confirm: "personal",
365
385
  confirmFor: (i, ctx) => this.updateClass(i.agentId, undefined, ctx),
366
386
  errors: [
@@ -379,15 +399,32 @@ let AgentsOperations = class AgentsOperations {
379
399
  code: "agent.profile_conflict",
380
400
  next: "Someone changed the profile while you were waiting: read it again with agents.get, then call again.",
381
401
  },
402
+ {
403
+ code: "attachment.too_many_pending",
404
+ next: "Too many avatar images are stored and not used yet; wait a day for them to expire, or save one of them.",
405
+ },
406
+ {
407
+ code: "storage.unavailable",
408
+ next: "File storage is unavailable, so an image cannot be stored; use a preset avatar, or tell the person to contact the super admin.",
409
+ },
382
410
  ],
383
411
  precheck: async (i, ctx) => {
384
412
  const { agent: _a, ...check } = await this.editCheck(i.agentId, ctx);
385
413
  return check;
386
414
  },
415
+ fileRef: (i) => i.avatar?.kind === "file"
416
+ ? {
417
+ ref: i.avatar.file,
418
+ limits: {
419
+ maxBytes: AGENT_AVATAR_MAX_BYTES,
420
+ contentTypes: AGENT_AVATAR_CONTENT_TYPES,
421
+ },
422
+ }
423
+ : null,
387
424
  summary: async (i, ctx) => {
388
425
  const a = await orElse(() => this.agents.get(ctx.orgId, i.agentId, ctx.actorAccountId), null);
389
426
  const n = named(a?.definition.name ?? i.agentId);
390
- const c = profileChanges(i);
427
+ const c = profileChanges(i, ctx.file?.name);
391
428
  return a?.visibility === "private"
392
429
  ? {
393
430
  zh: `修改私有智能体「${n}」的资料:${c.zh.join(";")}`,
@@ -401,10 +438,29 @@ let AgentsOperations = class AgentsOperations {
401
438
  run: async (input, ctx) => {
402
439
  // the profile as of now: the person approved this change, not a guard on an earlier read
403
440
  const current = await this.agents.get(ctx.orgId, input.agentId, ctx.actorAccountId);
404
- const profile = await this.profiles.update(ctx.orgId, input.agentId, ctx.actorAccountId, {
405
- ...(input.avatar !== undefined && { avatar: input.avatar }),
441
+ let avatar = input.avatar?.kind === "file" ? undefined : input.avatar;
442
+ if (input.avatar?.kind === "file") {
443
+ // the bytes the person confirmed (the tool verified their sha256), stored as the
444
+ // avatar route stores an upload, then saved as that upload
445
+ const f = ctx.file;
446
+ if (f === undefined)
447
+ throw new Error("agents.update-profile: the file is missing");
448
+ const { id } = await this.attachments.createFromBytes(ctx.orgId, ctx.actorAccountId, "agent-avatar", { name: f.name, contentType: f.contentType, bytes: f.bytes });
449
+ avatar = { kind: "upload", uploadId: id };
450
+ }
451
+ const stored = avatar?.kind === "upload" ? avatar.uploadId : null;
452
+ const profile = await this.profiles
453
+ .update(ctx.orgId, input.agentId, ctx.actorAccountId, {
454
+ ...(avatar !== undefined && { avatar }),
406
455
  ...(input.quickActions !== undefined && { quickActions: input.quickActions }),
407
- }, current.profile.updatedAt, undefined, viaAgent("agents.update-profile", ctx));
456
+ }, current.profile.updatedAt, undefined, viaAgent("agents.update-profile", ctx))
457
+ .catch(async (e) => {
458
+ // the stored image no profile took: out of the cap, cleaned up
459
+ if (stored !== null) {
460
+ await this.attachments.discardUpload(ctx.orgId, ctx.actorAccountId, stored);
461
+ }
462
+ throw e;
463
+ });
408
464
  return { id: input.agentId, profile };
409
465
  },
410
466
  }),
@@ -667,6 +723,7 @@ let AgentsOperations = class AgentsOperations {
667
723
  const r = await this.pending.reopen(ctx.orgId, input.agentId, ctx.actorAccountId, undefined, viaAgent("agents.request-connector-setup", ctx));
668
724
  return { created: r.created, request: await this.requests.view(r.id) };
669
725
  },
726
+ noHandoff: "The asker is not the one who decides: the request lands in the org admins' inbox, and the admins are told there.",
670
727
  }),
671
728
  defineOperation({
672
729
  id: "agents.connector-setup-status",
@@ -718,6 +775,7 @@ AgentsOperations = __decorate([
718
775
  __param(6, Inject(CatalogRequestsService)),
719
776
  __param(7, Inject(ConnectorsService)),
720
777
  __param(8, Inject(ReadinessService)),
721
- __param(9, Inject(KernelDb))
778
+ __param(9, Inject(KernelDb)),
779
+ __param(10, Inject(AttachmentsService))
722
780
  ], AgentsOperations);
723
781
  export { AgentsOperations };
@@ -13,6 +13,7 @@ import { ApprovalPathParams, ApprovalsListQuerySchema } from "../../tools/approv
13
13
  import { ApprovalsService } from "../../tools/approvals.service.js";
14
14
  import { defineOperation } from "../operation.js";
15
15
  import { PlatformOperations } from "../registry.js";
16
+ import { listCard } from "./list-handoff.js";
16
17
  import { Cursor, pageOf, startOf } from "./paging.js";
17
18
  /** Where a person decides an approval: never through an agent (spec §6.4, §14.1 item 2). */
18
19
  export const DECIDE_ON_WEB = "Decide it on the web, in the inbox or in its conversation.";
@@ -86,6 +87,10 @@ let ApprovalsOperations = class ApprovalsOperations {
86
87
  const all = await this.approvals.list(ctx.orgId, ctx.actorAccountId, input.status ?? "pending");
87
88
  return pageOf(all.map(approvalRow), startOf(input.cursor), 50);
88
89
  },
90
+ handoff: (_i, result) => listCard(result, "/inbox", {
91
+ zh: "去收件箱处理待审批的调用",
92
+ en: "Decide the waiting approvals in the inbox",
93
+ }),
89
94
  }),
90
95
  defineOperation({
91
96
  id: "approvals.get",
@@ -4,13 +4,20 @@ import { CatalogService } from "../../catalog/catalog.service.js";
4
4
  import { CatalogScopeService } from "../../catalog/catalog-scope.service.js";
5
5
  import { PlatformImportUpdate } from "../../catalog/platform-import-update.js";
6
6
  import { PluginImport, type PluginReview } from "../../catalog/plugin-import.js";
7
+ import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
7
8
  import { KernelDb } from "../../infra/database/kernel-db.js";
8
9
  import { ThemesService } from "../../themes/themes.service.js";
10
+ import { type HandoffView } from "../operation.js";
9
11
  import { PlatformOperations } from "../registry.js";
10
12
  /** `plugins.set-shown` on a plugin an org owns. */
11
13
  export declare const ORG_OWNED = "An org owns this plugin and manages who sees it; the super admin changes that in the admin console, not here.";
12
14
  /** Where the person confirms a staged import (the admin console's import review). */
13
15
  export declare const reviewUrl: (stagingId: string) => string;
16
+ /**
17
+ * The link card of a prepared import (platform web parity spec §6): the review page of its staged
18
+ * import, titled by the plugin's name. Null when the result carries no staging id.
19
+ */
20
+ export declare const importHandoff: (result: unknown) => HandoffView | null;
14
21
  /** What only a person does with a staged import, after reading its review on the web. */
15
22
  export declare const CONFIRM_ON_WEB = "Nothing is imported yet. Give the person the reviewUrl: they read the review (the extension's permissions among it) and confirm the import on the web. Confirming approves extension permissions, which only a person does.";
16
23
  /** A staged import's review as the model needs it: what it brings, not the files themselves. */
@@ -75,7 +82,8 @@ export declare class CatalogAdminOperations implements OnModuleInit {
75
82
  private readonly kdb;
76
83
  private readonly appearance;
77
84
  private readonly themes;
78
- constructor(registry: PlatformOperations, catalog: CatalogService, scope: CatalogScopeService, pluginImport: PluginImport, importUpdate: PlatformImportUpdate, kdb: KernelDb, appearance: AppearanceService, themes: ThemesService);
85
+ private readonly config;
86
+ constructor(registry: PlatformOperations, catalog: CatalogService, scope: CatalogScopeService, pluginImport: PluginImport, importUpdate: PlatformImportUpdate, kdb: KernelDb, appearance: AppearanceService, themes: ThemesService, config: KernelConfigService);
79
87
  /** The precheck refusal of another org's private plugin (the phase-1 rule), else null. */
80
88
  private otherOrgs;
81
89
  /** Whether an org owns the item (its owner org manages it, not the super admin's agent). */
@@ -17,13 +17,15 @@ import { CatalogScopeService } from "../../catalog/catalog-scope.service.js";
17
17
  import { PlatformImportUpdate } from "../../catalog/platform-import-update.js";
18
18
  import { PluginImport } from "../../catalog/plugin-import.js";
19
19
  import { ApiError } from "../../errors.js";
20
+ import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
20
21
  import { KernelDb } from "../../infra/database/kernel-db.js";
21
22
  import { ThemesService } from "../../themes/themes.service.js";
23
+ import { FileRefSchema } from "../file-ref.js";
22
24
  import { defineOperation } from "../operation.js";
23
25
  import { PlatformOperations } from "../registry.js";
24
26
  import { precheckBy, viaAgent } from "./errors.js";
25
27
  import { assertNotOtherOrgs, OTHER_ORG } from "./other-orgs.js";
26
- import { localized, orElse } from "./summary.js";
28
+ import { localized, named, orElse } from "./summary.js";
27
29
  /** A platform catalog item id (`:id` of the admin routes), named `itemId` like the org's. */
28
30
  const ItemId = CatalogIdPathParams.shape.id.max(100);
29
31
  const StagingId = CatalogStagingPathParams.shape.stagingId.max(100);
@@ -31,6 +33,23 @@ const StagingId = CatalogStagingPathParams.shape.stagingId.max(100);
31
33
  export const ORG_OWNED = "An org owns this plugin and manages who sees it; the super admin changes that in the admin console, not here.";
32
34
  /** Where the person confirms a staged import (the admin console's import review). */
33
35
  export const reviewUrl = (stagingId) => `/admin/plugins/import?staging=${stagingId}`;
36
+ /**
37
+ * The link card of a prepared import (platform web parity spec §6): the review page of its staged
38
+ * import, titled by the plugin's name. Null when the result carries no staging id.
39
+ */
40
+ export const importHandoff = (result) => {
41
+ const r = result;
42
+ if (typeof r?.stagingId !== "string")
43
+ return null;
44
+ const name = typeof r.review?.name === "string" ? named(r.review.name) : null;
45
+ return {
46
+ kind: "handoff",
47
+ title: name === null
48
+ ? { zh: "审阅插件的权限", en: "Review the plugin's permissions" }
49
+ : { zh: `审阅插件 ${name} 的权限`, en: `Review the permissions of plugin ${name}` },
50
+ target: { type: "link", url: reviewUrl(r.stagingId) },
51
+ };
52
+ };
34
53
  /** What only a person does with a staged import, after reading its review on the web. */
35
54
  export const CONFIRM_ON_WEB = "Nothing is imported yet. Give the person the reviewUrl: they read the review (the extension's permissions among it) and confirm the import on the web. Confirming approves extension permissions, which only a person does.";
36
55
  /** A staged import's review as the model needs it: what it brings, not the files themselves. */
@@ -130,7 +149,8 @@ let CatalogAdminOperations = class CatalogAdminOperations {
130
149
  kdb;
131
150
  appearance;
132
151
  themes;
133
- constructor(registry, catalog, scope, pluginImport, importUpdate, kdb, appearance, themes) {
152
+ config;
153
+ constructor(registry, catalog, scope, pluginImport, importUpdate, kdb, appearance, themes, config) {
134
154
  this.registry = registry;
135
155
  this.catalog = catalog;
136
156
  this.scope = scope;
@@ -139,6 +159,7 @@ let CatalogAdminOperations = class CatalogAdminOperations {
139
159
  this.kdb = kdb;
140
160
  this.appearance = appearance;
141
161
  this.themes = themes;
162
+ this.config = config;
142
163
  }
143
164
  /** The precheck refusal of another org's private plugin (the phase-1 rule), else null. */
144
165
  async otherOrgs(itemId, orgId) {
@@ -244,6 +265,58 @@ let CatalogAdminOperations = class CatalogAdminOperations {
244
265
  next: CONFIRM_ON_WEB,
245
266
  };
246
267
  },
268
+ handoff: (_i, result) => importHandoff(result),
269
+ }),
270
+ defineOperation({
271
+ id: "plugins.import-file",
272
+ description: "Prepare importing a plugin package into the catalog (super admin): a .tgz plugin package of this conversation, e.g. one you packed in the sandbox and delivered with Deliver, passed as file: { fileId }; at most the deployment's package limit. Checks the whole plugin and stages it for 24 hours. Returns what it brings and a reviewUrl; nothing is imported until the person confirms it there.",
273
+ input: z.object({ file: FileRefSchema }).strict(),
274
+ example: { file: { fileId: "0b9f2c4e-1d3a-4c55-9e0a-7a2f6d1c8b11" } },
275
+ authz: { superAdmin: true },
276
+ mirrors: ["POST /api/v1/admin/catalog/import/file"],
277
+ // the route's raw body
278
+ extras: ["file"],
279
+ confirm: "platform",
280
+ errors: [
281
+ {
282
+ code: "catalog.version_exists",
283
+ next: "This version is in the catalog already: update the plugin to it with plugins.update-preview, or raise the version and pack it again.",
284
+ },
285
+ {
286
+ code: "catalog.name_conflict",
287
+ next: "A catalog plugin of this name comes from another npm package; check the package name.",
288
+ },
289
+ {
290
+ code: "catalog.invalid_manifest",
291
+ next: "The package is not a valid plugin (the details list each problem); fix it, pack and deliver it again.",
292
+ },
293
+ ],
294
+ fileRef: (i) => ({
295
+ ref: i.file,
296
+ // the route takes the raw bytes whatever their type (a delivered .tgz is
297
+ // application/octet-stream); the package reader checks them
298
+ limits: { maxBytes: this.config.get().packages.maxBytes },
299
+ }),
300
+ summary: (_i, ctx) => {
301
+ const f = named(ctx.file?.name ?? "?");
302
+ return {
303
+ zh: `准备从文件「${f}」导入插件(确认在网页上进行)`,
304
+ en: `Prepare importing a plugin from the file "${f}" (confirmed on the web)`,
305
+ };
306
+ },
307
+ run: async (_input, ctx) => {
308
+ const f = ctx.file;
309
+ if (f === undefined)
310
+ throw new Error("plugins.import-file: the file is missing");
311
+ const r = await this.pluginImport.prepare(ctx.actorAccountId, f.bytes, { kind: "file", ref: "upload" }, { auditDetail: viaAgent("plugins.import-file", ctx) });
312
+ return {
313
+ stagingId: r.stagingId,
314
+ review: reviewBrief(r.review),
315
+ reviewUrl: reviewUrl(r.stagingId),
316
+ next: CONFIRM_ON_WEB,
317
+ };
318
+ },
319
+ handoff: (_i, result) => importHandoff(result),
247
320
  }),
248
321
  defineOperation({
249
322
  id: "plugins.import-git",
@@ -288,6 +361,7 @@ let CatalogAdminOperations = class CatalogAdminOperations {
288
361
  next: CONFIRM_ON_WEB,
289
362
  };
290
363
  },
364
+ handoff: (_i, result) => importHandoff(result),
291
365
  }),
292
366
  defineOperation({
293
367
  id: "plugins.import-review",
@@ -339,6 +413,14 @@ let CatalogAdminOperations = class CatalogAdminOperations {
339
413
  reviewUrl: reviewUrl(input.stagingId),
340
414
  next: CONFIRM_ON_WEB,
341
415
  }),
416
+ handoff: (input) => ({
417
+ kind: "handoff",
418
+ title: {
419
+ zh: "审阅并确认平台插件更新",
420
+ en: "Review and confirm the platform plugin update",
421
+ },
422
+ target: { type: "link", url: reviewUrl(input.stagingId) },
423
+ }),
342
424
  }),
343
425
  defineOperation({
344
426
  id: "plugins.scope-preview",
@@ -705,6 +787,7 @@ CatalogAdminOperations = __decorate([
705
787
  __param(4, Inject(PlatformImportUpdate)),
706
788
  __param(5, Inject(KernelDb)),
707
789
  __param(6, Inject(AppearanceService)),
708
- __param(7, Inject(ThemesService))
790
+ __param(7, Inject(ThemesService)),
791
+ __param(8, Inject(KernelConfigService))
709
792
  ], CatalogAdminOperations);
710
793
  export { CatalogAdminOperations };