Line data Source code
1 : import {
2 : zAgentSkill,
3 : zAgentSkillListResponse
4 : } from '@comfyorg/ingest-types/zod'
5 :
6 : import { parseErrorResponse } from '@/platform/remote/comfyui/errors'
7 : import { api } from '@/scripts/api'
8 :
9 : import type {
10 : SkillPack,
11 : SkillPackPublishRequest
12 : } from '@/platform/skills/types'
13 : import { MAX_NAME_LENGTH, PACK_NAME_PATTERN } from '@/platform/skills/types'
14 :
15 : /**
16 : * 400 is corrective; 409 is capacity. List/publish 404 hides the feature,
17 : * while delete 404 can also mean the pack is already gone.
18 : */
19 2355 : export class SkillPacksApiError extends Error {
20 : constructor(
21 : message: string,
22 0 : public readonly status: number
23 : ) {
24 0 : super(message)
25 0 : this.name = 'SkillPacksApiError'
26 : }
27 : }
28 :
29 : /**
30 : * Agent responses use `{ error }`; ingest uses the canonical ErrorResponse.
31 : * Preserve either producer's message, preferring explicit `message` fields.
32 : */
33 0 : async function toApiError(response: Response): Promise<SkillPacksApiError> {
34 0 : const body: unknown = await response
35 : .clone()
36 : .json()
37 : .catch(() => undefined)
38 0 : if (
39 : typeof body === 'object' &&
40 : body !== null &&
41 : !('message' in body) &&
42 : 'error' in body &&
43 : typeof body.error === 'string'
44 : ) {
45 0 : return new SkillPacksApiError(body.error, response.status)
46 : }
47 : const errorData = await parseErrorResponse(response)
48 0 : return new SkillPacksApiError(errorData.message, response.status)
49 : }
50 :
51 0 : export async function listSkillPacks(): Promise<SkillPack[]> {
52 0 : const response = await api.fetchApi('/agent/skills')
53 0 : if (!response.ok) throw await toApiError(response)
54 0 : return zAgentSkillListResponse.parse(await response.json()).skills
55 : }
56 :
57 : /** Publishing an existing name replaces that pack. */
58 0 : export async function publishSkillPack(
59 : payload: SkillPackPublishRequest
60 : ): Promise<SkillPack> {
61 0 : const response = await api.fetchApi('/agent/skills', {
62 : method: 'POST',
63 : headers: { 'Content-Type': 'application/json' },
64 : body: JSON.stringify(payload)
65 : })
66 0 : if (!response.ok) throw await toApiError(response)
67 0 : return zAgentSkill.parse(await response.json())
68 : }
69 :
70 0 : export async function deleteSkillPack(name: string): Promise<void> {
71 0 : if (name.length > MAX_NAME_LENGTH || !PACK_NAME_PATTERN.test(name)) {
72 0 : throw new SkillPacksApiError('Invalid skill pack name', 400)
73 : }
74 0 : const response = await api.fetchApi(
75 : `/agent/skills/${encodeURIComponent(name)}`,
76 : { method: 'DELETE' }
77 : )
78 0 : if (!response.ok) throw await toApiError(response)
79 : }
|