@vouched-dev/schema 0.1.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.
Files changed (53) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +12 -0
  3. package/dist/agent-id.d.ts +13 -0
  4. package/dist/agent-id.js +29 -0
  5. package/dist/api.d.ts +475 -0
  6. package/dist/api.js +260 -0
  7. package/dist/base64url.d.ts +4 -0
  8. package/dist/base64url.js +43 -0
  9. package/dist/credential.d.ts +85 -0
  10. package/dist/credential.js +67 -0
  11. package/dist/db/agents.d.ts +128 -0
  12. package/dist/db/agents.js +31 -0
  13. package/dist/db/client.d.ts +1443 -0
  14. package/dist/db/client.js +28 -0
  15. package/dist/db/credentials.d.ts +143 -0
  16. package/dist/db/credentials.js +16 -0
  17. package/dist/db/events.d.ts +160 -0
  18. package/dist/db/events.js +23 -0
  19. package/dist/db/feed-items.d.ts +109 -0
  20. package/dist/db/feed-items.js +16 -0
  21. package/dist/db/index.d.ts +43 -0
  22. package/dist/db/index.js +17 -0
  23. package/dist/db/migrate.d.ts +1 -0
  24. package/dist/db/migrate.js +30 -0
  25. package/dist/db/migrator.d.ts +2 -0
  26. package/dist/db/migrator.js +20 -0
  27. package/dist/db/operators.d.ts +109 -0
  28. package/dist/db/operators.js +11 -0
  29. package/dist/db/quota-counters.d.ts +92 -0
  30. package/dist/db/quota-counters.js +11 -0
  31. package/dist/db/ratings.d.ts +160 -0
  32. package/dist/db/ratings.js +27 -0
  33. package/dist/db/scores.d.ts +160 -0
  34. package/dist/db/scores.js +15 -0
  35. package/dist/db/task-outcomes.d.ts +126 -0
  36. package/dist/db/task-outcomes.js +18 -0
  37. package/dist/db/tasks.d.ts +251 -0
  38. package/dist/db/tasks.js +26 -0
  39. package/dist/db/timestamps.d.ts +4 -0
  40. package/dist/db/timestamps.js +24 -0
  41. package/dist/db/url.d.ts +12 -0
  42. package/dist/db/url.js +25 -0
  43. package/dist/dimensions.d.ts +20 -0
  44. package/dist/dimensions.js +11 -0
  45. package/dist/envelope.d.ts +16 -0
  46. package/dist/envelope.js +70 -0
  47. package/dist/events.d.ts +153 -0
  48. package/dist/events.js +96 -0
  49. package/dist/index.d.ts +9 -0
  50. package/dist/index.js +9 -0
  51. package/dist/tasks.d.ts +46 -0
  52. package/dist/tasks.js +41 -0
  53. package/package.json +52 -0
@@ -0,0 +1,153 @@
1
+ import { z } from 'zod';
2
+ export declare const Version: z.ZodString;
3
+ export declare const EventType: z.ZodEnum<{
4
+ incident: "incident";
5
+ "session.end": "session.end";
6
+ "session.start": "session.start";
7
+ "task.claimed": "task.claimed";
8
+ "task.outcome": "task.outcome";
9
+ "task.submitted": "task.submitted";
10
+ "tool.call": "tool.call";
11
+ usage: "usage";
12
+ }>;
13
+ export type EventType = z.infer<typeof EventType>;
14
+ export declare const IncidentKind: z.ZodEnum<{
15
+ flagged: "flagged";
16
+ leak_suspected: "leak_suspected";
17
+ scope: "scope";
18
+ tool_denied: "tool_denied";
19
+ }>;
20
+ export type IncidentKind = z.infer<typeof IncidentKind>;
21
+ export declare const EventPayload: {
22
+ 'session.start': z.ZodObject<{
23
+ session_id: z.ZodString;
24
+ }, z.core.$strict>;
25
+ 'session.end': z.ZodObject<{
26
+ session_id: z.ZodString;
27
+ duration_ms: z.ZodInt;
28
+ }, z.core.$strict>;
29
+ 'tool.call': z.ZodObject<{
30
+ tool: z.ZodString;
31
+ duration_ms: z.ZodInt;
32
+ ok: z.ZodBoolean;
33
+ error_class: z.ZodOptional<z.ZodString>;
34
+ }, z.core.$strict>;
35
+ 'task.claimed': z.ZodObject<{
36
+ task_id: z.ZodUUID;
37
+ task_type: z.ZodString;
38
+ }, z.core.$strict>;
39
+ 'task.submitted': z.ZodObject<{
40
+ task_id: z.ZodUUID;
41
+ task_type: z.ZodString;
42
+ }, z.core.$strict>;
43
+ 'task.outcome': z.ZodObject<{
44
+ task_id: z.ZodUUID;
45
+ outcome: z.ZodEnum<{
46
+ failure: "failure";
47
+ success: "success";
48
+ }>;
49
+ evidence_hash: z.ZodOptional<z.ZodString>;
50
+ }, z.core.$strict>;
51
+ incident: z.ZodObject<{
52
+ kind: z.ZodEnum<{
53
+ flagged: "flagged";
54
+ leak_suspected: "leak_suspected";
55
+ scope: "scope";
56
+ tool_denied: "tool_denied";
57
+ }>;
58
+ detail_hash: z.ZodOptional<z.ZodString>;
59
+ }, z.core.$strict>;
60
+ usage: z.ZodObject<{
61
+ tokens_in: z.ZodInt;
62
+ tokens_out: z.ZodInt;
63
+ latency_ms: z.ZodInt;
64
+ model: z.ZodString;
65
+ }, z.core.$strict>;
66
+ };
67
+ export type EventPayload<T extends EventType> = z.infer<(typeof EventPayload)[T]>;
68
+ export declare const Event: z.ZodDiscriminatedUnion<[z.ZodObject<{
69
+ event_id: z.ZodUUID;
70
+ type: z.ZodLiteral<"session.start">;
71
+ occurred_at: z.ZodISODateTime;
72
+ version: z.ZodString;
73
+ payload: z.ZodObject<{
74
+ session_id: z.ZodString;
75
+ }, z.core.$strict>;
76
+ }, z.core.$strict>, z.ZodObject<{
77
+ event_id: z.ZodUUID;
78
+ type: z.ZodLiteral<"session.end">;
79
+ occurred_at: z.ZodISODateTime;
80
+ version: z.ZodString;
81
+ payload: z.ZodObject<{
82
+ session_id: z.ZodString;
83
+ duration_ms: z.ZodInt;
84
+ }, z.core.$strict>;
85
+ }, z.core.$strict>, z.ZodObject<{
86
+ event_id: z.ZodUUID;
87
+ type: z.ZodLiteral<"tool.call">;
88
+ occurred_at: z.ZodISODateTime;
89
+ version: z.ZodString;
90
+ payload: z.ZodObject<{
91
+ tool: z.ZodString;
92
+ duration_ms: z.ZodInt;
93
+ ok: z.ZodBoolean;
94
+ error_class: z.ZodOptional<z.ZodString>;
95
+ }, z.core.$strict>;
96
+ }, z.core.$strict>, z.ZodObject<{
97
+ event_id: z.ZodUUID;
98
+ type: z.ZodLiteral<"task.claimed">;
99
+ occurred_at: z.ZodISODateTime;
100
+ version: z.ZodString;
101
+ payload: z.ZodObject<{
102
+ task_id: z.ZodUUID;
103
+ task_type: z.ZodString;
104
+ }, z.core.$strict>;
105
+ }, z.core.$strict>, z.ZodObject<{
106
+ event_id: z.ZodUUID;
107
+ type: z.ZodLiteral<"task.submitted">;
108
+ occurred_at: z.ZodISODateTime;
109
+ version: z.ZodString;
110
+ payload: z.ZodObject<{
111
+ task_id: z.ZodUUID;
112
+ task_type: z.ZodString;
113
+ }, z.core.$strict>;
114
+ }, z.core.$strict>, z.ZodObject<{
115
+ event_id: z.ZodUUID;
116
+ type: z.ZodLiteral<"task.outcome">;
117
+ occurred_at: z.ZodISODateTime;
118
+ version: z.ZodString;
119
+ payload: z.ZodObject<{
120
+ task_id: z.ZodUUID;
121
+ outcome: z.ZodEnum<{
122
+ failure: "failure";
123
+ success: "success";
124
+ }>;
125
+ evidence_hash: z.ZodOptional<z.ZodString>;
126
+ }, z.core.$strict>;
127
+ }, z.core.$strict>, z.ZodObject<{
128
+ event_id: z.ZodUUID;
129
+ type: z.ZodLiteral<"incident">;
130
+ occurred_at: z.ZodISODateTime;
131
+ version: z.ZodString;
132
+ payload: z.ZodObject<{
133
+ kind: z.ZodEnum<{
134
+ flagged: "flagged";
135
+ leak_suspected: "leak_suspected";
136
+ scope: "scope";
137
+ tool_denied: "tool_denied";
138
+ }>;
139
+ detail_hash: z.ZodOptional<z.ZodString>;
140
+ }, z.core.$strict>;
141
+ }, z.core.$strict>, z.ZodObject<{
142
+ event_id: z.ZodUUID;
143
+ type: z.ZodLiteral<"usage">;
144
+ occurred_at: z.ZodISODateTime;
145
+ version: z.ZodString;
146
+ payload: z.ZodObject<{
147
+ tokens_in: z.ZodInt;
148
+ tokens_out: z.ZodInt;
149
+ latency_ms: z.ZodInt;
150
+ model: z.ZodString;
151
+ }, z.core.$strict>;
152
+ }, z.core.$strict>], "type">;
153
+ export type Event = z.infer<typeof Event>;
package/dist/events.js ADDED
@@ -0,0 +1,96 @@
1
+ import { z } from 'zod';
2
+ import { TaskType } from './dimensions.js';
3
+ import { Sha256Hex, TaskOutcome } from './tasks.js';
4
+ /*
5
+ * The Zod pattern, shown once. Every schema file in this package follows it.
6
+ *
7
+ * 1. A strict object schema rejects unknown keys, so a payload cannot carry
8
+ * anything the taxonomy does not name.
9
+ *
10
+ * const ToolCall = z.strictObject({ tool: Name, ok: z.boolean() });
11
+ * ToolCall.parse({ tool: 'Bash', ok: true, prompt: 'hi' }); // throws
12
+ *
13
+ * 2. A discriminated union picks the member schema by one literal field, so
14
+ * `type` decides which payload schema runs and errors name the right one.
15
+ *
16
+ * const Event = z.discriminatedUnion('type', [
17
+ * z.strictObject({ type: z.literal('tool.call'), payload: ToolCall }),
18
+ * z.strictObject({ type: z.literal('usage'), payload: Usage }),
19
+ * ]);
20
+ *
21
+ * 3. z.infer derives the TypeScript type from the schema. The schema and the
22
+ * type share one name, so `Event` works as a value and as a type.
23
+ *
24
+ * export type Event = z.infer<typeof Event>;
25
+ *
26
+ * Use parse to throw on bad input and safeParse to get { success, data, error }.
27
+ */
28
+ export const Version = z.string().min(1).max(32);
29
+ const Name = z
30
+ .string()
31
+ .min(1)
32
+ .max(64)
33
+ .regex(/^[A-Za-z0-9._:/@-]+$/);
34
+ const Millis = z.int().min(0).max(604_800_000);
35
+ const Count = z.int().min(0).max(100_000_000);
36
+ export const EventType = z.enum([
37
+ 'session.start',
38
+ 'session.end',
39
+ 'tool.call',
40
+ 'task.claimed',
41
+ 'task.submitted',
42
+ 'task.outcome',
43
+ 'incident',
44
+ 'usage',
45
+ ]);
46
+ export const IncidentKind = z.enum([
47
+ 'scope',
48
+ 'tool_denied',
49
+ 'leak_suspected',
50
+ 'flagged',
51
+ ]);
52
+ const TaskRef = z.strictObject({ task_id: z.uuid(), task_type: TaskType });
53
+ export const EventPayload = {
54
+ 'session.start': z.strictObject({ session_id: Name }),
55
+ 'session.end': z.strictObject({ session_id: Name, duration_ms: Millis }),
56
+ 'tool.call': z.strictObject({
57
+ tool: Name,
58
+ duration_ms: Millis,
59
+ ok: z.boolean(),
60
+ error_class: Name.optional(),
61
+ }),
62
+ 'task.claimed': TaskRef,
63
+ 'task.submitted': TaskRef,
64
+ 'task.outcome': z.strictObject({
65
+ task_id: z.uuid(),
66
+ outcome: TaskOutcome,
67
+ evidence_hash: Sha256Hex.optional(),
68
+ }),
69
+ incident: z.strictObject({
70
+ kind: IncidentKind,
71
+ detail_hash: Sha256Hex.optional(),
72
+ }),
73
+ usage: z.strictObject({
74
+ tokens_in: Count,
75
+ tokens_out: Count,
76
+ latency_ms: Millis,
77
+ model: Name,
78
+ }),
79
+ };
80
+ const eventOf = (type) => z.strictObject({
81
+ event_id: z.uuid(),
82
+ type: z.literal(type),
83
+ occurred_at: z.iso.datetime(),
84
+ version: Version,
85
+ payload: EventPayload[type],
86
+ });
87
+ export const Event = z.discriminatedUnion('type', [
88
+ eventOf('session.start'),
89
+ eventOf('session.end'),
90
+ eventOf('tool.call'),
91
+ eventOf('task.claimed'),
92
+ eventOf('task.submitted'),
93
+ eventOf('task.outcome'),
94
+ eventOf('incident'),
95
+ eventOf('usage'),
96
+ ]);
@@ -0,0 +1,9 @@
1
+ export * from './agent-id.js';
2
+ export * from './api.js';
3
+ export * from './base64url.js';
4
+ export * from './credential.js';
5
+ export * from './dimensions.js';
6
+ export * from './envelope.js';
7
+ export * from './events.js';
8
+ export * from './tasks.js';
9
+ export declare const SCHEMA_VERSION = 1;
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ export * from './agent-id.js';
2
+ export * from './api.js';
3
+ export * from './base64url.js';
4
+ export * from './credential.js';
5
+ export * from './dimensions.js';
6
+ export * from './envelope.js';
7
+ export * from './events.js';
8
+ export * from './tasks.js';
9
+ export const SCHEMA_VERSION = 1;
@@ -0,0 +1,46 @@
1
+ import { z } from 'zod';
2
+ export declare const Sha256Hex: z.ZodString;
3
+ export declare const JSON_SCHEMA_MAX_BYTES = 16384;
4
+ export declare const HashVerification: z.ZodObject<{
5
+ kind: z.ZodLiteral<"hash">;
6
+ sha256: z.ZodString;
7
+ }, z.core.$strict>;
8
+ export declare const SchemaVerification: z.ZodObject<{
9
+ kind: z.ZodLiteral<"schema">;
10
+ jsonSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
11
+ }, z.core.$strict>;
12
+ export declare const CounterpartyVerification: z.ZodObject<{
13
+ kind: z.ZodLiteral<"counterparty">;
14
+ }, z.core.$strict>;
15
+ export declare const VerificationSpec: z.ZodDiscriminatedUnion<[z.ZodObject<{
16
+ kind: z.ZodLiteral<"hash">;
17
+ sha256: z.ZodString;
18
+ }, z.core.$strict>, z.ZodObject<{
19
+ kind: z.ZodLiteral<"schema">;
20
+ jsonSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
21
+ }, z.core.$strict>, z.ZodObject<{
22
+ kind: z.ZodLiteral<"counterparty">;
23
+ }, z.core.$strict>], "kind">;
24
+ export type VerificationSpec = z.infer<typeof VerificationSpec>;
25
+ export declare const TaskOutcome: z.ZodEnum<{
26
+ failure: "failure";
27
+ success: "success";
28
+ }>;
29
+ export type TaskOutcome = z.infer<typeof TaskOutcome>;
30
+ export declare const TaskState: z.ZodEnum<{
31
+ claimed: "claimed";
32
+ expired: "expired";
33
+ open: "open";
34
+ submitted: "submitted";
35
+ verified: "verified";
36
+ }>;
37
+ export type TaskState = z.infer<typeof TaskState>;
38
+ type Timestamp = Date | string;
39
+ export type TaskTimestamps = {
40
+ claimedAt: Timestamp | null;
41
+ submittedAt: Timestamp | null;
42
+ verifiedAt: Timestamp | null;
43
+ expiresAt: Timestamp;
44
+ };
45
+ export declare function taskState(task: TaskTimestamps, now?: Date): TaskState;
46
+ export {};
package/dist/tasks.js ADDED
@@ -0,0 +1,41 @@
1
+ import { z } from 'zod';
2
+ import { utf8Encode } from './base64url.js';
3
+ export const Sha256Hex = z.string().regex(/^[0-9a-f]{64}$/);
4
+ export const JSON_SCHEMA_MAX_BYTES = 16384;
5
+ export const HashVerification = z.strictObject({
6
+ kind: z.literal('hash'),
7
+ sha256: Sha256Hex,
8
+ });
9
+ export const SchemaVerification = z.strictObject({
10
+ kind: z.literal('schema'),
11
+ jsonSchema: z
12
+ .record(z.string(), z.unknown())
13
+ .refine((schema) => utf8Encode(JSON.stringify(schema)).length <= JSON_SCHEMA_MAX_BYTES, `jsonSchema must be at most ${JSON_SCHEMA_MAX_BYTES} bytes`),
14
+ });
15
+ export const CounterpartyVerification = z.strictObject({
16
+ kind: z.literal('counterparty'),
17
+ });
18
+ export const VerificationSpec = z.discriminatedUnion('kind', [
19
+ HashVerification,
20
+ SchemaVerification,
21
+ CounterpartyVerification,
22
+ ]);
23
+ export const TaskOutcome = z.enum(['success', 'failure']);
24
+ export const TaskState = z.enum([
25
+ 'open',
26
+ 'claimed',
27
+ 'submitted',
28
+ 'verified',
29
+ 'expired',
30
+ ]);
31
+ export function taskState(task, now = new Date()) {
32
+ if (task.verifiedAt !== null)
33
+ return 'verified';
34
+ if (new Date(task.expiresAt).getTime() <= now.getTime())
35
+ return 'expired';
36
+ if (task.submittedAt !== null)
37
+ return 'submitted';
38
+ if (task.claimedAt !== null)
39
+ return 'claimed';
40
+ return 'open';
41
+ }
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@vouched-dev/schema",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "description": "Zod schemas, event taxonomy and signing helpers for Vouched.",
6
+ "license": "Apache-2.0",
7
+ "homepage": "https://vouched.run",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://vouched.run"
11
+ },
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ },
21
+ "./db": {
22
+ "types": "./dist/db/index.d.ts",
23
+ "default": "./dist/db/index.js"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "README.md",
29
+ "LICENSE"
30
+ ],
31
+ "scripts": {
32
+ "build": "tsc -p tsconfig.json",
33
+ "typecheck": "tsc -p tsconfig.json --noEmit",
34
+ "prepublishOnly": "rm -rf dist && pnpm run build",
35
+ "test": "vitest run",
36
+ "db:generate": "drizzle-kit generate",
37
+ "db:migrate": "drizzle-kit migrate",
38
+ "db:migrate:prod": "node dist/db/migrate.js"
39
+ },
40
+ "dependencies": {
41
+ "@noble/ed25519": "^3.2.0",
42
+ "drizzle-orm": "^0.45.3",
43
+ "postgres": "^3.4.9",
44
+ "zod": "^4.6.5"
45
+ },
46
+ "devDependencies": {
47
+ "@types/node": "^24.13.6",
48
+ "drizzle-kit": "^0.31.11",
49
+ "typescript": "^7.0.2",
50
+ "vitest": "^5.0.1"
51
+ }
52
+ }