@cesarus777/opencode-cgp 0.0.0-stage → 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.
package/dist/rpc.js ADDED
@@ -0,0 +1,173 @@
1
+ // @bun
2
+ // src/rpc.ts
3
+ import { Rpc } from "@opencode/plugin/rpc";
4
+ var count = {
5
+ type: "integer",
6
+ minimum: 0,
7
+ maximum: Number.MAX_SAFE_INTEGER
8
+ };
9
+ var text = { type: "string", minLength: 1, maxLength: 512 };
10
+ var enumeration = (...values) => ({ type: "string", enum: values });
11
+ var object = (properties, required = Object.keys(properties)) => ({
12
+ type: "object",
13
+ properties,
14
+ required,
15
+ additionalProperties: false
16
+ });
17
+ var array = (items, maxItems = 128) => ({
18
+ type: "array",
19
+ items,
20
+ maxItems
21
+ });
22
+ var measurement = object({
23
+ tokens: count,
24
+ at: count,
25
+ model: text,
26
+ requestId: text,
27
+ provenance: enumeration("provider-normalized", "serialized-estimate"),
28
+ confidence: enumeration("reported", "approximate"),
29
+ scope: enumeration("completed-request", "prepared-request")
30
+ }, ["tokens", "at", "model", "provenance", "confidence", "scope"]);
31
+ var row = object({
32
+ sessionId: text,
33
+ revision: count,
34
+ observedAt: count,
35
+ parentId: text,
36
+ agent: text,
37
+ model: text,
38
+ measurement,
39
+ limits: object({ context: count, input: count, output: count }, [
40
+ "context",
41
+ "output"
42
+ ]),
43
+ capacityTokens: count,
44
+ outputReserveTokens: count,
45
+ peakTokens: count,
46
+ policy: object({
47
+ id: text,
48
+ configured: object({
49
+ type: enumeration("tokens", "ratio", "remaining"),
50
+ value: { type: "number", minimum: 0 }
51
+ }),
52
+ triggerTokens: { type: "number" },
53
+ comparison: enumeration(">=", ">"),
54
+ reason: enumeration("missing-usage", "model-changed", "invalid-usage", "stale-usage", "missing-model-budget", "incompatible-model-budget", "threshold-crossed", "below-threshold"),
55
+ crossed: { type: "boolean" }
56
+ }, ["id", "configured", "comparison", "reason", "crossed"]),
57
+ compaction: object({
58
+ phase: enumeration("idle", "requesting", "accepted", "running", "awaiting-measurement", "blocked", "uncertain"),
59
+ count,
60
+ armed: { type: "boolean" },
61
+ blockedReason: enumeration("session-removed", "compaction-failed", "reclaim-unverifiable", "insufficient-reclaim", "completion-event-timeout", "admission-outcome-unknown", "restart-needs-lifecycle-evidence"),
62
+ lastAttempt: count,
63
+ lastCompleted: count,
64
+ cooldownUntil: count
65
+ }, ["phase", "armed"]),
66
+ cumulative: object({
67
+ input: count,
68
+ output: count,
69
+ reasoning: count,
70
+ cache: object({ read: count, write: count })
71
+ }),
72
+ cumulativeScope: enumeration("session", "unknown"),
73
+ lastObservedActive: { type: "boolean" }
74
+ }, [
75
+ "sessionId",
76
+ "revision",
77
+ "observedAt",
78
+ "policy",
79
+ "compaction",
80
+ "cumulativeScope",
81
+ "lastObservedActive"
82
+ ]);
83
+ var batchFields = {
84
+ schemaVersion: { const: 1 },
85
+ epoch: text,
86
+ sequence: count,
87
+ generatedAt: count,
88
+ health: object({
89
+ enabled: { type: "boolean" },
90
+ mode: enumeration("observe", "enforce"),
91
+ enforcement: enumeration("observing", "available", "suspended", "disabled"),
92
+ reasons: array(enumeration("event-stream-gap", "persistence-unavailable", "capacity-exceeded", "model-metadata-unavailable"), 8),
93
+ staleMs: count,
94
+ maxSessions: count
95
+ }),
96
+ upserts: array(row),
97
+ removed: array(object({
98
+ sessionId: text,
99
+ revision: count,
100
+ reason: enumeration("deleted", "evicted")
101
+ }))
102
+ };
103
+ var readSchema = object({
104
+ schemaVersion: { const: 1 },
105
+ sessionIds: { ...array(text), uniqueItems: true }
106
+ });
107
+ var batchSchema = object(batchFields);
108
+ var resultSchema = object({
109
+ ...batchFields,
110
+ unavailable: array(object({ sessionId: text, reason: enumeration("not-monitored") }))
111
+ });
112
+ var monitorRpc = Rpc.define({
113
+ id: "context-guard.monitor.v1",
114
+ methods: { read: { input: readSchema, output: resultSchema } },
115
+ events: { changed: { schema: batchSchema } }
116
+ });
117
+ function record(x) {
118
+ return typeof x === "object" && x !== null && !Array.isArray(x);
119
+ }
120
+ function matches(value, schema) {
121
+ if (!record(schema))
122
+ return false;
123
+ if ("const" in schema && value !== schema.const)
124
+ return false;
125
+ if (Array.isArray(schema.enum) && !schema.enum.includes(value))
126
+ return false;
127
+ if (schema.type === "number" || schema.type === "integer")
128
+ return typeof value === "number" && Number.isFinite(value) && (schema.type !== "integer" || Number.isSafeInteger(value)) && (typeof schema.minimum !== "number" || value >= schema.minimum) && (typeof schema.maximum !== "number" || value <= schema.maximum);
129
+ if (schema.type === "string")
130
+ return typeof value === "string" && value.length > 0 && value.length <= 512;
131
+ if (schema.type === "boolean")
132
+ return typeof value === "boolean";
133
+ if (schema.type === "array")
134
+ return Array.isArray(value) && value.length <= Number(schema.maxItems) && (!schema.uniqueItems || new Set(value).size === value.length) && value.every((v) => matches(v, schema.items));
135
+ if (schema.type === "object") {
136
+ if (!record(value) || !record(schema.properties) || !Array.isArray(schema.required))
137
+ return false;
138
+ const properties = schema.properties;
139
+ return schema.required.every((k) => typeof k === "string" && (k in value)) && Object.keys(value).every((k) => (k in properties) && matches(value[k], properties[k]));
140
+ }
141
+ return "const" in schema;
142
+ }
143
+ function valid(value, schema) {
144
+ try {
145
+ return JSON.stringify(value).length <= 524288 && matches(value, schema);
146
+ } catch {
147
+ return false;
148
+ }
149
+ }
150
+ function isRead(value) {
151
+ return valid(value, readSchema);
152
+ }
153
+ function isBatch(value) {
154
+ return valid(value, batchSchema) && uniqueRows(value);
155
+ }
156
+ function isResult(value) {
157
+ return valid(value, resultSchema) && uniqueRows(value);
158
+ }
159
+ function uniqueRows(value) {
160
+ if (!record(value) || !Array.isArray(value.upserts) || !Array.isArray(value.removed))
161
+ return false;
162
+ const ids = [...value.upserts, ...value.removed].map((v) => record(v) ? v.sessionId : undefined);
163
+ return ids.length <= 128 && new Set(ids).size === ids.length;
164
+ }
165
+ export {
166
+ resultSchema,
167
+ readSchema,
168
+ monitorRpc,
169
+ isResult,
170
+ isRead,
171
+ isBatch,
172
+ batchSchema
173
+ };