opencode-swarm-plugin 0.15.0 → 0.16.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.
@@ -0,0 +1,232 @@
1
+ /**
2
+ * Mandate schemas for voting system
3
+ *
4
+ * Agents file and vote on ideas, tips, lore, snippets, and feature requests.
5
+ * High-consensus items become "mandates" that influence future behavior.
6
+ *
7
+ * Vote decay and scoring patterns match learning.ts (90-day half-life).
8
+ */
9
+ import { z } from "zod";
10
+
11
+ // ============================================================================
12
+ // Core Types
13
+ // ============================================================================
14
+
15
+ /**
16
+ * Content types for mandate entries
17
+ */
18
+ export const MandateContentTypeSchema = z.enum([
19
+ "idea",
20
+ "tip",
21
+ "lore",
22
+ "snippet",
23
+ "feature_request",
24
+ ]);
25
+ export type MandateContentType = z.infer<typeof MandateContentTypeSchema>;
26
+
27
+ /**
28
+ * Mandate status lifecycle
29
+ *
30
+ * - candidate: New entry, collecting votes
31
+ * - established: Has some consensus but not enough for mandate status
32
+ * - mandate: High consensus (net_votes >= 5 AND vote_ratio >= 0.7)
33
+ * - rejected: Strong negative consensus or explicitly rejected
34
+ */
35
+ export const MandateStatusSchema = z.enum([
36
+ "candidate",
37
+ "established",
38
+ "mandate",
39
+ "rejected",
40
+ ]);
41
+ export type MandateStatus = z.infer<typeof MandateStatusSchema>;
42
+
43
+ /**
44
+ * Vote type
45
+ */
46
+ export const VoteTypeSchema = z.enum(["upvote", "downvote"]);
47
+ export type VoteType = z.infer<typeof VoteTypeSchema>;
48
+
49
+ // ============================================================================
50
+ // Entry Schema
51
+ // ============================================================================
52
+
53
+ /**
54
+ * A mandate entry represents a proposal from an agent
55
+ *
56
+ * Entries can be ideas, tips, lore, code snippets, or feature requests.
57
+ * Other agents vote on entries to reach consensus.
58
+ */
59
+ export const MandateEntrySchema = z.object({
60
+ /** Unique ID for this entry */
61
+ id: z.string(),
62
+ /** The actual content of the mandate */
63
+ content: z.string().min(1, "Content required"),
64
+ /** Type of content */
65
+ content_type: MandateContentTypeSchema,
66
+ /** Agent that created this entry */
67
+ author_agent: z.string(),
68
+ /** When this entry was created (ISO-8601) */
69
+ created_at: z.string().datetime({ offset: true }),
70
+ /** Current status */
71
+ status: MandateStatusSchema.default("candidate"),
72
+ /** Optional tags for categorization and search */
73
+ tags: z.array(z.string()).default([]),
74
+ /** Optional metadata (e.g., code language for snippets) */
75
+ metadata: z.record(z.string(), z.unknown()).optional(),
76
+ });
77
+ export type MandateEntry = z.infer<typeof MandateEntrySchema>;
78
+
79
+ // ============================================================================
80
+ // Vote Schema
81
+ // ============================================================================
82
+
83
+ /**
84
+ * A vote on a mandate entry
85
+ *
86
+ * Each agent can vote once per entry (upvote or downvote).
87
+ * Votes decay with 90-day half-life matching learning.ts patterns.
88
+ */
89
+ export const VoteSchema = z.object({
90
+ /** Unique ID for this vote */
91
+ id: z.string(),
92
+ /** The mandate entry this vote applies to */
93
+ mandate_id: z.string(),
94
+ /** Agent that cast this vote */
95
+ agent_name: z.string(),
96
+ /** Type of vote */
97
+ vote_type: VoteTypeSchema,
98
+ /** When this vote was cast (ISO-8601) */
99
+ timestamp: z.string().datetime({ offset: true }),
100
+ /** Raw vote weight before decay (default: 1.0) */
101
+ weight: z.number().min(0).max(1).default(1.0),
102
+ });
103
+ export type Vote = z.infer<typeof VoteSchema>;
104
+
105
+ // ============================================================================
106
+ // Score Schema
107
+ // ============================================================================
108
+
109
+ /**
110
+ * Calculated score for a mandate entry
111
+ *
112
+ * Scores are recalculated periodically with decay applied.
113
+ * Uses same decay formula as learning.ts (90-day half-life).
114
+ */
115
+ export const MandateScoreSchema = z.object({
116
+ /** The mandate entry this score applies to */
117
+ mandate_id: z.string(),
118
+ /** Net votes (upvotes - downvotes) with decay applied */
119
+ net_votes: z.number(),
120
+ /** Vote ratio: upvotes / (upvotes + downvotes) */
121
+ vote_ratio: z.number().min(0).max(1),
122
+ /** Final decayed score for ranking */
123
+ decayed_score: z.number(),
124
+ /** When this score was last calculated (ISO-8601) */
125
+ last_calculated: z.string().datetime({ offset: true }),
126
+ /** Raw vote counts (before decay) */
127
+ raw_upvotes: z.number().int().min(0),
128
+ raw_downvotes: z.number().int().min(0),
129
+ /** Decayed vote counts */
130
+ decayed_upvotes: z.number().min(0),
131
+ decayed_downvotes: z.number().min(0),
132
+ });
133
+ export type MandateScore = z.infer<typeof MandateScoreSchema>;
134
+
135
+ // ============================================================================
136
+ // Decay Configuration
137
+ // ============================================================================
138
+
139
+ /**
140
+ * Configuration for mandate decay calculation
141
+ *
142
+ * Matches learning.ts decay patterns.
143
+ */
144
+ export interface MandateDecayConfig {
145
+ /** Half-life for vote decay in days */
146
+ halfLifeDays: number;
147
+ /** Net votes threshold for mandate status */
148
+ mandateNetVotesThreshold: number;
149
+ /** Vote ratio threshold for mandate status */
150
+ mandateVoteRatioThreshold: number;
151
+ /** Net votes threshold for established status */
152
+ establishedNetVotesThreshold: number;
153
+ /** Negative net votes threshold for rejected status */
154
+ rejectedNetVotesThreshold: number;
155
+ }
156
+
157
+ export const DEFAULT_MANDATE_DECAY_CONFIG: MandateDecayConfig = {
158
+ halfLifeDays: 90,
159
+ mandateNetVotesThreshold: 5,
160
+ mandateVoteRatioThreshold: 0.7,
161
+ establishedNetVotesThreshold: 2,
162
+ rejectedNetVotesThreshold: -3,
163
+ };
164
+
165
+ // ============================================================================
166
+ // API Schemas
167
+ // ============================================================================
168
+
169
+ /**
170
+ * Arguments for creating a mandate entry
171
+ */
172
+ export const CreateMandateArgsSchema = z.object({
173
+ content: z.string().min(1, "Content required"),
174
+ content_type: MandateContentTypeSchema,
175
+ tags: z.array(z.string()).default([]),
176
+ metadata: z.record(z.string(), z.unknown()).optional(),
177
+ });
178
+ export type CreateMandateArgs = z.infer<typeof CreateMandateArgsSchema>;
179
+
180
+ /**
181
+ * Arguments for casting a vote
182
+ */
183
+ export const CastVoteArgsSchema = z.object({
184
+ mandate_id: z.string(),
185
+ vote_type: VoteTypeSchema,
186
+ weight: z.number().min(0).max(1).default(1.0),
187
+ });
188
+ export type CastVoteArgs = z.infer<typeof CastVoteArgsSchema>;
189
+
190
+ /**
191
+ * Arguments for querying mandates
192
+ */
193
+ export const QueryMandatesArgsSchema = z.object({
194
+ status: MandateStatusSchema.optional(),
195
+ content_type: MandateContentTypeSchema.optional(),
196
+ tags: z.array(z.string()).optional(),
197
+ author_agent: z.string().optional(),
198
+ limit: z.number().int().positive().default(20),
199
+ min_score: z.number().optional(),
200
+ });
201
+ export type QueryMandatesArgs = z.infer<typeof QueryMandatesArgsSchema>;
202
+
203
+ /**
204
+ * Result of score calculation
205
+ */
206
+ export const ScoreCalculationResultSchema = z.object({
207
+ mandate_id: z.string(),
208
+ previous_status: MandateStatusSchema,
209
+ new_status: MandateStatusSchema,
210
+ score: MandateScoreSchema,
211
+ status_changed: z.boolean(),
212
+ });
213
+ export type ScoreCalculationResult = z.infer<
214
+ typeof ScoreCalculationResultSchema
215
+ >;
216
+
217
+ // ============================================================================
218
+ // Exports
219
+ // ============================================================================
220
+
221
+ export const mandateSchemas = {
222
+ MandateContentTypeSchema,
223
+ MandateStatusSchema,
224
+ VoteTypeSchema,
225
+ MandateEntrySchema,
226
+ VoteSchema,
227
+ MandateScoreSchema,
228
+ CreateMandateArgsSchema,
229
+ CastVoteArgsSchema,
230
+ QueryMandatesArgsSchema,
231
+ ScoreCalculationResultSchema,
232
+ };