|
| 1 | +/** |
| 2 | + * SkillService - Effect service for skill CRUD and search operations. |
| 3 | + * |
| 4 | + * Wraps the shared skill utilities from `@okcode/shared/skill` as an Effect |
| 5 | + * service using the project's `ServiceMap.Service` pattern. |
| 6 | + * |
| 7 | + * @module SkillService |
| 8 | + */ |
| 9 | +import type { |
| 10 | + SkillCreateResult, |
| 11 | + SkillListResult, |
| 12 | + SkillReadResult, |
| 13 | + SkillSearchResult, |
| 14 | +} from "@okcode/contracts"; |
| 15 | +import { Effect, Layer, Schema, ServiceMap } from "effect"; |
| 16 | +import { |
| 17 | + listSkills, |
| 18 | + readSkill, |
| 19 | + searchSkills, |
| 20 | + createSkill, |
| 21 | + deleteSkill, |
| 22 | +} from "@okcode/shared/skill"; |
| 23 | + |
| 24 | +/** |
| 25 | + * SkillServiceError - Tagged error for skill service failures. |
| 26 | + */ |
| 27 | +export class SkillServiceError extends Schema.TaggedErrorClass<SkillServiceError>()( |
| 28 | + "SkillServiceError", |
| 29 | + { |
| 30 | + operation: Schema.String, |
| 31 | + detail: Schema.String, |
| 32 | + cause: Schema.optional(Schema.Defect), |
| 33 | + }, |
| 34 | +) { |
| 35 | + override get message(): string { |
| 36 | + return `Skill service error (${this.operation}): ${this.detail}`; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * SkillServiceShape - Service API for skill CRUD and search operations. |
| 42 | + */ |
| 43 | +export interface SkillServiceShape { |
| 44 | + /** |
| 45 | + * List all installed skills. |
| 46 | + */ |
| 47 | + readonly list: (input: { |
| 48 | + readonly cwd?: string | undefined; |
| 49 | + }) => Effect.Effect<SkillListResult, SkillServiceError>; |
| 50 | + |
| 51 | + /** |
| 52 | + * Read a skill by name. |
| 53 | + */ |
| 54 | + readonly read: (input: { |
| 55 | + readonly name: string; |
| 56 | + readonly cwd?: string | undefined; |
| 57 | + }) => Effect.Effect<SkillReadResult, SkillServiceError>; |
| 58 | + |
| 59 | + /** |
| 60 | + * Create a new skill with scaffold template. |
| 61 | + */ |
| 62 | + readonly create: (input: { |
| 63 | + readonly name: string; |
| 64 | + readonly description: string; |
| 65 | + readonly scope: "global" | "project"; |
| 66 | + readonly cwd?: string | undefined; |
| 67 | + }) => Effect.Effect<SkillCreateResult, SkillServiceError>; |
| 68 | + |
| 69 | + /** |
| 70 | + * Delete a skill. |
| 71 | + */ |
| 72 | + readonly delete: (input: { |
| 73 | + readonly name: string; |
| 74 | + readonly scope: "global" | "project"; |
| 75 | + readonly cwd?: string | undefined; |
| 76 | + }) => Effect.Effect<void, SkillServiceError>; |
| 77 | + |
| 78 | + /** |
| 79 | + * Search skills by query. |
| 80 | + */ |
| 81 | + readonly search: (input: { |
| 82 | + readonly query: string; |
| 83 | + readonly cwd?: string | undefined; |
| 84 | + }) => Effect.Effect<SkillSearchResult, SkillServiceError>; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * SkillService - Service tag for skill CRUD and search operations. |
| 89 | + */ |
| 90 | +export class SkillService extends ServiceMap.Service<SkillService, SkillServiceShape>()( |
| 91 | + "okcode/skills/SkillService", |
| 92 | +) {} |
| 93 | + |
| 94 | +export const SkillServiceLive = Layer.succeed(SkillService, { |
| 95 | + list: (input) => |
| 96 | + Effect.try({ |
| 97 | + try: () => { |
| 98 | + const entries = listSkills(input.cwd); |
| 99 | + return { |
| 100 | + skills: entries.map((e) => ({ |
| 101 | + name: e.name, |
| 102 | + scope: e.scope, |
| 103 | + description: e.description, |
| 104 | + tags: e.tags, |
| 105 | + path: e.path, |
| 106 | + })), |
| 107 | + }; |
| 108 | + }, |
| 109 | + catch: (cause) => |
| 110 | + new SkillServiceError({ |
| 111 | + operation: "list", |
| 112 | + detail: cause instanceof Error ? cause.message : String(cause), |
| 113 | + cause: cause instanceof Error ? cause : undefined, |
| 114 | + }), |
| 115 | + }), |
| 116 | + |
| 117 | + read: (input) => |
| 118 | + Effect.try({ |
| 119 | + try: () => { |
| 120 | + const result = readSkill(input.name, input.cwd); |
| 121 | + if (!result) { |
| 122 | + throw new Error(`Skill "${input.name}" not found`); |
| 123 | + } |
| 124 | + return { |
| 125 | + name: result.name, |
| 126 | + scope: result.scope, |
| 127 | + description: result.description, |
| 128 | + content: result.content.raw, |
| 129 | + path: result.path, |
| 130 | + tags: result.tags, |
| 131 | + }; |
| 132 | + }, |
| 133 | + catch: (cause) => |
| 134 | + new SkillServiceError({ |
| 135 | + operation: "read", |
| 136 | + detail: cause instanceof Error ? cause.message : String(cause), |
| 137 | + cause: cause instanceof Error ? cause : undefined, |
| 138 | + }), |
| 139 | + }), |
| 140 | + |
| 141 | + create: (input) => |
| 142 | + Effect.try({ |
| 143 | + try: () => createSkill(input.name, input.description, input.scope, input.cwd), |
| 144 | + catch: (cause) => |
| 145 | + new SkillServiceError({ |
| 146 | + operation: "create", |
| 147 | + detail: cause instanceof Error ? cause.message : String(cause), |
| 148 | + cause: cause instanceof Error ? cause : undefined, |
| 149 | + }), |
| 150 | + }), |
| 151 | + |
| 152 | + delete: (input) => |
| 153 | + Effect.try({ |
| 154 | + try: () => deleteSkill(input.name, input.scope, input.cwd), |
| 155 | + catch: (cause) => |
| 156 | + new SkillServiceError({ |
| 157 | + operation: "delete", |
| 158 | + detail: cause instanceof Error ? cause.message : String(cause), |
| 159 | + cause: cause instanceof Error ? cause : undefined, |
| 160 | + }), |
| 161 | + }), |
| 162 | + |
| 163 | + search: (input) => |
| 164 | + Effect.try({ |
| 165 | + try: () => { |
| 166 | + const entries = searchSkills(input.query, input.cwd); |
| 167 | + return { |
| 168 | + skills: entries.map((e) => ({ |
| 169 | + name: e.name, |
| 170 | + scope: e.scope, |
| 171 | + description: e.description, |
| 172 | + tags: e.tags, |
| 173 | + path: e.path, |
| 174 | + })), |
| 175 | + }; |
| 176 | + }, |
| 177 | + catch: (cause) => |
| 178 | + new SkillServiceError({ |
| 179 | + operation: "search", |
| 180 | + detail: cause instanceof Error ? cause.message : String(cause), |
| 181 | + cause: cause instanceof Error ? cause : undefined, |
| 182 | + }), |
| 183 | + }), |
| 184 | +} satisfies SkillServiceShape); |
0 commit comments