@kitsuden/shared 0.0.0-stage → 0.9.0-beta
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/LICENSE +21 -0
- package/README.md +65 -2
- package/dist/admin-registry-contract.d.ts +99 -0
- package/dist/admin-registry-contract.d.ts.map +1 -0
- package/dist/admin-view.d.ts +123 -0
- package/dist/admin-view.d.ts.map +1 -0
- package/dist/agent-permission.d.ts +161 -0
- package/dist/agent-permission.d.ts.map +1 -0
- package/dist/approvals.d.ts +164 -0
- package/dist/approvals.d.ts.map +1 -0
- package/dist/automation.d.ts +175 -0
- package/dist/automation.d.ts.map +1 -0
- package/dist/block-keys.d.ts +82 -0
- package/dist/block-keys.d.ts.map +1 -0
- package/dist/code-scan.d.ts +64 -0
- package/dist/code-scan.d.ts.map +1 -0
- package/dist/comment-subjects.d.ts +129 -0
- package/dist/comment-subjects.d.ts.map +1 -0
- package/dist/connections.d.ts +96 -0
- package/dist/connections.d.ts.map +1 -0
- package/dist/constants.d.ts +37 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/content-publishing.d.ts +34 -0
- package/dist/content-publishing.d.ts.map +1 -0
- package/dist/contracts.d.ts +87 -0
- package/dist/contracts.d.ts.map +1 -0
- package/dist/dataset-catalog.d.ts +91 -0
- package/dist/dataset-catalog.d.ts.map +1 -0
- package/dist/deck-analysis.d.ts +63 -0
- package/dist/deck-analysis.d.ts.map +1 -0
- package/dist/env.d.ts +2 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/errors.d.ts +275 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/field-types.d.ts +304 -0
- package/dist/field-types.d.ts.map +1 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7785 -0
- package/dist/kon-api.d.ts +914 -0
- package/dist/kon-api.d.ts.map +1 -0
- package/dist/kon-entities.d.ts +204 -0
- package/dist/kon-entities.d.ts.map +1 -0
- package/dist/kon-events.d.ts +103 -0
- package/dist/kon-events.d.ts.map +1 -0
- package/dist/kon-package.d.ts +124 -0
- package/dist/kon-package.d.ts.map +1 -0
- package/dist/link-relations.d.ts +73 -0
- package/dist/link-relations.d.ts.map +1 -0
- package/dist/meta-levels.d.ts +81 -0
- package/dist/meta-levels.d.ts.map +1 -0
- package/dist/models-sql.d.ts +110 -0
- package/dist/models-sql.d.ts.map +1 -0
- package/dist/models.d.ts +354 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/operations.d.ts +428 -0
- package/dist/operations.d.ts.map +1 -0
- package/dist/promotion.d.ts +244 -0
- package/dist/promotion.d.ts.map +1 -0
- package/dist/public-site.d.ts +106 -0
- package/dist/public-site.d.ts.map +1 -0
- package/dist/render-blocks.d.ts +150 -0
- package/dist/render-blocks.d.ts.map +1 -0
- package/dist/reports.d.ts +109 -0
- package/dist/reports.d.ts.map +1 -0
- package/dist/rls-builder.d.ts +136 -0
- package/dist/rls-builder.d.ts.map +1 -0
- package/dist/safe-pattern.d.ts +47 -0
- package/dist/safe-pattern.d.ts.map +1 -0
- package/dist/schemas.d.ts +977 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/sensitivity.d.ts +37 -0
- package/dist/sensitivity.d.ts.map +1 -0
- package/dist/signer.d.ts +67 -0
- package/dist/signer.d.ts.map +1 -0
- package/dist/tier-ranges.d.ts +37 -0
- package/dist/tier-ranges.d.ts.map +1 -0
- package/dist/types.d.ts +261 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils.d.ts +71 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/wiki-links.d.ts +56 -0
- package/dist/wiki-links.d.ts.map +1 -0
- package/package.json +53 -3
|
@@ -0,0 +1,914 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Kitsuden — Kon API (Contracts for Self-Contained Kons)
|
|
3
|
+
*
|
|
4
|
+
* This module defines the abstractions that let kons
|
|
5
|
+
* be completely independent of the application core.
|
|
6
|
+
*
|
|
7
|
+
* Kons receive an injected context instead of importing
|
|
8
|
+
* dependencies directly from apps/api/src/
|
|
9
|
+
*
|
|
10
|
+
* ═══════════════════════════════════════════════════════════════════════════
|
|
11
|
+
* INJECTION FLOW:
|
|
12
|
+
* ═══════════════════════════════════════════════════════════════════════════
|
|
13
|
+
*
|
|
14
|
+
* 1. Module Loader (apps/api/src/lib/module-loader.ts)
|
|
15
|
+
* ↓ Creates context with all the dependencies
|
|
16
|
+
* 2. KonContext (interface)
|
|
17
|
+
* ↓ Injected into each kon
|
|
18
|
+
* 3. createKonRouter() (factory)
|
|
19
|
+
* ↓ Returns a configured Hono router
|
|
20
|
+
* 4. Mounted at /api/{kon-id}
|
|
21
|
+
*
|
|
22
|
+
* ═══════════════════════════════════════════════════════════════════════════
|
|
23
|
+
* USAGE EXAMPLE:
|
|
24
|
+
* ═══════════════════════════════════════════════════════════════════════════
|
|
25
|
+
*
|
|
26
|
+
* // In a kon:
|
|
27
|
+
* export const kon = {
|
|
28
|
+
* id: "blog",
|
|
29
|
+
* createRouter: (ctx: KonContext) => {
|
|
30
|
+
* const router = new Hono();
|
|
31
|
+
*
|
|
32
|
+
* router.get("/", async (c) => {
|
|
33
|
+
* const posts = await ctx.db.select().from(blogPosts);
|
|
34
|
+
* return c.json(posts);
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* return router;
|
|
38
|
+
* }
|
|
39
|
+
* };
|
|
40
|
+
*/
|
|
41
|
+
import type { MiddlewareHandler } from "hono";
|
|
42
|
+
import { Hono } from "hono";
|
|
43
|
+
import type { ZodTypeAny } from "zod";
|
|
44
|
+
import type { AutomationHandlerDefinition, KonAutomation } from "./automation";
|
|
45
|
+
import type { KonConnections } from "./connections";
|
|
46
|
+
import type { KonEntities, KonEntityDefinition, KonSecretColumn, KonSecrets } from "./kon-entities";
|
|
47
|
+
import type { KonEventsDeclaration } from "./kon-events";
|
|
48
|
+
import type { KonOperation } from "./operations";
|
|
49
|
+
import type { ReportDefinition } from "./reports";
|
|
50
|
+
/**
|
|
51
|
+
* Cache abstraction for kons
|
|
52
|
+
*/
|
|
53
|
+
export interface KonCache {
|
|
54
|
+
/** Get a value from the cache */
|
|
55
|
+
get<T = any>(key: string): Promise<T | null>;
|
|
56
|
+
/** Store a value in the cache with optional TTL (seconds) */
|
|
57
|
+
set<T = any>(key: string, value: T, ttlSeconds?: number): Promise<void>;
|
|
58
|
+
/** Invalidate cache by pattern (e.g. "blog:*") */
|
|
59
|
+
invalidate(pattern: string): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Invalidate every entry stored with any of these tags (`page:<id>`). Cheaper and more exact than a
|
|
62
|
+
* pattern: the core's cache keeps the keys of each tag. Optional: a host without it falls back to
|
|
63
|
+
* patterns.
|
|
64
|
+
*/
|
|
65
|
+
invalidateTags?: (...tags: string[]) => Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Events abstraction for kons
|
|
69
|
+
*/
|
|
70
|
+
export interface KonEvents {
|
|
71
|
+
/** Emit an event for workers/webhooks */
|
|
72
|
+
emit(event: string, data: unknown): void;
|
|
73
|
+
/** Listen for an event (for internal processing) */
|
|
74
|
+
on(event: string, handler: (data: unknown) => void): void;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Realtime abstraction for kons
|
|
78
|
+
*/
|
|
79
|
+
export interface KonRealtime {
|
|
80
|
+
/** Broadcast a message to all connected clients */
|
|
81
|
+
broadcast(channel: string, message: unknown): void;
|
|
82
|
+
/** Send to a specific user, on every replica */
|
|
83
|
+
sendToUser(userId: string, message: unknown): void;
|
|
84
|
+
/**
|
|
85
|
+
* Declares who may subscribe to this kon's channels, `kon:<id without "kon-">:<rest>`
|
|
86
|
+
* (`kon-koe` → `kon:koe:inbox:team-1`). `authorize` receives `<rest>` and the socket's
|
|
87
|
+
* user (`id: null` when anonymous); it runs on subscribe and again every minute, and a
|
|
88
|
+
* `false` or a throw denies. Without it every channel of the kon is denied, admins
|
|
89
|
+
* included. Optional: a host without realtime, or a test, leaves it undefined.
|
|
90
|
+
*/
|
|
91
|
+
channels?(authorize: (rest: string, user: {
|
|
92
|
+
id: string | null;
|
|
93
|
+
role: string | null;
|
|
94
|
+
}) => boolean | Promise<boolean>): void;
|
|
95
|
+
/**
|
|
96
|
+
* A channel part (64 hex characters) for `material`, keyed by this installation's secret and
|
|
97
|
+
* bound to this kon. Use it for a channel the kon authorizes to whoever names it (a visitor
|
|
98
|
+
* with no account): broadcast on `kon:<short>:<prefix>:<capability>` and give the name only
|
|
99
|
+
* to whoever proves the original secret. Never use as a channel name a value the kon also
|
|
100
|
+
* stores where staff can read it: whoever once read it could listen after losing access.
|
|
101
|
+
* Throws without a secrets key. Optional: older hosts leave it undefined.
|
|
102
|
+
*/
|
|
103
|
+
capability?(material: string): string;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Authentication abstraction for kons
|
|
107
|
+
*/
|
|
108
|
+
export interface KonAuth {
|
|
109
|
+
/** Middleware that requires authentication */
|
|
110
|
+
requireAuth: MiddlewareHandler;
|
|
111
|
+
/** Middleware that requires specific roles */
|
|
112
|
+
requireRole: (roles: string[]) => MiddlewareHandler;
|
|
113
|
+
/** Optional middleware (sets the user if present) */
|
|
114
|
+
optionalAuth: MiddlewareHandler;
|
|
115
|
+
/** Get the current session of the request */
|
|
116
|
+
getSession(headers: Headers): Promise<{
|
|
117
|
+
user: any;
|
|
118
|
+
session: any;
|
|
119
|
+
} | null>;
|
|
120
|
+
/**
|
|
121
|
+
* Who those accounts are: only the PUBLIC part of each, nothing more.
|
|
122
|
+
*
|
|
123
|
+
* A kon cannot read `auth_users`: it belongs to the host, and the isolation
|
|
124
|
+
* rule exists so that a kon does not depend on tables that are not its own.
|
|
125
|
+
* But the blog needs to sign what it publishes, and it only stored the id: the
|
|
126
|
+
* entry appeared with no author across the whole site.
|
|
127
|
+
*
|
|
128
|
+
* That is why it asks instead of reading. It returns `name` and `image` and NEVER
|
|
129
|
+
* the email or the role: a byline needs no more, and what is not returned
|
|
130
|
+
* cannot leak through a public response.
|
|
131
|
+
*
|
|
132
|
+
* Ids that do not exist simply do not appear in the map.
|
|
133
|
+
*/
|
|
134
|
+
usersById(ids: string[]): Promise<Map<string, {
|
|
135
|
+
id: string;
|
|
136
|
+
name: string;
|
|
137
|
+
image: string | null;
|
|
138
|
+
}>>;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Storage abstraction (S3/MinIO) for kons
|
|
142
|
+
*/
|
|
143
|
+
export interface KonStorage {
|
|
144
|
+
/**
|
|
145
|
+
* Store an object. It is the normal upload operation.
|
|
146
|
+
*
|
|
147
|
+
* Before, only the signed-URL ones existed, which let the BROWSER
|
|
148
|
+
* upload straight to the bucket. When the file is already on the server —the case
|
|
149
|
+
* of `POST /api/media/upload`— asking itself for a URL to forward it
|
|
150
|
+
* is an extra round trip, and with local disk storage there is nobody to
|
|
151
|
+
* sign anything for: that was why no upload worked.
|
|
152
|
+
*/
|
|
153
|
+
put(key: string, content: Buffer, contentType: string): Promise<{
|
|
154
|
+
key: string;
|
|
155
|
+
url: string;
|
|
156
|
+
bytes: number;
|
|
157
|
+
}>;
|
|
158
|
+
/** Public URL of an already stored object. Synchronous: it queries nothing. */
|
|
159
|
+
publicUrl(key: string): string;
|
|
160
|
+
/**
|
|
161
|
+
* Signed URL for the client to upload directly to the bucket.
|
|
162
|
+
*
|
|
163
|
+
* It only makes sense with S3/MinIO. With local storage it throws, instead of
|
|
164
|
+
* returning an empty string that would blow up later and farther away.
|
|
165
|
+
*/
|
|
166
|
+
getSignedUploadUrl(key: string, contentType: string, expiresIn?: number): Promise<string>;
|
|
167
|
+
/** Generate a public/signed URL for download */
|
|
168
|
+
getSignedDownloadUrl(key: string, expiresIn?: number): Promise<string>;
|
|
169
|
+
/** Delete an object. It not existing is not an error. */
|
|
170
|
+
delete(key: string): Promise<void>;
|
|
171
|
+
/**
|
|
172
|
+
* Read a whole object; `null` if it does not exist. Works the same with local disk
|
|
173
|
+
* and with S3 (locally, `getSignedDownloadUrl` is a relative path that the
|
|
174
|
+
* server can neither request nor tell whether the file exists).
|
|
175
|
+
*/
|
|
176
|
+
get(key: string): Promise<Buffer | null>;
|
|
177
|
+
/**
|
|
178
|
+
* Keys of all objects that start with `prefix` (empty if there are
|
|
179
|
+
* none). Used by `DELETE /api/media/cache/:id` to delete the stored
|
|
180
|
+
* transformations of an image: it used to call a `list` that
|
|
181
|
+
* no store implemented and always responded 500.
|
|
182
|
+
*/
|
|
183
|
+
list(prefix: string): Promise<string[]>;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Database abstraction for kons
|
|
187
|
+
*
|
|
188
|
+
* NOTE: Instead of abstracting all of Drizzle, we provide
|
|
189
|
+
* the direct instance since kons define their own schemas.
|
|
190
|
+
*/
|
|
191
|
+
/**
|
|
192
|
+
* The Drizzle client a kon receives.
|
|
193
|
+
*
|
|
194
|
+
* WHY IT IS GENERIC
|
|
195
|
+
*
|
|
196
|
+
* `instance: any` is the widest-reaching `any` in the project: ALL kons
|
|
197
|
+
* inherit it and from there typing switches off in cascade — every
|
|
198
|
+
* `db.select()` returns `any` and that `any` propagates to the results.
|
|
199
|
+
*
|
|
200
|
+
* The real Drizzle type cannot be imported here: `@kitsuden/shared` is a
|
|
201
|
+
* publishable package and must not drag in the ORM, and besides pnpm may resolve
|
|
202
|
+
* drizzle-orm into different instances, so the imported type would not be
|
|
203
|
+
* the same as the host's.
|
|
204
|
+
*
|
|
205
|
+
* The way out is a generic parameter with a permissive default. The default
|
|
206
|
+
* keeps existing kons working, and whoever wants real typing
|
|
207
|
+
* gets it back IN FULL by declaring their own type, as kon-conta and
|
|
208
|
+
* kon-newsletter do:
|
|
209
|
+
*
|
|
210
|
+
* ```ts
|
|
211
|
+
* import type { PgDatabase } from "drizzle-orm/pg-core";
|
|
212
|
+
* import * as schema from "./mi.schema";
|
|
213
|
+
* type MiDb = PgDatabase<any, typeof schema, any>;
|
|
214
|
+
*
|
|
215
|
+
* export function createRouter(ctx: KonContext<MiDb>) {
|
|
216
|
+
* const db = ctx.db.instance; // ← fully typed, no casts
|
|
217
|
+
* }
|
|
218
|
+
* ```
|
|
219
|
+
*
|
|
220
|
+
* Migrating a kon is a local change: it does not touch the host or the other kons.
|
|
221
|
+
*/
|
|
222
|
+
export interface KonDatabase<TInstance = any> {
|
|
223
|
+
/**
|
|
224
|
+
* Drizzle instance OF THE REQUEST.
|
|
225
|
+
*
|
|
226
|
+
* Inside a kon route it is the RLS transaction the host opens
|
|
227
|
+
* for each request (`app.current_user_id` / `app.current_user_role`
|
|
228
|
+
* set with `set_config(..., true)`): Postgres applies the policies to everything
|
|
229
|
+
* read or written through it. Outside a request (startup,
|
|
230
|
+
* `onStartup`, background tasks) it is the system connection.
|
|
231
|
+
*
|
|
232
|
+
* `instance.transaction(fn)` inside a route opens a SAVEPOINT of that
|
|
233
|
+
* same transaction: same user, same connection.
|
|
234
|
+
*/
|
|
235
|
+
instance: TInstance;
|
|
236
|
+
/**
|
|
237
|
+
* Runs `fn` as the SYSTEM (no user or role) inside the
|
|
238
|
+
* request's transaction, and restores the context when done.
|
|
239
|
+
*
|
|
240
|
+
* For what a route does on behalf of others and the asker's RLS
|
|
241
|
+
* does not allow —recounting other people's votes, signing up an anonymous user, processing a
|
|
242
|
+
* webhook—, and ONLY after having authorized the action in the application.
|
|
243
|
+
* Outside a request it runs `fn` as is (it is already the system).
|
|
244
|
+
*
|
|
245
|
+
* Optional in the type so as not to break hosts or test doubles that do not
|
|
246
|
+
* provide it; the Kitsuden host always does.
|
|
247
|
+
*/
|
|
248
|
+
asSystem?<T>(fn: () => Promise<T>): Promise<T>;
|
|
249
|
+
/**
|
|
250
|
+
* Runs `fn` OUTSIDE the request's transaction, with the system connection.
|
|
251
|
+
* For work that outlives the response: a `void task()`
|
|
252
|
+
* that is launched and not awaited, a timer, a live session.
|
|
253
|
+
*
|
|
254
|
+
* It is needed because the request's transaction is committed on responding:
|
|
255
|
+
* a query launched inside it and executed afterwards would go to a
|
|
256
|
+
* connection that was already returned to the pool (and may be serving someone
|
|
257
|
+
* else). Inside `fn`, `instance` always resolves to the global `db`.
|
|
258
|
+
*/
|
|
259
|
+
background?<T>(fn: () => T): T;
|
|
260
|
+
/** Helper to run raw SQL */
|
|
261
|
+
execute(sql: string): Promise<unknown>;
|
|
262
|
+
/** Check whether a table exists */
|
|
263
|
+
tableExists(tableName: string): Promise<boolean>;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Content Publishing abstraction for kons
|
|
267
|
+
*
|
|
268
|
+
* Lets data from any source be promoted to Content Targets
|
|
269
|
+
* (Blog, Documentation, Shop, etc.) provided by primary kons.
|
|
270
|
+
*/
|
|
271
|
+
/** Data source for content promotion */
|
|
272
|
+
export type PromotionSource = {
|
|
273
|
+
type: "dataset" | "kon";
|
|
274
|
+
konId?: string;
|
|
275
|
+
table?: string;
|
|
276
|
+
recordId: string;
|
|
277
|
+
data: Record<string, any>;
|
|
278
|
+
};
|
|
279
|
+
/** Content promotion options */
|
|
280
|
+
export interface PromotionOptions {
|
|
281
|
+
title?: string;
|
|
282
|
+
slug?: string;
|
|
283
|
+
autoPublish?: boolean;
|
|
284
|
+
scheduledAt?: Date;
|
|
285
|
+
authorId?: string;
|
|
286
|
+
targetConfig?: Record<string, any>;
|
|
287
|
+
}
|
|
288
|
+
/** Data to promote content to a target */
|
|
289
|
+
export interface PromotionData {
|
|
290
|
+
source: PromotionSource;
|
|
291
|
+
options: PromotionOptions;
|
|
292
|
+
}
|
|
293
|
+
/** Content publishing result */
|
|
294
|
+
export interface PublishResult {
|
|
295
|
+
contentId: string;
|
|
296
|
+
targetId: string;
|
|
297
|
+
slug: string;
|
|
298
|
+
url: string;
|
|
299
|
+
status: string;
|
|
300
|
+
createdAt: Date;
|
|
301
|
+
}
|
|
302
|
+
export interface ContentTargetInfo {
|
|
303
|
+
id: string;
|
|
304
|
+
name: string;
|
|
305
|
+
konId: string;
|
|
306
|
+
capabilities: {
|
|
307
|
+
supportsSeo: boolean;
|
|
308
|
+
supportsScheduling: boolean;
|
|
309
|
+
supportsEditorial: boolean;
|
|
310
|
+
supportsTaxonomy: boolean;
|
|
311
|
+
supportsMedia: boolean;
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
export interface ContentTarget {
|
|
315
|
+
id: string;
|
|
316
|
+
name: string;
|
|
317
|
+
konId: string;
|
|
318
|
+
capabilities: ContentTargetInfo["capabilities"];
|
|
319
|
+
publish(data: PromotionData, ctx: KonContext): Promise<PublishResult>;
|
|
320
|
+
getPublishedUrl(result: PublishResult): string;
|
|
321
|
+
unpublish?(contentId: string, ctx: KonContext): Promise<void>;
|
|
322
|
+
update?(contentId: string, data: Partial<PromotionData>, ctx: KonContext): Promise<PublishResult>;
|
|
323
|
+
getStatus?(contentId: string, ctx: KonContext): Promise<string>;
|
|
324
|
+
}
|
|
325
|
+
export interface KonContentPublishing {
|
|
326
|
+
/** Get all available targets */
|
|
327
|
+
getAvailableTargets(): ContentTargetInfo[];
|
|
328
|
+
/** Check whether a target is available */
|
|
329
|
+
hasTarget(targetId: string): boolean;
|
|
330
|
+
/**
|
|
331
|
+
* Register a Content Target (used by primary kons)
|
|
332
|
+
*/
|
|
333
|
+
register(target: ContentTarget): void;
|
|
334
|
+
/**
|
|
335
|
+
* Unregister a Content Target (used by primary kons)
|
|
336
|
+
*/
|
|
337
|
+
unregister(targetId: string): void;
|
|
338
|
+
/**
|
|
339
|
+
* Promote content to a target
|
|
340
|
+
* @throws Error if the target does not exist
|
|
341
|
+
*/
|
|
342
|
+
promote(targetId: string, data: PromotionData): Promise<PublishResult>;
|
|
343
|
+
/** Unpublish content */
|
|
344
|
+
unpublish(targetId: string, contentId: string): Promise<void>;
|
|
345
|
+
/** Get the public URL of promoted content */
|
|
346
|
+
getPublishedUrl(targetId: string, result: PublishResult): string;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Full context injected into each kon
|
|
350
|
+
*/
|
|
351
|
+
export interface KonContext<TDb = any> {
|
|
352
|
+
/** Database (Drizzle). Generic: `KonContext<MyDb>` gives real types. */
|
|
353
|
+
db: KonDatabase<TDb>;
|
|
354
|
+
/** Cache (Valkey/Redis) */
|
|
355
|
+
cache: KonCache;
|
|
356
|
+
/** Event system */
|
|
357
|
+
events: KonEvents;
|
|
358
|
+
/** Realtime/WebSockets */
|
|
359
|
+
realtime: KonRealtime;
|
|
360
|
+
/** Authentication */
|
|
361
|
+
auth: KonAuth;
|
|
362
|
+
/** File storage */
|
|
363
|
+
storage: KonStorage;
|
|
364
|
+
/** Content Publishing API */
|
|
365
|
+
contentPublishing: KonContentPublishing;
|
|
366
|
+
/** Structured logger */
|
|
367
|
+
logger: {
|
|
368
|
+
info(message: string, meta?: Record<string, unknown>): void;
|
|
369
|
+
warn(message: string, meta?: Record<string, unknown>): void;
|
|
370
|
+
error(message: string, meta?: Record<string, unknown>): void;
|
|
371
|
+
debug(message: string, meta?: Record<string, unknown>): void;
|
|
372
|
+
};
|
|
373
|
+
/** Site configuration */
|
|
374
|
+
config: {
|
|
375
|
+
siteName: string;
|
|
376
|
+
siteUrl: string;
|
|
377
|
+
environment: "development" | "production" | "test";
|
|
378
|
+
};
|
|
379
|
+
/**
|
|
380
|
+
* Settings the active grimoire declares for each kon (`konConfig` in
|
|
381
|
+
* `grimoire.json`), indexed by kon id.
|
|
382
|
+
*
|
|
383
|
+
* It is what lets a grimoire have an OPINION without forking the kon: the
|
|
384
|
+
* same `kon-broadcast` starts configured for fighting games in an FGC
|
|
385
|
+
* grimoire and for something else in another. Without this, `konConfig` was only seen in the
|
|
386
|
+
* admin panel and changed nobody's behavior.
|
|
387
|
+
*
|
|
388
|
+
* Optional on purpose: with no active grimoire, each kon uses its defaults.
|
|
389
|
+
* A kon must NEVER require an entry to exist here.
|
|
390
|
+
*/
|
|
391
|
+
konConfig?: Record<string, Record<string, unknown>>;
|
|
392
|
+
/**
|
|
393
|
+
* The PUBLIC site as configured in the panel (General settings
|
|
394
|
+
* → Site URL), read live.
|
|
395
|
+
*
|
|
396
|
+
* It is NOT `config.siteUrl`: that one is used by kon-newsletter and kon-broadcast as
|
|
397
|
+
* the API origin (`…/api/newsletter/portal.js`, the agent's `--url`), and
|
|
398
|
+
* pointing it at the frontend would break them. This is the address the
|
|
399
|
+
* visitor sees — the one the wikis' subdomains hang from, for example.
|
|
400
|
+
*
|
|
401
|
+
* Optional: a context without it (tests, tooling) must fall back to `SITE_URL`.
|
|
402
|
+
*/
|
|
403
|
+
site?: {
|
|
404
|
+
publicUrl(): Promise<string>;
|
|
405
|
+
};
|
|
406
|
+
/**
|
|
407
|
+
* Runs a message through one of the core's agent identities: its model, its scopes, its allowed tools,
|
|
408
|
+
* human approval and traces, exactly as from the panel. A kon cannot widen any of that: `allowedTools` and
|
|
409
|
+
* `toolset` only NARROW what the agent already has (the intersection), never add to it.
|
|
410
|
+
*
|
|
411
|
+
* Optional: a host without an agent engine, or a test, leaves it undefined, and a kon must say so plainly
|
|
412
|
+
* instead of failing.
|
|
413
|
+
*/
|
|
414
|
+
agents?: {
|
|
415
|
+
run(options: KonAgentRun): Promise<KonAgentResult>;
|
|
416
|
+
};
|
|
417
|
+
/**
|
|
418
|
+
* Embeddings from the provider the core is configured with. `null` when there is no real provider:
|
|
419
|
+
* a kon must not search with made-up vectors.
|
|
420
|
+
*/
|
|
421
|
+
ai?: {
|
|
422
|
+
embed(texts: string[]): Promise<number[][] | null>;
|
|
423
|
+
};
|
|
424
|
+
/**
|
|
425
|
+
* Seals a third party's credential for storage and records every time it is
|
|
426
|
+
* opened (docs/kons/secrets.md). Optional: a host without a secrets key, or a
|
|
427
|
+
* test, leaves it undefined, and a kon must then refuse to store the
|
|
428
|
+
* credential instead of storing it readable.
|
|
429
|
+
*/
|
|
430
|
+
secrets?: KonSecrets;
|
|
431
|
+
/**
|
|
432
|
+
* Accounts at other services, connected once in the core (docs/connections.md): a client that carries
|
|
433
|
+
* the credential to the provider without the kon ever seeing it. Optional: absent on a host without a
|
|
434
|
+
* secrets key or without the connections battery.
|
|
435
|
+
*/
|
|
436
|
+
connections?: KonConnections;
|
|
437
|
+
/**
|
|
438
|
+
* Automation rules the kon keeps for its own configuration, run by the
|
|
439
|
+
* core's rule engine with the steps the kon contributed in
|
|
440
|
+
* `defineKon({ actionHandlers })` (docs/automation.md). Its presence is the
|
|
441
|
+
* capability: a host without it does not run kon steps.
|
|
442
|
+
*/
|
|
443
|
+
automation?: KonAutomation;
|
|
444
|
+
/**
|
|
445
|
+
* Records of the entities the kon declared in `defineKon({ entities })`:
|
|
446
|
+
* datasets the kon owns, with fields and no migration (docs/kons/entities.md).
|
|
447
|
+
* Optional: absent when the datasets battery is not installed.
|
|
448
|
+
*/
|
|
449
|
+
entities?: KonEntities;
|
|
450
|
+
/**
|
|
451
|
+
* The job queue, tied to this kon (docs/kons/queues.md): names live in the kon's namespace
|
|
452
|
+
* (`<short id>:`), a plain name is mapped into it (`handle("sync")` and `add("sync", …)` both use
|
|
453
|
+
* `crm:sync`), and a core queue or another kon's is refused unless the core or that kon allows it.
|
|
454
|
+
* Optional: a host or test without a queue leaves it undefined.
|
|
455
|
+
*/
|
|
456
|
+
queue?: KonQueue;
|
|
457
|
+
/**
|
|
458
|
+
* Present when this core runs `defineKon({ workers })` (docs/kons/workers.md), whatever
|
|
459
|
+
* `KITSUDEN_WORKERS` says in this process: with `off` the workers run in another process, not
|
|
460
|
+
* nowhere. A kon that must also run on an older core keeps its old `setInterval` only when this is
|
|
461
|
+
* absent, so the work never runs twice.
|
|
462
|
+
*/
|
|
463
|
+
workers?: {
|
|
464
|
+
supported: true;
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
/** A job as `ctx.queue` hands it to a handler. */
|
|
468
|
+
export interface KonQueueJob<T = unknown> {
|
|
469
|
+
id: string;
|
|
470
|
+
/** The full name it was queued on (`crm:sync`). */
|
|
471
|
+
queue: string;
|
|
472
|
+
name: string;
|
|
473
|
+
payload: T;
|
|
474
|
+
attempt: number;
|
|
475
|
+
maxAttempts: number;
|
|
476
|
+
/** Who queued it, re-read when it runs; `null` = the system. */
|
|
477
|
+
actor: {
|
|
478
|
+
id: string;
|
|
479
|
+
role: string;
|
|
480
|
+
scopes?: string[];
|
|
481
|
+
} | null;
|
|
482
|
+
requestId: string | null;
|
|
483
|
+
}
|
|
484
|
+
/** `ctx.queue` (docs/kons/queues.md). */
|
|
485
|
+
export interface KonQueue {
|
|
486
|
+
/** Queues a job; inside a request it is written in the request's transaction. */
|
|
487
|
+
add<T = unknown>(queue: string, job: string, payload: T, options?: {
|
|
488
|
+
reason?: string;
|
|
489
|
+
attempts?: number;
|
|
490
|
+
delayMs?: number;
|
|
491
|
+
cost?: number;
|
|
492
|
+
onBudgetExhausted?: "postpone" | "fail";
|
|
493
|
+
dedupeKey?: string;
|
|
494
|
+
}): Promise<{
|
|
495
|
+
id: string;
|
|
496
|
+
deduplicated?: boolean;
|
|
497
|
+
}>;
|
|
498
|
+
/** Registers this kon's handler for one of its queues. */
|
|
499
|
+
handle<T = unknown>(queue: string, fn: (job: KonQueueJob<T>) => Promise<unknown>, options?: {
|
|
500
|
+
concurrency?: number;
|
|
501
|
+
rules?: {
|
|
502
|
+
label: string;
|
|
503
|
+
description: string;
|
|
504
|
+
payload?: Record<string, string>;
|
|
505
|
+
};
|
|
506
|
+
/** Other kons that may queue jobs here: kon ids, short ids or `"*"`. */
|
|
507
|
+
senders?: string[];
|
|
508
|
+
}): void;
|
|
509
|
+
/** The full name a queue of this kon resolves to (`sync` → `crm:sync`), e.g. to tell another kon where to reply. */
|
|
510
|
+
name(queue: string): string;
|
|
511
|
+
}
|
|
512
|
+
export interface KonAgentRun {
|
|
513
|
+
/** The core agent identity that answers. */
|
|
514
|
+
agentId: string;
|
|
515
|
+
message: string;
|
|
516
|
+
/** Continues a conversation the engine already knows. */
|
|
517
|
+
sessionId?: string;
|
|
518
|
+
/** Earlier turns, oldest first, when the kon keeps the conversation itself. */
|
|
519
|
+
history?: Array<{
|
|
520
|
+
role: "user" | "assistant";
|
|
521
|
+
content: string;
|
|
522
|
+
}>;
|
|
523
|
+
/** Extra context for this turn only (what a search found), placed before the message. */
|
|
524
|
+
context?: string;
|
|
525
|
+
/** Only these tools, of those the agent may use. */
|
|
526
|
+
allowedTools?: string[];
|
|
527
|
+
/** Only the tools of these toolsets: `kon:<id>` or `kon:<id>/<set>`, comma-separated. */
|
|
528
|
+
toolset?: string;
|
|
529
|
+
/** Hears the run as it happens: text as it is written, each tool call, its result and any approval it waits for. */
|
|
530
|
+
onEvent?: (event: KonAgentEvent) => void;
|
|
531
|
+
}
|
|
532
|
+
export type KonAgentEvent = {
|
|
533
|
+
type: "step-start";
|
|
534
|
+
} | {
|
|
535
|
+
type: "text-delta";
|
|
536
|
+
delta: string;
|
|
537
|
+
} | {
|
|
538
|
+
type: "tool-call";
|
|
539
|
+
id: string;
|
|
540
|
+
name: string;
|
|
541
|
+
input: Record<string, unknown>;
|
|
542
|
+
} | {
|
|
543
|
+
type: "tool-result";
|
|
544
|
+
id: string;
|
|
545
|
+
name: string;
|
|
546
|
+
ok: boolean;
|
|
547
|
+
output: string;
|
|
548
|
+
pendingApproval?: {
|
|
549
|
+
proposalId: string;
|
|
550
|
+
};
|
|
551
|
+
} | {
|
|
552
|
+
type: "step-finish";
|
|
553
|
+
};
|
|
554
|
+
export interface KonAgentResult {
|
|
555
|
+
response: string;
|
|
556
|
+
sessionId: string;
|
|
557
|
+
traceId: string;
|
|
558
|
+
provider: string;
|
|
559
|
+
model: string;
|
|
560
|
+
tokensIn: number;
|
|
561
|
+
tokensOut: number;
|
|
562
|
+
/** Each tool it called, whether it worked, and the proposal a person must approve when it was held. */
|
|
563
|
+
toolCalls: Array<{
|
|
564
|
+
name: string;
|
|
565
|
+
ok: boolean;
|
|
566
|
+
pendingApproval?: {
|
|
567
|
+
proposalId: string;
|
|
568
|
+
};
|
|
569
|
+
}>;
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Reads a setting the active grimoire declares for a kon.
|
|
573
|
+
*
|
|
574
|
+
* Returns `undefined` if there is no grimoire, if it does not configure that kon or if the
|
|
575
|
+
* value is not of the expected type — the three cases mean the same to whoever
|
|
576
|
+
* calls ("use your default") and telling them apart would only invite treating an absent
|
|
577
|
+
* configuration as an error.
|
|
578
|
+
*
|
|
579
|
+
* The manifest is a JSON that whoever packages the grimoire can edit, so
|
|
580
|
+
* the type is checked at runtime: a `gameProfile: 42` cannot slip through
|
|
581
|
+
* to the point where an id is expected.
|
|
582
|
+
*/
|
|
583
|
+
export declare function konSetting<T extends string | number | boolean>(ctx: Pick<KonContext, "konConfig">, konId: string, key: string, kind?: "string" | "number" | "boolean"): T | undefined;
|
|
584
|
+
/**
|
|
585
|
+
* Type for the router-creating function.
|
|
586
|
+
*
|
|
587
|
+
* The `Env` is left open on purpose, and it is the only justified `any` in this
|
|
588
|
+
* contract. A kon can type its own `Variables` —`new Hono<KonEnv>()`,
|
|
589
|
+
* so that `c.get("user")` has a type instead of being cast— and the host only
|
|
590
|
+
* needs to know that what it receives is a Hono router in order to mount it.
|
|
591
|
+
*
|
|
592
|
+
* Closing it to a parameterless `Hono` would force every kon to cast its router when
|
|
593
|
+
* returning it, which is moving the `any` from a well-typed place to a worse one.
|
|
594
|
+
*/
|
|
595
|
+
export type KonRouterFactory = (ctx: KonContext) => Hono<any>;
|
|
596
|
+
/**
|
|
597
|
+
* Request limit declared by a kon for one of its routes.
|
|
598
|
+
*
|
|
599
|
+
* The path is RELATIVE to the `mountPath`: a kon must not know where it ends up
|
|
600
|
+
* mounted, and this way the limit follows the kon even if it is remounted elsewhere.
|
|
601
|
+
*/
|
|
602
|
+
export interface KonRateLimit {
|
|
603
|
+
/** Sub-path relative to the mountPath. Supports Hono wildcards: "/webhook/*". */
|
|
604
|
+
path: string;
|
|
605
|
+
/** Requests allowed per window. */
|
|
606
|
+
limit: number;
|
|
607
|
+
/** Window in seconds. */
|
|
608
|
+
window: number;
|
|
609
|
+
/** Message when exceeded. */
|
|
610
|
+
message?: string;
|
|
611
|
+
/**
|
|
612
|
+
* Deny if the counter is unavailable (Valkey down).
|
|
613
|
+
*
|
|
614
|
+
* For a route that sends email or moves money, being left without a brake is worse
|
|
615
|
+
* than rejecting the request. Defaults to `false`.
|
|
616
|
+
*/
|
|
617
|
+
failClosed?: boolean;
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Full interface of a self-contained Kon
|
|
621
|
+
*/
|
|
622
|
+
export interface KonDefinition {
|
|
623
|
+
/** Unique kon ID */
|
|
624
|
+
id: string;
|
|
625
|
+
/** Display name */
|
|
626
|
+
name: string;
|
|
627
|
+
/** Semver version */
|
|
628
|
+
version: string;
|
|
629
|
+
/** Description */
|
|
630
|
+
description?: string;
|
|
631
|
+
/** Icon (Lucide name) */
|
|
632
|
+
icon?: string;
|
|
633
|
+
/**
|
|
634
|
+
* It is a primary kon that provides capabilities to the framework.
|
|
635
|
+
* Examples: Blog (provides Content Target), Auth (provides authentication)
|
|
636
|
+
*/
|
|
637
|
+
primary?: boolean;
|
|
638
|
+
/**
|
|
639
|
+
* Can be deactivated by the user.
|
|
640
|
+
* Primary kons are typically optional.
|
|
641
|
+
*/
|
|
642
|
+
optional?: boolean;
|
|
643
|
+
/** Factory that creates the router with injected context */
|
|
644
|
+
createRouter: KonRouterFactory;
|
|
645
|
+
/** Tables it manages (for introspection) */
|
|
646
|
+
tables?: string[];
|
|
647
|
+
/**
|
|
648
|
+
* Request limits for specific routes of this kon.
|
|
649
|
+
*
|
|
650
|
+
* The host applies a generous global limit (designed for reads), but only
|
|
651
|
+
* the kon knows which of its routes are EXPENSIVE: those that send an email,
|
|
652
|
+
* charge money, or trigger a heavy job. Before, that forced touching the host's
|
|
653
|
+
* route map, which breaks isolation — a kon must not appear
|
|
654
|
+
* in the `apps/api` code.
|
|
655
|
+
*
|
|
656
|
+
* They are declared here and the module-loader mounts them when hooking up the router.
|
|
657
|
+
*/
|
|
658
|
+
rateLimits?: KonRateLimit[];
|
|
659
|
+
/**
|
|
660
|
+
* Named groups of this kon's operations, for agents: `{ sales: ["list-deals", "move-deal"] }`. An MCP
|
|
661
|
+
* session or an agent asks for one as `kon:<id>/<name>` (`kon:crm/sales`) and sees only those tools.
|
|
662
|
+
* A model chooses worse among a hundred tools than among ten; only the kon knows which go together.
|
|
663
|
+
* They narrow, never grant: the agent still needs the scopes.
|
|
664
|
+
*/
|
|
665
|
+
mcpToolsets?: Record<string, string[]>;
|
|
666
|
+
/** Lifecycle hooks */
|
|
667
|
+
hooks?: {
|
|
668
|
+
/** Called when the kon is enabled */
|
|
669
|
+
onEnable?: (ctx: KonContext) => Promise<void>;
|
|
670
|
+
/** Called when the kon is disabled */
|
|
671
|
+
onDisable?: (ctx: KonContext) => Promise<void>;
|
|
672
|
+
/** Called on every startup (for migrations) */
|
|
673
|
+
onStartup?: (ctx: KonContext) => Promise<void>;
|
|
674
|
+
};
|
|
675
|
+
/** Admin UI configuration */
|
|
676
|
+
admin?: {
|
|
677
|
+
/**
|
|
678
|
+
* Category in the sidebar.
|
|
679
|
+
*
|
|
680
|
+
* This is the kon's DECLARATION, not the wire format: the registry
|
|
681
|
+
* normalizes it with `mapCategory` before serving it, and `ADMIN_REGISTRY_VERSION`
|
|
682
|
+
* still exposes its usual enum. That is why values can be added
|
|
683
|
+
* here without bumping the contract version — `"tools"` travels as `"data"`
|
|
684
|
+
* until the panel learns to draw its own section.
|
|
685
|
+
*/
|
|
686
|
+
category?: "core" | "content" | "data" | "system" | "esports" | "tools";
|
|
687
|
+
/** Available views */
|
|
688
|
+
views?: Array<{
|
|
689
|
+
id: string;
|
|
690
|
+
/**
|
|
691
|
+
* `element` is the recommended form for custom views: a framework-agnostic
|
|
692
|
+
* Web Component, loaded at runtime from the kon's artifact.
|
|
693
|
+
* `custom` remains for the Svelte legacy, which couples the
|
|
694
|
+
* extension to the admin's internal framework.
|
|
695
|
+
*/
|
|
696
|
+
type: "grid" | "kanban" | "calendar" | "custom" | "element";
|
|
697
|
+
title: string;
|
|
698
|
+
table?: string;
|
|
699
|
+
/** `element` only: name of the custom element. It must contain a hyphen. */
|
|
700
|
+
element?: string;
|
|
701
|
+
/** `element` only: ESM bundle, relative to the root of the kon's package. */
|
|
702
|
+
src?: string;
|
|
703
|
+
}>;
|
|
704
|
+
};
|
|
705
|
+
/** Dependencies */
|
|
706
|
+
dependencies?: {
|
|
707
|
+
/** Required kon IDs */
|
|
708
|
+
kons?: string[];
|
|
709
|
+
/** Required core feature flags */
|
|
710
|
+
core?: string[];
|
|
711
|
+
};
|
|
712
|
+
/** Mount point (default: /api/{id}) */
|
|
713
|
+
mountPath?: string;
|
|
714
|
+
/**
|
|
715
|
+
* OpenAPI specification for auto-generated documentation.
|
|
716
|
+
* When provided, endpoints will be documented at /api/docs/openapi.json
|
|
717
|
+
*/
|
|
718
|
+
openApi?: KonOpenApiSpec;
|
|
719
|
+
/**
|
|
720
|
+
* Kon SDK (see `operations.ts` and docs/kons/README.md): the declared
|
|
721
|
+
* operations. The REST route, the OpenAPI and the MCP tool come from them.
|
|
722
|
+
* A fixed list or a factory that receives the kon's context.
|
|
723
|
+
*/
|
|
724
|
+
operations?: KonOperation<any, any, any>[] | ((ctx: KonContext) => KonOperation<any, any, any>[]);
|
|
725
|
+
/** Version of the SDK contract the kon uses (`KONS_SDK_VERSION`). */
|
|
726
|
+
sdk?: number;
|
|
727
|
+
/**
|
|
728
|
+
* The events this kon emits and the ones it reacts to (docs/kons/events.md).
|
|
729
|
+
* A declaration, not a subscription: it feeds the catalog the panel and
|
|
730
|
+
* agents read to write automation rules. Emitting is still `ctx.events.emit`.
|
|
731
|
+
*/
|
|
732
|
+
events?: KonEventsDeclaration;
|
|
733
|
+
/**
|
|
734
|
+
* Steps this kon contributes to automation rules (docs/automation.md): the
|
|
735
|
+
* things only it knows how to do. Exposed as `<kon id>:<handler id>`.
|
|
736
|
+
*/
|
|
737
|
+
actionHandlers?: AutomationHandlerDefinition[];
|
|
738
|
+
/**
|
|
739
|
+
* Reports this kon ships (docs/reports.md): aggregate queries over its own
|
|
740
|
+
* tables, listed next to the ones people save. Served as `kon:<kon id>/<id>`.
|
|
741
|
+
*/
|
|
742
|
+
reports?: ReportDefinition[];
|
|
743
|
+
/**
|
|
744
|
+
* Entities the kon owns: datasets with fields, created at load with no
|
|
745
|
+
* migration (docs/kons/entities.md). Read and written through `ctx.entities`.
|
|
746
|
+
*/
|
|
747
|
+
entities?: KonEntityDefinition[];
|
|
748
|
+
/**
|
|
749
|
+
* The columns where this kon stores secrets sealed with `ctx.secrets`
|
|
750
|
+
* (docs/kons/secrets.md), so a key rotation re-encrypts them.
|
|
751
|
+
*/
|
|
752
|
+
secretColumns?: KonSecretColumn[];
|
|
753
|
+
/**
|
|
754
|
+
* Work the kon does on a clock (docs/kons/schedules.md): a nightly sync, an hourly cleanup. Each tick runs
|
|
755
|
+
* ONCE across every replica of the API, on the durable job queue.
|
|
756
|
+
*/
|
|
757
|
+
schedules?: KonSchedule[];
|
|
758
|
+
/**
|
|
759
|
+
* Long-running processes the kon needs (docs/kons/workers.md): a socket to another service that
|
|
760
|
+
* must stay open, a poller. Each one runs ONCE across every replica and process of the API (a
|
|
761
|
+
* Postgres advisory lock), restarts on a crash and stops when the kon is switched off.
|
|
762
|
+
*/
|
|
763
|
+
workers?: KonWorker[];
|
|
764
|
+
}
|
|
765
|
+
/** A kon's long-running process (`defineKon({ workers })`). */
|
|
766
|
+
export interface KonWorker {
|
|
767
|
+
/** Unique inside the kon: `gateway` (lower case letters, digits and `-`). Its full name is `<short id>:<id>`. */
|
|
768
|
+
id: string;
|
|
769
|
+
/** What it does, for the panel and agents. */
|
|
770
|
+
description?: string;
|
|
771
|
+
/**
|
|
772
|
+
* The work. It runs as the system (no request, no user) until `worker.signal` aborts, and must
|
|
773
|
+
* return soon after it does. Throwing, or returning while the signal is not aborted, counts as a
|
|
774
|
+
* crash: it is started again after a growing pause. Do the receiving here and the processing in a
|
|
775
|
+
* queue handler (`ctx.queue.add` here, `ctx.queue.handle` in the kon's setup).
|
|
776
|
+
*/
|
|
777
|
+
run(ctx: KonContext, worker: KonWorkerRuntime): Promise<void>;
|
|
778
|
+
}
|
|
779
|
+
/** What a running worker gets besides the kon's context. */
|
|
780
|
+
export interface KonWorkerRuntime {
|
|
781
|
+
/** Aborts on shutdown, when the kon is switched off, or when this process loses the worker's lock. */
|
|
782
|
+
signal: AbortSignal;
|
|
783
|
+
/** The full name: `dc:gateway`. */
|
|
784
|
+
workerId: string;
|
|
785
|
+
/** Restarts so far in this process (0 on the first run). */
|
|
786
|
+
attempt: number;
|
|
787
|
+
}
|
|
788
|
+
/** A kon's periodic task (`defineKon({ schedules })`). */
|
|
789
|
+
export interface KonSchedule {
|
|
790
|
+
/** Unique inside the kon: `sync-partners` (lower case letters, digits and `-`). */
|
|
791
|
+
id: string;
|
|
792
|
+
/** Cron expression: 5 fields (minute first) or 6 (second first). `0 3 * * *` is 03:00 daily. */
|
|
793
|
+
cron: string;
|
|
794
|
+
/** IANA time zone the expression is read in. Default UTC. */
|
|
795
|
+
timezone?: string;
|
|
796
|
+
/** What it does, for the panel and agents. */
|
|
797
|
+
description?: string;
|
|
798
|
+
/**
|
|
799
|
+
* Attempts when `run` throws (1 to 5, default 1). Retries back off on the queue; keep them below the
|
|
800
|
+
* interval, or a retry may meet the next tick.
|
|
801
|
+
*/
|
|
802
|
+
attempts?: number;
|
|
803
|
+
/**
|
|
804
|
+
* The work. `scheduledAt` is the tick's instant, the same on every replica: use it, not `new Date()`,
|
|
805
|
+
* to decide what "since the last run" means. Runs as the system (no request, no user); must be
|
|
806
|
+
* idempotent, as every queued job (a lease that expires can run it again).
|
|
807
|
+
*/
|
|
808
|
+
run(ctx: KonContext, tick: {
|
|
809
|
+
scheduledAt: Date;
|
|
810
|
+
}): Promise<void>;
|
|
811
|
+
}
|
|
812
|
+
/** HTTP methods supported in OpenAPI paths */
|
|
813
|
+
export type OpenApiMethod = "get" | "post" | "put" | "patch" | "delete" | "head" | "options";
|
|
814
|
+
/** OpenAPI path definition for a single endpoint */
|
|
815
|
+
export interface KonOpenApiPath {
|
|
816
|
+
/** HTTP method */
|
|
817
|
+
method: OpenApiMethod;
|
|
818
|
+
/** Path relative to kon mount (e.g., "/posts" or "/posts/:id") */
|
|
819
|
+
path: string;
|
|
820
|
+
/** Short summary (e.g., "List all posts") */
|
|
821
|
+
summary: string;
|
|
822
|
+
/** Detailed description (supports Markdown) */
|
|
823
|
+
description?: string;
|
|
824
|
+
/** Operation ID for code generation (auto-generated if not provided) */
|
|
825
|
+
operationId?: string;
|
|
826
|
+
/** Tags for grouping in documentation */
|
|
827
|
+
tags?: string[];
|
|
828
|
+
/** Path parameters schema */
|
|
829
|
+
params?: ZodTypeAny;
|
|
830
|
+
/** Query parameters schema */
|
|
831
|
+
query?: ZodTypeAny;
|
|
832
|
+
/** Request body schema */
|
|
833
|
+
body?: ZodTypeAny;
|
|
834
|
+
/** Response schemas by status code */
|
|
835
|
+
responses: Record<number, {
|
|
836
|
+
description: string;
|
|
837
|
+
schema: ZodTypeAny;
|
|
838
|
+
}>;
|
|
839
|
+
/** Whether authentication is required */
|
|
840
|
+
requireAuth?: boolean;
|
|
841
|
+
/** Rate limiting configuration */
|
|
842
|
+
rateLimit?: {
|
|
843
|
+
requests: number;
|
|
844
|
+
window: string;
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
/** OpenAPI tag definition */
|
|
848
|
+
export interface KonOpenApiTag {
|
|
849
|
+
/** Tag name */
|
|
850
|
+
name: string;
|
|
851
|
+
/** Tag description */
|
|
852
|
+
description: string;
|
|
853
|
+
/** External documentation URL */
|
|
854
|
+
externalDocs?: {
|
|
855
|
+
description?: string;
|
|
856
|
+
url: string;
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
/** Complete OpenAPI specification for a kon */
|
|
860
|
+
export interface KonOpenApiSpec {
|
|
861
|
+
/** Tags used by this kon's endpoints */
|
|
862
|
+
tags?: KonOpenApiTag[];
|
|
863
|
+
/** Reusable schemas (registered as {konId}_{schemaName}) */
|
|
864
|
+
schemas?: Record<string, ZodTypeAny>;
|
|
865
|
+
/** Endpoint definitions */
|
|
866
|
+
paths: KonOpenApiPath[];
|
|
867
|
+
/** Webhook definitions */
|
|
868
|
+
webhooks?: Record<string, {
|
|
869
|
+
post: {
|
|
870
|
+
summary: string;
|
|
871
|
+
description?: string;
|
|
872
|
+
requestBody: {
|
|
873
|
+
content: {
|
|
874
|
+
"application/json": {
|
|
875
|
+
schema: ZodTypeAny;
|
|
876
|
+
};
|
|
877
|
+
};
|
|
878
|
+
};
|
|
879
|
+
};
|
|
880
|
+
}>;
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Helper to create a kon with type safety
|
|
884
|
+
*/
|
|
885
|
+
export declare function defineKon(def: KonDefinition): KonDefinition;
|
|
886
|
+
/**
|
|
887
|
+
* Helper to create a router with context type inference
|
|
888
|
+
*/
|
|
889
|
+
export declare function createKonRouter(setup: (router: Hono, ctx: KonContext) => void): KonRouterFactory;
|
|
890
|
+
/**
|
|
891
|
+
* Standard error for kons
|
|
892
|
+
*/
|
|
893
|
+
export declare class KonError extends Error {
|
|
894
|
+
code: string;
|
|
895
|
+
statusCode: number;
|
|
896
|
+
details?: Record<string, unknown> | undefined;
|
|
897
|
+
constructor(message: string, code: string, statusCode?: number, details?: Record<string, unknown> | undefined);
|
|
898
|
+
toJSON(): {
|
|
899
|
+
details?: Record<string, unknown> | undefined;
|
|
900
|
+
error: string;
|
|
901
|
+
code: string;
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Predefined common errors
|
|
906
|
+
*/
|
|
907
|
+
export declare const KonErrors: {
|
|
908
|
+
readonly notFound: (resource: string, id?: string) => KonError;
|
|
909
|
+
readonly unauthorized: (message?: string) => KonError;
|
|
910
|
+
readonly forbidden: (message?: string) => KonError;
|
|
911
|
+
readonly validation: (message: string, details?: Record<string, unknown>) => KonError;
|
|
912
|
+
readonly conflict: (message: string) => KonError;
|
|
913
|
+
};
|
|
914
|
+
//# sourceMappingURL=kon-api.d.ts.map
|