@m6d/cortex-server 2.2.0 → 2.4.0
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/contracts/src/runtime/index.ts +99 -4
- package/dist/contracts/src/graph/helpers.d.ts +5 -5
- package/dist/contracts/src/runtime/index.d.ts +175 -4
- package/dist/src/lib/adapters/database/index.d.ts +20 -2
- package/dist/src/lib/adapters/database/mssql/attachments.d.ts +9 -9
- package/dist/src/lib/adapters/database/mssql/index.d.ts +133 -28
- package/dist/src/lib/adapters/database/mssql/llm-requests.d.ts +3 -2
- package/dist/src/lib/adapters/database/mssql/messages.d.ts +6 -5
- package/dist/src/lib/adapters/database/mssql/outbox.d.ts +103 -0
- package/dist/src/lib/adapters/database/mssql/threads.d.ts +21 -14
- package/dist/src/lib/adapters/database/postgres/attachments.d.ts +9 -9
- package/dist/src/lib/adapters/database/postgres/index.d.ts +133 -28
- package/dist/src/lib/adapters/database/postgres/llm-requests.d.ts +3 -2
- package/dist/src/lib/adapters/database/postgres/messages.d.ts +6 -5
- package/dist/src/lib/adapters/database/postgres/outbox.d.ts +103 -0
- package/dist/src/lib/adapters/database/postgres/threads.d.ts +21 -14
- package/dist/src/lib/ai/attachments.d.ts +2 -2
- package/dist/src/lib/ai/cc-runtime.d.ts +0 -1
- package/dist/src/lib/ai/client-tools.d.ts +1 -1
- package/dist/src/lib/auth/middleware.d.ts +2 -2
- package/dist/src/lib/auth/playground.d.ts +14 -0
- package/dist/src/lib/cc/client.d.ts +9 -2
- package/dist/src/lib/cc/config-cache.d.ts +0 -1
- package/dist/src/lib/cc/flusher.d.ts +40 -0
- package/dist/src/lib/cc/sync-events.d.ts +45 -0
- package/dist/src/lib/cc/types.d.ts +1 -1
- package/dist/src/lib/config.d.ts +11 -0
- package/dist/src/lib/db/schema.mssql.d.ts +161 -0
- package/dist/src/lib/db/schema.pg.d.ts +199 -0
- package/dist/src/lib/factory.d.ts +3 -0
- package/dist/src/lib/routes/owned-thread.d.ts +5 -4
- package/package.json +1 -1
- package/src/lib/adapters/database/index.ts +15 -1
- package/src/lib/adapters/database/mssql/index.ts +13 -4
- package/src/lib/adapters/database/mssql/llm-requests.ts +35 -15
- package/src/lib/adapters/database/mssql/messages.ts +81 -30
- package/src/lib/adapters/database/mssql/outbox.ts +71 -0
- package/src/lib/adapters/database/mssql/threads.ts +42 -11
- package/src/lib/adapters/database/postgres/index.ts +13 -4
- package/src/lib/adapters/database/postgres/llm-requests.ts +35 -15
- package/src/lib/adapters/database/postgres/messages.ts +81 -30
- package/src/lib/adapters/database/postgres/outbox.ts +66 -0
- package/src/lib/adapters/database/postgres/threads.ts +39 -11
- package/src/lib/ai/finish-turn.ts +3 -1
- package/src/lib/ai/prompt.ts +3 -1
- package/src/lib/auth/middleware.ts +34 -8
- package/src/lib/auth/playground.ts +72 -0
- package/src/lib/cc/client.ts +21 -1
- package/src/lib/cc/flusher.ts +147 -0
- package/src/lib/cc/format.ts +0 -4
- package/src/lib/cc/sync-events.ts +65 -0
- package/src/lib/cc/types.ts +3 -0
- package/src/lib/config.ts +11 -0
- package/src/lib/db/migrations/mssql/20260829050547_friendly_red_shift/migration.sql +8 -0
- package/src/lib/db/migrations/mssql/20260829050547_friendly_red_shift/snapshot.json +581 -0
- package/src/lib/db/migrations/mssql/20260830022424_easy_nicolaos/migration.sql +5 -0
- package/src/lib/db/migrations/mssql/20260830022424_easy_nicolaos/snapshot.json +614 -0
- package/src/lib/db/migrations/pg/20260829050547_perfect_wildside/migration.sql +7 -0
- package/src/lib/db/migrations/pg/20260829050547_perfect_wildside/snapshot.json +650 -0
- package/src/lib/db/migrations/pg/20260830022424_serious_deadpool/migration.sql +4 -0
- package/src/lib/db/migrations/pg/20260830022424_serious_deadpool/snapshot.json +690 -0
- package/src/lib/db/schema.mssql.ts +32 -1
- package/src/lib/db/schema.pg.ts +24 -0
- package/src/lib/factory.ts +22 -8
- package/src/lib/routes/threads.ts +5 -1
|
@@ -7,10 +7,10 @@ import { z } from "zod";
|
|
|
7
7
|
* payloads the runtime's parser then fills defaults into.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
/**
|
|
10
|
+
/** The `:id` path param, validated on both sides of the wire. */
|
|
11
11
|
export const AGENT_SLUG_PATTERN = /^[a-z0-9][a-z0-9-]{0,62}$/;
|
|
12
12
|
|
|
13
|
-
/**
|
|
13
|
+
/** Tool names become `tools.<name>()` sandbox bindings on the server. */
|
|
14
14
|
export const TOOL_NAME_PATTERN = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
15
15
|
|
|
16
16
|
export const PROMPT_VARIABLES = ["userName", "channel", "locale", "utcTime", "timezone"] as const;
|
|
@@ -90,7 +90,7 @@ export const knowledgeChunkSchema = z.object({
|
|
|
90
90
|
});
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
|
-
*
|
|
93
|
+
* Present on signatures of `embedded` tools — flows
|
|
94
94
|
* the end user completes in an embedded surface inside the chat widget. The
|
|
95
95
|
* tool's endpoint fields act as the *initiate* call (returns the embed URL);
|
|
96
96
|
* the result the page reports back is relayed as-is — verifying it is the
|
|
@@ -132,7 +132,6 @@ export const runtimeAgentConfigSchema = z.object({
|
|
|
132
132
|
agentId: agentSlugSchema,
|
|
133
133
|
systemPrompt: z.string(),
|
|
134
134
|
promptVariables: z.array(z.enum(PROMPT_VARIABLES)),
|
|
135
|
-
catalogBlurb: z.string(),
|
|
136
135
|
defaultLocale: localeSchema,
|
|
137
136
|
metaTools: z.object({
|
|
138
137
|
searchKnowledge: z.boolean(),
|
|
@@ -253,9 +252,105 @@ export const runtimeErrorSchema = z.object({
|
|
|
253
252
|
}),
|
|
254
253
|
});
|
|
255
254
|
|
|
255
|
+
/*
|
|
256
|
+
* Fleet sync: `@m6d/cortex-server` mirrors thread/message/usage activity into
|
|
257
|
+
* cc via `POST /api/runtime/sync` batches. Upserts are idempotent on
|
|
258
|
+
* (serverId, id); an empty batch is the heartbeat.
|
|
259
|
+
*/
|
|
260
|
+
|
|
261
|
+
const tokenCountSchema = z.number().int().nonnegative();
|
|
262
|
+
|
|
263
|
+
/** Mirrors the zero-dep `TokenUsage` shape in `wire/index.ts` (wire must stay import-free). */
|
|
264
|
+
export const tokenUsageSchema = z.object({
|
|
265
|
+
input: z.object({
|
|
266
|
+
noCache: tokenCountSchema,
|
|
267
|
+
cacheRead: tokenCountSchema,
|
|
268
|
+
cacheWrite: tokenCountSchema,
|
|
269
|
+
total: tokenCountSchema,
|
|
270
|
+
}),
|
|
271
|
+
output: z.object({
|
|
272
|
+
reasoning: tokenCountSchema,
|
|
273
|
+
text: tokenCountSchema,
|
|
274
|
+
total: tokenCountSchema,
|
|
275
|
+
}),
|
|
276
|
+
total: tokenCountSchema,
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
export const MESSAGE_ROLES = ["system", "user", "assistant", "tool"] as const;
|
|
280
|
+
|
|
281
|
+
export const syncEventSchema = z.discriminatedUnion("type", [
|
|
282
|
+
z.object({
|
|
283
|
+
type: z.literal("thread.upsert"),
|
|
284
|
+
id: z.uuid(),
|
|
285
|
+
agentSlug: agentSlugSchema,
|
|
286
|
+
userId: z.string().max(255),
|
|
287
|
+
title: z.string().max(256).nullable(),
|
|
288
|
+
isTest: z.boolean(),
|
|
289
|
+
createdAt: z.iso.datetime(),
|
|
290
|
+
updatedAt: z.iso.datetime(),
|
|
291
|
+
}),
|
|
292
|
+
z.object({
|
|
293
|
+
type: z.literal("thread.delete"),
|
|
294
|
+
id: z.uuid(),
|
|
295
|
+
}),
|
|
296
|
+
z.object({
|
|
297
|
+
type: z.literal("message.upsert"),
|
|
298
|
+
id: z.string().max(128),
|
|
299
|
+
threadId: z.uuid(),
|
|
300
|
+
role: z.enum(MESSAGE_ROLES),
|
|
301
|
+
text: z.string().nullable(),
|
|
302
|
+
ordinal: z.number().int(),
|
|
303
|
+
content: z.looseObject({}),
|
|
304
|
+
createdAt: z.iso.datetime(),
|
|
305
|
+
}),
|
|
306
|
+
z.object({
|
|
307
|
+
type: z.literal("usage.record"),
|
|
308
|
+
id: z.uuid(),
|
|
309
|
+
messageId: z.string().max(128),
|
|
310
|
+
threadId: z.uuid(),
|
|
311
|
+
stepNumber: z.number().int(),
|
|
312
|
+
tokenUsage: tokenUsageSchema,
|
|
313
|
+
// Stamped by the event constructor: llm_requests has no timestamp
|
|
314
|
+
// column, but the mirror needs one for time-bucketed usage stats.
|
|
315
|
+
createdAt: z.iso.datetime(),
|
|
316
|
+
}),
|
|
317
|
+
]);
|
|
318
|
+
|
|
319
|
+
/*
|
|
320
|
+
* `epoch` identifies the sending database's generation (a UUIDv7 minted into
|
|
321
|
+
* the server DB's `sync_meta` row): a recreated database gets a new epoch, so
|
|
322
|
+
* cc can tell a legitimately restarted outbox sequence from a stale batch.
|
|
323
|
+
* v7 uuids are time-ordered, so cc also rejects batches from a superseded
|
|
324
|
+
* (lexicographically lower) epoch outright.
|
|
325
|
+
* `seq` is the batch's highest outbox row id; batches are contiguous
|
|
326
|
+
* id-ordered outbox prefixes, so within one epoch cc keeps a per-server
|
|
327
|
+
* high-water mark and discards batches at or below it — a stale in-flight
|
|
328
|
+
* batch (an expired flusher lease, a replay after a lost ack) can never
|
|
329
|
+
* overwrite newer mirrored state. Heartbeats carry no events and nothing to
|
|
330
|
+
* fence, so `seq` exists only on the non-empty branch.
|
|
331
|
+
*/
|
|
332
|
+
export const syncRequestSchema = z.union([
|
|
333
|
+
z.object({
|
|
334
|
+
events: z.array(syncEventSchema).min(1).max(500),
|
|
335
|
+
epoch: z.uuid(),
|
|
336
|
+
seq: z.number().int().positive(),
|
|
337
|
+
}),
|
|
338
|
+
z.object({
|
|
339
|
+
events: z.array(syncEventSchema).length(0),
|
|
340
|
+
epoch: z.uuid(),
|
|
341
|
+
}),
|
|
342
|
+
]);
|
|
343
|
+
|
|
344
|
+
export const syncResponseSchema = z.object({
|
|
345
|
+
accepted: z.number().int(),
|
|
346
|
+
});
|
|
347
|
+
|
|
256
348
|
export type ToolEmbed = z.infer<typeof toolEmbedSchema>;
|
|
257
349
|
export type RuntimeAgentConfig = z.infer<typeof runtimeAgentConfigSchema>;
|
|
258
350
|
export type ResolveRequest = z.input<typeof resolveRequestSchema>;
|
|
259
351
|
export type ResolveResponse = z.infer<typeof resolveResponseSchema>;
|
|
260
352
|
export type SearchRequest = z.input<typeof searchRequestSchema>;
|
|
261
353
|
export type ExecuteRequest = z.input<typeof executeRequestSchema>;
|
|
354
|
+
export type SyncEvent = z.infer<typeof syncEventSchema>;
|
|
355
|
+
export type SyncRequest = z.input<typeof syncRequestSchema>;
|
|
356
|
+
export type SyncResponse = z.infer<typeof syncResponseSchema>;
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { ConceptDef, DomainDef, EndpointDef, EndpointInput, RuleDef, ServiceDef } from "./types";
|
|
7
7
|
export declare function defineConcept(def: Omit<ConceptDef, "__brand">): {
|
|
8
|
-
description: string;
|
|
9
8
|
name: string;
|
|
9
|
+
description: string;
|
|
10
10
|
aliases?: string[] | undefined;
|
|
11
11
|
metadata?: Record<string, unknown> | undefined;
|
|
12
12
|
parentConcept?: ConceptDef | undefined;
|
|
@@ -14,14 +14,14 @@ export declare function defineConcept(def: Omit<ConceptDef, "__brand">): {
|
|
|
14
14
|
__brand: "concept";
|
|
15
15
|
};
|
|
16
16
|
export declare function defineRule(def: Omit<RuleDef, "__brand">): {
|
|
17
|
-
description: string;
|
|
18
17
|
name: string;
|
|
18
|
+
description: string;
|
|
19
19
|
metadata?: Record<string, unknown> | undefined;
|
|
20
20
|
__brand: "rule";
|
|
21
21
|
};
|
|
22
22
|
export declare function defineService(def: Omit<ServiceDef, "__brand">): {
|
|
23
|
-
description: string;
|
|
24
23
|
name: string;
|
|
24
|
+
description: string;
|
|
25
25
|
metadata?: Record<string, unknown> | undefined;
|
|
26
26
|
builtInId: string;
|
|
27
27
|
governedBy?: RuleDef[] | undefined;
|
|
@@ -29,12 +29,12 @@ export declare function defineService(def: Omit<ServiceDef, "__brand">): {
|
|
|
29
29
|
__brand: "service";
|
|
30
30
|
};
|
|
31
31
|
export declare function defineDomain(def: Omit<DomainDef, "__brand">): {
|
|
32
|
-
description: string;
|
|
33
32
|
name: string;
|
|
33
|
+
services?: ServiceDef[] | undefined;
|
|
34
|
+
description: string;
|
|
34
35
|
metadata?: Record<string, unknown> | undefined;
|
|
35
36
|
concepts?: ConceptDef[] | undefined;
|
|
36
37
|
endpoints?: EndpointDef[] | undefined;
|
|
37
|
-
services?: ServiceDef[] | undefined;
|
|
38
38
|
rules?: RuleDef[] | undefined;
|
|
39
39
|
__brand: "domain";
|
|
40
40
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
/**
|
|
2
|
+
/** The `:id` path param, validated on both sides of the wire. */
|
|
3
3
|
export declare const AGENT_SLUG_PATTERN: RegExp;
|
|
4
|
-
/**
|
|
4
|
+
/** Tool names become `tools.<name>()` sandbox bindings on the server. */
|
|
5
5
|
export declare const TOOL_NAME_PATTERN: RegExp;
|
|
6
6
|
export declare const PROMPT_VARIABLES: readonly ["userName", "channel", "locale", "utcTime", "timezone"];
|
|
7
7
|
/**
|
|
@@ -61,7 +61,7 @@ export declare const knowledgeChunkSchema: z.ZodObject<{
|
|
|
61
61
|
}, z.core.$strip>;
|
|
62
62
|
}, z.core.$strip>;
|
|
63
63
|
/**
|
|
64
|
-
*
|
|
64
|
+
* Present on signatures of `embedded` tools — flows
|
|
65
65
|
* the end user completes in an embedded surface inside the chat widget. The
|
|
66
66
|
* tool's endpoint fields act as the *initiate* call (returns the embed URL);
|
|
67
67
|
* the result the page reports back is relayed as-is — verifying it is the
|
|
@@ -109,7 +109,6 @@ export declare const runtimeAgentConfigSchema: z.ZodObject<{
|
|
|
109
109
|
utcTime: "utcTime";
|
|
110
110
|
timezone: "timezone";
|
|
111
111
|
}>>;
|
|
112
|
-
catalogBlurb: z.ZodString;
|
|
113
112
|
defaultLocale: z.ZodEnum<{
|
|
114
113
|
ar: "ar";
|
|
115
114
|
en: "en";
|
|
@@ -348,10 +347,182 @@ export declare const runtimeErrorSchema: z.ZodObject<{
|
|
|
348
347
|
upstreamStatus: z.ZodOptional<z.ZodNumber>;
|
|
349
348
|
}, z.core.$strip>;
|
|
350
349
|
}, z.core.$strip>;
|
|
350
|
+
/** Mirrors the zero-dep `TokenUsage` shape in `wire/index.ts` (wire must stay import-free). */
|
|
351
|
+
export declare const tokenUsageSchema: z.ZodObject<{
|
|
352
|
+
input: z.ZodObject<{
|
|
353
|
+
noCache: z.ZodNumber;
|
|
354
|
+
cacheRead: z.ZodNumber;
|
|
355
|
+
cacheWrite: z.ZodNumber;
|
|
356
|
+
total: z.ZodNumber;
|
|
357
|
+
}, z.core.$strip>;
|
|
358
|
+
output: z.ZodObject<{
|
|
359
|
+
reasoning: z.ZodNumber;
|
|
360
|
+
text: z.ZodNumber;
|
|
361
|
+
total: z.ZodNumber;
|
|
362
|
+
}, z.core.$strip>;
|
|
363
|
+
total: z.ZodNumber;
|
|
364
|
+
}, z.core.$strip>;
|
|
365
|
+
export declare const MESSAGE_ROLES: readonly ["system", "user", "assistant", "tool"];
|
|
366
|
+
export declare const syncEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
367
|
+
type: z.ZodLiteral<"thread.upsert">;
|
|
368
|
+
id: z.ZodUUID;
|
|
369
|
+
agentSlug: z.ZodString;
|
|
370
|
+
userId: z.ZodString;
|
|
371
|
+
title: z.ZodNullable<z.ZodString>;
|
|
372
|
+
isTest: z.ZodBoolean;
|
|
373
|
+
createdAt: z.ZodISODateTime;
|
|
374
|
+
updatedAt: z.ZodISODateTime;
|
|
375
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
376
|
+
type: z.ZodLiteral<"thread.delete">;
|
|
377
|
+
id: z.ZodUUID;
|
|
378
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
379
|
+
type: z.ZodLiteral<"message.upsert">;
|
|
380
|
+
id: z.ZodString;
|
|
381
|
+
threadId: z.ZodUUID;
|
|
382
|
+
role: z.ZodEnum<{
|
|
383
|
+
system: "system";
|
|
384
|
+
user: "user";
|
|
385
|
+
assistant: "assistant";
|
|
386
|
+
tool: "tool";
|
|
387
|
+
}>;
|
|
388
|
+
text: z.ZodNullable<z.ZodString>;
|
|
389
|
+
ordinal: z.ZodNumber;
|
|
390
|
+
content: z.ZodObject<{}, z.core.$loose>;
|
|
391
|
+
createdAt: z.ZodISODateTime;
|
|
392
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
393
|
+
type: z.ZodLiteral<"usage.record">;
|
|
394
|
+
id: z.ZodUUID;
|
|
395
|
+
messageId: z.ZodString;
|
|
396
|
+
threadId: z.ZodUUID;
|
|
397
|
+
stepNumber: z.ZodNumber;
|
|
398
|
+
tokenUsage: z.ZodObject<{
|
|
399
|
+
input: z.ZodObject<{
|
|
400
|
+
noCache: z.ZodNumber;
|
|
401
|
+
cacheRead: z.ZodNumber;
|
|
402
|
+
cacheWrite: z.ZodNumber;
|
|
403
|
+
total: z.ZodNumber;
|
|
404
|
+
}, z.core.$strip>;
|
|
405
|
+
output: z.ZodObject<{
|
|
406
|
+
reasoning: z.ZodNumber;
|
|
407
|
+
text: z.ZodNumber;
|
|
408
|
+
total: z.ZodNumber;
|
|
409
|
+
}, z.core.$strip>;
|
|
410
|
+
total: z.ZodNumber;
|
|
411
|
+
}, z.core.$strip>;
|
|
412
|
+
createdAt: z.ZodISODateTime;
|
|
413
|
+
}, z.core.$strip>], "type">;
|
|
414
|
+
export declare const syncRequestSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
415
|
+
events: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
416
|
+
type: z.ZodLiteral<"thread.upsert">;
|
|
417
|
+
id: z.ZodUUID;
|
|
418
|
+
agentSlug: z.ZodString;
|
|
419
|
+
userId: z.ZodString;
|
|
420
|
+
title: z.ZodNullable<z.ZodString>;
|
|
421
|
+
isTest: z.ZodBoolean;
|
|
422
|
+
createdAt: z.ZodISODateTime;
|
|
423
|
+
updatedAt: z.ZodISODateTime;
|
|
424
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
425
|
+
type: z.ZodLiteral<"thread.delete">;
|
|
426
|
+
id: z.ZodUUID;
|
|
427
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
428
|
+
type: z.ZodLiteral<"message.upsert">;
|
|
429
|
+
id: z.ZodString;
|
|
430
|
+
threadId: z.ZodUUID;
|
|
431
|
+
role: z.ZodEnum<{
|
|
432
|
+
system: "system";
|
|
433
|
+
user: "user";
|
|
434
|
+
assistant: "assistant";
|
|
435
|
+
tool: "tool";
|
|
436
|
+
}>;
|
|
437
|
+
text: z.ZodNullable<z.ZodString>;
|
|
438
|
+
ordinal: z.ZodNumber;
|
|
439
|
+
content: z.ZodObject<{}, z.core.$loose>;
|
|
440
|
+
createdAt: z.ZodISODateTime;
|
|
441
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
442
|
+
type: z.ZodLiteral<"usage.record">;
|
|
443
|
+
id: z.ZodUUID;
|
|
444
|
+
messageId: z.ZodString;
|
|
445
|
+
threadId: z.ZodUUID;
|
|
446
|
+
stepNumber: z.ZodNumber;
|
|
447
|
+
tokenUsage: z.ZodObject<{
|
|
448
|
+
input: z.ZodObject<{
|
|
449
|
+
noCache: z.ZodNumber;
|
|
450
|
+
cacheRead: z.ZodNumber;
|
|
451
|
+
cacheWrite: z.ZodNumber;
|
|
452
|
+
total: z.ZodNumber;
|
|
453
|
+
}, z.core.$strip>;
|
|
454
|
+
output: z.ZodObject<{
|
|
455
|
+
reasoning: z.ZodNumber;
|
|
456
|
+
text: z.ZodNumber;
|
|
457
|
+
total: z.ZodNumber;
|
|
458
|
+
}, z.core.$strip>;
|
|
459
|
+
total: z.ZodNumber;
|
|
460
|
+
}, z.core.$strip>;
|
|
461
|
+
createdAt: z.ZodISODateTime;
|
|
462
|
+
}, z.core.$strip>], "type">>;
|
|
463
|
+
epoch: z.ZodUUID;
|
|
464
|
+
seq: z.ZodNumber;
|
|
465
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
466
|
+
events: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
467
|
+
type: z.ZodLiteral<"thread.upsert">;
|
|
468
|
+
id: z.ZodUUID;
|
|
469
|
+
agentSlug: z.ZodString;
|
|
470
|
+
userId: z.ZodString;
|
|
471
|
+
title: z.ZodNullable<z.ZodString>;
|
|
472
|
+
isTest: z.ZodBoolean;
|
|
473
|
+
createdAt: z.ZodISODateTime;
|
|
474
|
+
updatedAt: z.ZodISODateTime;
|
|
475
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
476
|
+
type: z.ZodLiteral<"thread.delete">;
|
|
477
|
+
id: z.ZodUUID;
|
|
478
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
479
|
+
type: z.ZodLiteral<"message.upsert">;
|
|
480
|
+
id: z.ZodString;
|
|
481
|
+
threadId: z.ZodUUID;
|
|
482
|
+
role: z.ZodEnum<{
|
|
483
|
+
system: "system";
|
|
484
|
+
user: "user";
|
|
485
|
+
assistant: "assistant";
|
|
486
|
+
tool: "tool";
|
|
487
|
+
}>;
|
|
488
|
+
text: z.ZodNullable<z.ZodString>;
|
|
489
|
+
ordinal: z.ZodNumber;
|
|
490
|
+
content: z.ZodObject<{}, z.core.$loose>;
|
|
491
|
+
createdAt: z.ZodISODateTime;
|
|
492
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
493
|
+
type: z.ZodLiteral<"usage.record">;
|
|
494
|
+
id: z.ZodUUID;
|
|
495
|
+
messageId: z.ZodString;
|
|
496
|
+
threadId: z.ZodUUID;
|
|
497
|
+
stepNumber: z.ZodNumber;
|
|
498
|
+
tokenUsage: z.ZodObject<{
|
|
499
|
+
input: z.ZodObject<{
|
|
500
|
+
noCache: z.ZodNumber;
|
|
501
|
+
cacheRead: z.ZodNumber;
|
|
502
|
+
cacheWrite: z.ZodNumber;
|
|
503
|
+
total: z.ZodNumber;
|
|
504
|
+
}, z.core.$strip>;
|
|
505
|
+
output: z.ZodObject<{
|
|
506
|
+
reasoning: z.ZodNumber;
|
|
507
|
+
text: z.ZodNumber;
|
|
508
|
+
total: z.ZodNumber;
|
|
509
|
+
}, z.core.$strip>;
|
|
510
|
+
total: z.ZodNumber;
|
|
511
|
+
}, z.core.$strip>;
|
|
512
|
+
createdAt: z.ZodISODateTime;
|
|
513
|
+
}, z.core.$strip>], "type">>;
|
|
514
|
+
epoch: z.ZodUUID;
|
|
515
|
+
}, z.core.$strip>]>;
|
|
516
|
+
export declare const syncResponseSchema: z.ZodObject<{
|
|
517
|
+
accepted: z.ZodNumber;
|
|
518
|
+
}, z.core.$strip>;
|
|
351
519
|
export type ToolEmbed = z.infer<typeof toolEmbedSchema>;
|
|
352
520
|
export type RuntimeAgentConfig = z.infer<typeof runtimeAgentConfigSchema>;
|
|
353
521
|
export type ResolveRequest = z.input<typeof resolveRequestSchema>;
|
|
354
522
|
export type ResolveResponse = z.infer<typeof resolveResponseSchema>;
|
|
355
523
|
export type SearchRequest = z.input<typeof searchRequestSchema>;
|
|
356
524
|
export type ExecuteRequest = z.input<typeof executeRequestSchema>;
|
|
525
|
+
export type SyncEvent = z.infer<typeof syncEventSchema>;
|
|
526
|
+
export type SyncRequest = z.input<typeof syncRequestSchema>;
|
|
527
|
+
export type SyncResponse = z.infer<typeof syncResponseSchema>;
|
|
357
528
|
export {};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { SyncEvent } from "../../../../contracts/src/runtime";
|
|
1
2
|
import type { Attachment, ChatMessage, NewAttachment, StoredMessage, Thread, TokenUsage } from "../../types";
|
|
2
3
|
import type { ThreadContextMeta } from "../../ai/context/types";
|
|
3
4
|
/**
|
|
@@ -20,7 +21,9 @@ export type DatabaseAdapter = {
|
|
|
20
21
|
threads: {
|
|
21
22
|
list(userId: string, agentId: string): Promise<Thread[]>;
|
|
22
23
|
getById(userId: string, threadId: string): Promise<Thread | null>;
|
|
23
|
-
create(userId: string, agentId: string
|
|
24
|
+
create(userId: string, agentId: string, options?: {
|
|
25
|
+
isTest?: boolean;
|
|
26
|
+
}): Promise<Thread>;
|
|
24
27
|
delete(userId: string, threadId: string): Promise<void>;
|
|
25
28
|
touch(threadId: string): Promise<Thread>;
|
|
26
29
|
updateTitle(threadId: string, title: string): Promise<void>;
|
|
@@ -36,7 +39,7 @@ export type DatabaseAdapter = {
|
|
|
36
39
|
}): Promise<void>;
|
|
37
40
|
};
|
|
38
41
|
llmRequests: {
|
|
39
|
-
insert(requests: {
|
|
42
|
+
insert(threadId: string, requests: {
|
|
40
43
|
messageId: string;
|
|
41
44
|
stepNumber: number;
|
|
42
45
|
prompt: string;
|
|
@@ -58,4 +61,19 @@ export type DatabaseAdapter = {
|
|
|
58
61
|
setDescription(id: string, userId: string, status: Attachment["status"], description?: string): Promise<void>;
|
|
59
62
|
remove(id: string, userId: string): Promise<Attachment | undefined>;
|
|
60
63
|
};
|
|
64
|
+
/**
|
|
65
|
+
* The cc sync queue. Write sites enqueue through the dialect's
|
|
66
|
+
* `enqueueSyncEvents` inside their own transaction; the flusher drains via
|
|
67
|
+
* `peek` (id ascending = insertion order) and `ack` (delete) here.
|
|
68
|
+
*/
|
|
69
|
+
outbox: {
|
|
70
|
+
enqueue(events: SyncEvent[]): Promise<void>;
|
|
71
|
+
peek(limit: number): Promise<{
|
|
72
|
+
id: number;
|
|
73
|
+
event: SyncEvent;
|
|
74
|
+
}[]>;
|
|
75
|
+
ack(ids: number[]): Promise<void>;
|
|
76
|
+
/** This database generation's id, minted on first use (see `syncMeta`). */
|
|
77
|
+
epoch(): Promise<string>;
|
|
78
|
+
};
|
|
61
79
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { MssqlDb } from "./client";
|
|
2
2
|
export declare function createAttachmentsRepository(db: MssqlDb): {
|
|
3
3
|
createPending(row: {
|
|
4
|
-
userId: string;
|
|
5
4
|
threadId: string;
|
|
5
|
+
userId: string;
|
|
6
6
|
filename: string;
|
|
7
7
|
contentType: string;
|
|
8
8
|
sizeBytes: number;
|
|
@@ -14,11 +14,11 @@ export declare function createAttachmentsRepository(db: MssqlDb): {
|
|
|
14
14
|
messageId?: string | null | undefined;
|
|
15
15
|
description?: string | null | undefined;
|
|
16
16
|
}, limit: number): Promise<{
|
|
17
|
-
|
|
17
|
+
threadId: string;
|
|
18
18
|
userId: string;
|
|
19
|
+
id: string;
|
|
19
20
|
createdAt: Date;
|
|
20
21
|
updatedAt: Date;
|
|
21
|
-
threadId: string;
|
|
22
22
|
messageId: string | null;
|
|
23
23
|
filename: string;
|
|
24
24
|
contentType: string;
|
|
@@ -28,11 +28,11 @@ export declare function createAttachmentsRepository(db: MssqlDb): {
|
|
|
28
28
|
description: string | null;
|
|
29
29
|
} | undefined>;
|
|
30
30
|
getById(id: string, userId: string): Promise<{
|
|
31
|
-
|
|
31
|
+
threadId: string;
|
|
32
32
|
userId: string;
|
|
33
|
+
id: string;
|
|
33
34
|
createdAt: Date;
|
|
34
35
|
updatedAt: Date;
|
|
35
|
-
threadId: string;
|
|
36
36
|
messageId: string | null;
|
|
37
37
|
filename: string;
|
|
38
38
|
contentType: string;
|
|
@@ -69,11 +69,11 @@ export declare function createAttachmentsRepository(db: MssqlDb): {
|
|
|
69
69
|
description: string | null;
|
|
70
70
|
}[]>;
|
|
71
71
|
claimForMessage(threadId: string, userId: string, messageId: string): Promise<{
|
|
72
|
-
|
|
72
|
+
threadId: string;
|
|
73
73
|
userId: string;
|
|
74
|
+
id: string;
|
|
74
75
|
createdAt: Date;
|
|
75
76
|
updatedAt: Date;
|
|
76
|
-
threadId: string;
|
|
77
77
|
messageId: string | null;
|
|
78
78
|
filename: string;
|
|
79
79
|
contentType: string;
|
|
@@ -84,11 +84,11 @@ export declare function createAttachmentsRepository(db: MssqlDb): {
|
|
|
84
84
|
}[]>;
|
|
85
85
|
setDescription(id: string, userId: string, status: "pending" | "described" | "failed" | "skipped", description: string | undefined): Promise<void>;
|
|
86
86
|
remove(id: string, userId: string): Promise<{
|
|
87
|
-
|
|
87
|
+
threadId: string;
|
|
88
88
|
userId: string;
|
|
89
|
+
id: string;
|
|
89
90
|
createdAt: Date;
|
|
90
91
|
updatedAt: Date;
|
|
91
|
-
threadId: string;
|
|
92
92
|
messageId: string | null;
|
|
93
93
|
filename: string;
|
|
94
94
|
contentType: string;
|