@richjava/req0 0.8.0 → 0.8.1

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/jev-pack.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { JEV_CATALOG, SECTION_SCORE_LEVELS } from "./gates.js";
2
2
  const JEV_STATE_CLIP = 8_000;
3
- const STD_STATE_CLIP = 4_000;
4
3
  export const STD_DOCUMENT_QUESTIONS = [
5
4
  {
6
5
  id: "std.document.title-names-problem",
@@ -197,6 +196,7 @@ export function emitJevPack(spec, context = {}) {
197
196
  roles: spec.matrix.roles,
198
197
  optionalPresent,
199
198
  contextFiles,
199
+ markdown: clip(context.markdown ?? "", JEV_STATE_CLIP),
200
200
  housePackStandard: packStandard,
201
201
  houseDocumentStandard: documentStandard,
202
202
  housePackPath: context.standards?.pack.path ?? "",
@@ -205,14 +205,11 @@ export function emitJevPack(spec, context = {}) {
205
205
  });
206
206
  const stdState = {
207
207
  title: spec.title,
208
- markdown: clip(context.markdown ?? "", STD_STATE_CLIP),
209
208
  ruleIds: spec.businessRules.map((rule) => rule.id),
210
209
  useCaseIds: spec.useCases.map((useCase) => useCase.id),
211
210
  roles: spec.matrix.roles,
212
211
  optionalPresent,
213
212
  contextFiles,
214
- housePackStandard: clip(context.standards?.pack.text ?? "", STD_STATE_CLIP),
215
- houseDocumentStandard: clip(context.standards?.document.text ?? "", STD_STATE_CLIP),
216
213
  };
217
214
  for (const item of STD_DOCUMENT_QUESTIONS) {
218
215
  questions.push({
package/dist/jev.d.ts CHANGED
@@ -20,4 +20,13 @@ export declare function createMockJevClient(overrides?: {
20
20
  choice?: string;
21
21
  score?: Record<string, number>;
22
22
  }): JevClient;
23
+ /** Jev default is 64k tokens per request / 32k per question. Over this, send the long-context header. */
24
+ export declare const TYPESAFE_LONG_CONTEXT_CHARS = 80000;
25
+ export declare function buildTypeSafeRequest(pack: JevPack): {
26
+ model: string;
27
+ state: Record<string, unknown>;
28
+ questions: Record<string, Record<string, unknown>>;
29
+ };
30
+ export declare function typeSafeRequestHeaders(apiKey: string, bodyLength: number): Record<string, string>;
31
+ export declare function typeSafeErrorMessage(raw: Record<string, unknown>, status: number): string;
23
32
  export declare function createTypeSafeJevClient(apiKey: string, fetchImpl?: typeof fetch): JevClient;
package/dist/jev.js CHANGED
@@ -72,30 +72,100 @@ function noulByPrefix(map, id) {
72
72
  const hit = Object.keys(map).find((key) => id.startsWith(key));
73
73
  return hit ? map[hit] : undefined;
74
74
  }
75
+ /** Jev default is 64k tokens per request / 32k per question. Over this, send the long-context header. */
76
+ export const TYPESAFE_LONG_CONTEXT_CHARS = 80_000;
77
+ const SHARED_STATE_KEYS = ["markdown", "housePackStandard", "houseDocumentStandard"];
78
+ export function buildTypeSafeRequest(pack) {
79
+ const questions = {};
80
+ const items = {};
81
+ const shared = {};
82
+ for (const question of pack.questions) {
83
+ questions[question.id] = {
84
+ type: question.type,
85
+ instructions: question.instructions,
86
+ ...(question.criteria !== undefined ? { criteria: question.criteria } : {}),
87
+ };
88
+ const item = { ...question.state };
89
+ for (const key of SHARED_STATE_KEYS) {
90
+ const value = item[key];
91
+ if (typeof value === "string" && value.length > 0) {
92
+ if (typeof shared[key] !== "string")
93
+ shared[key] = value;
94
+ delete item[key];
95
+ }
96
+ }
97
+ items[question.id] = item;
98
+ }
99
+ return {
100
+ model: JEV_MODEL,
101
+ state: {
102
+ catalog: pack.catalog,
103
+ requirementId: pack.requirementId,
104
+ ...shared,
105
+ items,
106
+ },
107
+ questions,
108
+ };
109
+ }
110
+ export function typeSafeRequestHeaders(apiKey, bodyLength) {
111
+ const headers = {
112
+ Authorization: `Bearer ${apiKey}`,
113
+ "Content-Type": "application/json",
114
+ };
115
+ if (bodyLength >= TYPESAFE_LONG_CONTEXT_CHARS) {
116
+ headers["TypeSafe-Danger-Zone-Long-Context"] = "true";
117
+ }
118
+ return headers;
119
+ }
120
+ export function typeSafeErrorMessage(raw, status) {
121
+ const formatted = formatTypeSafeDetail(raw.detail) ?? formatTypeSafeDetail(raw.error);
122
+ if (!formatted)
123
+ return `TypeSafe System One returned HTTP ${status}.`;
124
+ return formatted.startsWith("TypeSafe ") ? formatted : `TypeSafe System One: ${formatted}`;
125
+ }
126
+ function formatTypeSafeDetail(detail) {
127
+ if (typeof detail === "string" && detail.trim())
128
+ return detail.trim();
129
+ if (Array.isArray(detail)) {
130
+ const parts = detail
131
+ .map((entry) => {
132
+ if (typeof entry === "string")
133
+ return entry.trim();
134
+ if (!entry || typeof entry !== "object")
135
+ return "";
136
+ const row = entry;
137
+ const msg = typeof row.msg === "string" ? row.msg : typeof row.message === "string" ? row.message : "";
138
+ const loc = Array.isArray(row.loc)
139
+ ? row.loc.filter((part) => typeof part === "string" || typeof part === "number").join(".")
140
+ : "";
141
+ if (loc && msg)
142
+ return `${loc}: ${msg}`;
143
+ return msg.trim();
144
+ })
145
+ .filter(Boolean);
146
+ if (parts.length)
147
+ return parts.join("; ");
148
+ }
149
+ if (detail && typeof detail === "object") {
150
+ const body = detail;
151
+ if (typeof body.message === "string" && body.message.trim())
152
+ return body.message.trim();
153
+ if (typeof body.detail === "string" && body.detail.trim())
154
+ return body.detail.trim();
155
+ if (typeof body.error === "string" && body.error.trim())
156
+ return body.error.trim();
157
+ }
158
+ return undefined;
159
+ }
75
160
  export function createTypeSafeJevClient(apiKey, fetchImpl = fetch) {
76
161
  return {
77
162
  async judge(pack) {
78
- const questions = {};
79
- const combinedState = {};
80
- for (const question of pack.questions) {
81
- questions[question.id] = {
82
- type: question.type,
83
- instructions: question.instructions,
84
- ...(question.criteria !== undefined ? { criteria: question.criteria } : {}),
85
- };
86
- combinedState[question.id] = question.state;
87
- }
163
+ const payload = buildTypeSafeRequest(pack);
164
+ const body = JSON.stringify(payload);
88
165
  const response = await fetchImpl(JEV_ENDPOINT, {
89
166
  method: "POST",
90
- headers: {
91
- Authorization: `Bearer ${apiKey}`,
92
- "Content-Type": "application/json",
93
- },
94
- body: JSON.stringify({
95
- model: JEV_MODEL,
96
- state: { catalog: pack.catalog, requirementId: pack.requirementId, items: combinedState },
97
- questions,
98
- }),
167
+ headers: typeSafeRequestHeaders(apiKey, body.length),
168
+ body,
99
169
  });
100
170
  const text = await response.text();
101
171
  let raw = {};
@@ -108,13 +178,7 @@ export function createTypeSafeJevClient(apiKey, fetchImpl = fetch) {
108
178
  }
109
179
  }
110
180
  if (!response.ok) {
111
- const nested = raw.error;
112
- const message = typeof nested === "string"
113
- ? nested
114
- : nested && typeof nested === "object" && typeof nested.message === "string"
115
- ? nested.message
116
- : `TypeSafe System One returned HTTP ${response.status}.`;
117
- throw new JevRequestError(message, response.status);
181
+ throw new JevRequestError(typeSafeErrorMessage(raw, response.status), response.status);
118
182
  }
119
183
  const answers = extractAnswers(raw);
120
184
  return pack.questions.map((question) => normalizeAnswer(question.id, question.type, answers[question.id]));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@richjava/req0",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Requirement operating system: spec packs, a local cockpit, Jev, coding-agent implement, and browser proof.",
5
5
  "license": "MIT",
6
6
  "type": "module",