cencori 1.8.2 → 1.8.3

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,128 @@
1
+ import { C as CencoriConfig } from '../types-CAhDuNTz.mjs';
2
+
3
+ /**
4
+ * Judgment API — structured decisions from decision models.
5
+ *
6
+ * Send a state plus typed questions (noul / choice / score) and get back
7
+ * probabilities — never generated text. Any `judgment`-type catalog model
8
+ * works here through the same endpoint (TypeSafe's Jev today).
9
+ *
10
+ * @example
11
+ * const result = await cencori.judgment.judge({
12
+ * state: 'I was charged twice. Please refund the duplicate.',
13
+ * questions: {
14
+ * department: {
15
+ * type: 'choice',
16
+ * instructions: 'Which team should handle this?',
17
+ * criteria: { billing: 'Payments, invoicing, refunds', technical: 'Bugs, outages' },
18
+ * },
19
+ * requests_refund: { type: 'noul', instructions: 'Does the customer request a refund?' },
20
+ * },
21
+ * });
22
+ * console.log(result.answers.department.choice); // 'billing'
23
+ *
24
+ * @example
25
+ * const { data: models } = await cencori.judgment.listModels();
26
+ */
27
+
28
+ type JudgmentQuestionType = 'noul' | 'choice' | 'score';
29
+ /** Text, a JSON object, or an array of text values. */
30
+ type JudgmentState = string | Record<string, unknown> | unknown[];
31
+ interface NoulQuestion {
32
+ type: 'noul';
33
+ instructions: string | Record<string, unknown>;
34
+ criteria?: {
35
+ true?: string | Record<string, unknown> | null;
36
+ false?: string | Record<string, unknown> | null;
37
+ } | null;
38
+ }
39
+ interface ChoiceQuestion {
40
+ type: 'choice';
41
+ instructions: string | Record<string, unknown>;
42
+ /** 1–255 options: option name → rubric description (`null` needs none). */
43
+ criteria: Record<string, string | Record<string, unknown> | null>;
44
+ }
45
+ interface ScoreQuestion {
46
+ type: 'score';
47
+ instructions: string | Record<string, unknown>;
48
+ /** 2–10 ordered level descriptions, lowest first. */
49
+ criteria: string[];
50
+ }
51
+ type JudgmentQuestion = NoulQuestion | ChoiceQuestion | ScoreQuestion;
52
+ interface JudgeRequest {
53
+ /** What the questions are about: text, a JSON object, or an array. */
54
+ state: JudgmentState;
55
+ /** Non-empty map of question id → typed question. Answers come back under the same ids. */
56
+ questions: Record<string, JudgmentQuestion>;
57
+ /** Judgment model id. Default: `jev-latest`. */
58
+ model?: string;
59
+ }
60
+ interface NoulAnswer {
61
+ type: 'noul';
62
+ /** Probability the answer is yes, 0–1. */
63
+ noul: number;
64
+ }
65
+ interface ChoiceAnswer {
66
+ type: 'choice';
67
+ choice: string;
68
+ confidence: number;
69
+ probabilities: Record<string, number>;
70
+ }
71
+ interface ScoreAnswer {
72
+ type: 'score';
73
+ score: number;
74
+ confidence: number;
75
+ legend: Record<string, string>;
76
+ probabilities: Record<string, number>;
77
+ }
78
+ type JudgmentAnswer = NoulAnswer | ChoiceAnswer | ScoreAnswer;
79
+ interface JudgmentUsage {
80
+ input_tokens: number;
81
+ output_tokens: number;
82
+ }
83
+ interface JudgeResult {
84
+ object: 'judgment';
85
+ /** The model id that answered (aliases resolve upstream, e.g. `jev-latest` → `jev-1.13.0`). */
86
+ model: string;
87
+ answers: Record<string, JudgmentAnswer>;
88
+ usage: JudgmentUsage;
89
+ uses_byok?: boolean;
90
+ }
91
+ interface JudgmentModelInfo {
92
+ id: string;
93
+ owned_by: string;
94
+ type: 'judgment';
95
+ context_window: number;
96
+ available: boolean;
97
+ unavailable_reason?: string;
98
+ pricing: {
99
+ input_per_million: number | null;
100
+ output_per_million: number | null;
101
+ currency: 'USD';
102
+ input_display: string;
103
+ output_display: string;
104
+ note: string;
105
+ };
106
+ }
107
+ interface JudgmentModelsResponse {
108
+ object: 'list';
109
+ endpoint: string;
110
+ context_tokens: number;
111
+ data: JudgmentModelInfo[];
112
+ question_types: JudgmentQuestionType[];
113
+ default_model: string;
114
+ }
115
+ declare class JudgmentNamespace {
116
+ private config;
117
+ constructor(config: Required<CencoriConfig>);
118
+ /**
119
+ * Judge a state against typed questions. Returns one answer per
120
+ * question id with probabilities your code can branch on.
121
+ */
122
+ judge(request: JudgeRequest): Promise<JudgeResult>;
123
+ /** List the judgment models with their pricing. No API key required. */
124
+ listModels(): Promise<JudgmentModelsResponse>;
125
+ private error;
126
+ }
127
+
128
+ export { type ChoiceAnswer, type ChoiceQuestion, type JudgeRequest, type JudgeResult, type JudgmentAnswer, type JudgmentModelInfo, type JudgmentModelsResponse, JudgmentNamespace, type JudgmentQuestion, type JudgmentQuestionType, type JudgmentState, type JudgmentUsage, type NoulAnswer, type NoulQuestion, type ScoreAnswer, type ScoreQuestion };
@@ -0,0 +1,128 @@
1
+ import { C as CencoriConfig } from '../types-CAhDuNTz.js';
2
+
3
+ /**
4
+ * Judgment API — structured decisions from decision models.
5
+ *
6
+ * Send a state plus typed questions (noul / choice / score) and get back
7
+ * probabilities — never generated text. Any `judgment`-type catalog model
8
+ * works here through the same endpoint (TypeSafe's Jev today).
9
+ *
10
+ * @example
11
+ * const result = await cencori.judgment.judge({
12
+ * state: 'I was charged twice. Please refund the duplicate.',
13
+ * questions: {
14
+ * department: {
15
+ * type: 'choice',
16
+ * instructions: 'Which team should handle this?',
17
+ * criteria: { billing: 'Payments, invoicing, refunds', technical: 'Bugs, outages' },
18
+ * },
19
+ * requests_refund: { type: 'noul', instructions: 'Does the customer request a refund?' },
20
+ * },
21
+ * });
22
+ * console.log(result.answers.department.choice); // 'billing'
23
+ *
24
+ * @example
25
+ * const { data: models } = await cencori.judgment.listModels();
26
+ */
27
+
28
+ type JudgmentQuestionType = 'noul' | 'choice' | 'score';
29
+ /** Text, a JSON object, or an array of text values. */
30
+ type JudgmentState = string | Record<string, unknown> | unknown[];
31
+ interface NoulQuestion {
32
+ type: 'noul';
33
+ instructions: string | Record<string, unknown>;
34
+ criteria?: {
35
+ true?: string | Record<string, unknown> | null;
36
+ false?: string | Record<string, unknown> | null;
37
+ } | null;
38
+ }
39
+ interface ChoiceQuestion {
40
+ type: 'choice';
41
+ instructions: string | Record<string, unknown>;
42
+ /** 1–255 options: option name → rubric description (`null` needs none). */
43
+ criteria: Record<string, string | Record<string, unknown> | null>;
44
+ }
45
+ interface ScoreQuestion {
46
+ type: 'score';
47
+ instructions: string | Record<string, unknown>;
48
+ /** 2–10 ordered level descriptions, lowest first. */
49
+ criteria: string[];
50
+ }
51
+ type JudgmentQuestion = NoulQuestion | ChoiceQuestion | ScoreQuestion;
52
+ interface JudgeRequest {
53
+ /** What the questions are about: text, a JSON object, or an array. */
54
+ state: JudgmentState;
55
+ /** Non-empty map of question id → typed question. Answers come back under the same ids. */
56
+ questions: Record<string, JudgmentQuestion>;
57
+ /** Judgment model id. Default: `jev-latest`. */
58
+ model?: string;
59
+ }
60
+ interface NoulAnswer {
61
+ type: 'noul';
62
+ /** Probability the answer is yes, 0–1. */
63
+ noul: number;
64
+ }
65
+ interface ChoiceAnswer {
66
+ type: 'choice';
67
+ choice: string;
68
+ confidence: number;
69
+ probabilities: Record<string, number>;
70
+ }
71
+ interface ScoreAnswer {
72
+ type: 'score';
73
+ score: number;
74
+ confidence: number;
75
+ legend: Record<string, string>;
76
+ probabilities: Record<string, number>;
77
+ }
78
+ type JudgmentAnswer = NoulAnswer | ChoiceAnswer | ScoreAnswer;
79
+ interface JudgmentUsage {
80
+ input_tokens: number;
81
+ output_tokens: number;
82
+ }
83
+ interface JudgeResult {
84
+ object: 'judgment';
85
+ /** The model id that answered (aliases resolve upstream, e.g. `jev-latest` → `jev-1.13.0`). */
86
+ model: string;
87
+ answers: Record<string, JudgmentAnswer>;
88
+ usage: JudgmentUsage;
89
+ uses_byok?: boolean;
90
+ }
91
+ interface JudgmentModelInfo {
92
+ id: string;
93
+ owned_by: string;
94
+ type: 'judgment';
95
+ context_window: number;
96
+ available: boolean;
97
+ unavailable_reason?: string;
98
+ pricing: {
99
+ input_per_million: number | null;
100
+ output_per_million: number | null;
101
+ currency: 'USD';
102
+ input_display: string;
103
+ output_display: string;
104
+ note: string;
105
+ };
106
+ }
107
+ interface JudgmentModelsResponse {
108
+ object: 'list';
109
+ endpoint: string;
110
+ context_tokens: number;
111
+ data: JudgmentModelInfo[];
112
+ question_types: JudgmentQuestionType[];
113
+ default_model: string;
114
+ }
115
+ declare class JudgmentNamespace {
116
+ private config;
117
+ constructor(config: Required<CencoriConfig>);
118
+ /**
119
+ * Judge a state against typed questions. Returns one answer per
120
+ * question id with probabilities your code can branch on.
121
+ */
122
+ judge(request: JudgeRequest): Promise<JudgeResult>;
123
+ /** List the judgment models with their pricing. No API key required. */
124
+ listModels(): Promise<JudgmentModelsResponse>;
125
+ private error;
126
+ }
127
+
128
+ export { type ChoiceAnswer, type ChoiceQuestion, type JudgeRequest, type JudgeResult, type JudgmentAnswer, type JudgmentModelInfo, type JudgmentModelsResponse, JudgmentNamespace, type JudgmentQuestion, type JudgmentQuestionType, type JudgmentState, type JudgmentUsage, type NoulAnswer, type NoulQuestion, type ScoreAnswer, type ScoreQuestion };
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/judgment/index.ts
21
+ var judgment_exports = {};
22
+ __export(judgment_exports, {
23
+ JudgmentNamespace: () => JudgmentNamespace
24
+ });
25
+ module.exports = __toCommonJS(judgment_exports);
26
+ var JudgmentNamespace = class {
27
+ constructor(config) {
28
+ this.config = config;
29
+ }
30
+ /**
31
+ * Judge a state against typed questions. Returns one answer per
32
+ * question id with probabilities your code can branch on.
33
+ */
34
+ async judge(request) {
35
+ if (request.state === void 0 || request.state === null) {
36
+ throw new Error("judgment.judge requires `state`");
37
+ }
38
+ if (!request.questions || Object.keys(request.questions).length === 0) {
39
+ throw new Error("judgment.judge requires a non-empty `questions` object");
40
+ }
41
+ const response = await fetch(`${this.config.baseUrl}/api/v1/judgments`, {
42
+ method: "POST",
43
+ headers: {
44
+ "CENCORI_API_KEY": this.config.apiKey,
45
+ "Content-Type": "application/json",
46
+ ...this.config.headers
47
+ },
48
+ body: JSON.stringify(request)
49
+ });
50
+ if (!response.ok) {
51
+ throw await this.error(response, "judgment.judge");
52
+ }
53
+ return await response.json();
54
+ }
55
+ /** List the judgment models with their pricing. No API key required. */
56
+ async listModels() {
57
+ const response = await fetch(`${this.config.baseUrl}/api/v1/judgments`, {
58
+ headers: this.config.headers
59
+ });
60
+ if (!response.ok) {
61
+ throw await this.error(response, "judgment.listModels");
62
+ }
63
+ return await response.json();
64
+ }
65
+ async error(response, op) {
66
+ const data = await response.json().catch(() => ({}));
67
+ const message = data && typeof data === "object" && "message" in data && typeof data.message === "string" ? data.message : `${op} failed with status ${response.status}`;
68
+ const code = data && typeof data === "object" && "error" in data && typeof data.error === "string" ? data.error : "request_failed";
69
+ const err = new Error(message);
70
+ err.code = code;
71
+ err.details = data;
72
+ return err;
73
+ }
74
+ };
75
+ // Annotate the CommonJS export names for ESM import in node:
76
+ 0 && (module.exports = {
77
+ JudgmentNamespace
78
+ });
79
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/judgment/index.ts"],"sourcesContent":["/**\n * Judgment API — structured decisions from decision models.\n *\n * Send a state plus typed questions (noul / choice / score) and get back\n * probabilities — never generated text. Any `judgment`-type catalog model\n * works here through the same endpoint (TypeSafe's Jev today).\n *\n * @example\n * const result = await cencori.judgment.judge({\n * state: 'I was charged twice. Please refund the duplicate.',\n * questions: {\n * department: {\n * type: 'choice',\n * instructions: 'Which team should handle this?',\n * criteria: { billing: 'Payments, invoicing, refunds', technical: 'Bugs, outages' },\n * },\n * requests_refund: { type: 'noul', instructions: 'Does the customer request a refund?' },\n * },\n * });\n * console.log(result.answers.department.choice); // 'billing'\n *\n * @example\n * const { data: models } = await cencori.judgment.listModels();\n */\n\nimport type { CencoriConfig } from '../types';\n\nexport type JudgmentQuestionType = 'noul' | 'choice' | 'score';\n\n/** Text, a JSON object, or an array of text values. */\nexport type JudgmentState = string | Record<string, unknown> | unknown[];\n\nexport interface NoulQuestion {\n type: 'noul';\n instructions: string | Record<string, unknown>;\n criteria?: { true?: string | Record<string, unknown> | null; false?: string | Record<string, unknown> | null } | null;\n}\n\nexport interface ChoiceQuestion {\n type: 'choice';\n instructions: string | Record<string, unknown>;\n /** 1–255 options: option name → rubric description (`null` needs none). */\n criteria: Record<string, string | Record<string, unknown> | null>;\n}\n\nexport interface ScoreQuestion {\n type: 'score';\n instructions: string | Record<string, unknown>;\n /** 2–10 ordered level descriptions, lowest first. */\n criteria: string[];\n}\n\nexport type JudgmentQuestion = NoulQuestion | ChoiceQuestion | ScoreQuestion;\n\nexport interface JudgeRequest {\n /** What the questions are about: text, a JSON object, or an array. */\n state: JudgmentState;\n /** Non-empty map of question id → typed question. Answers come back under the same ids. */\n questions: Record<string, JudgmentQuestion>;\n /** Judgment model id. Default: `jev-latest`. */\n model?: string;\n}\n\nexport interface NoulAnswer {\n type: 'noul';\n /** Probability the answer is yes, 0–1. */\n noul: number;\n}\n\nexport interface ChoiceAnswer {\n type: 'choice';\n choice: string;\n confidence: number;\n probabilities: Record<string, number>;\n}\n\nexport interface ScoreAnswer {\n type: 'score';\n score: number;\n confidence: number;\n legend: Record<string, string>;\n probabilities: Record<string, number>;\n}\n\nexport type JudgmentAnswer = NoulAnswer | ChoiceAnswer | ScoreAnswer;\n\nexport interface JudgmentUsage {\n input_tokens: number;\n output_tokens: number;\n}\n\nexport interface JudgeResult {\n object: 'judgment';\n /** The model id that answered (aliases resolve upstream, e.g. `jev-latest` → `jev-1.13.0`). */\n model: string;\n answers: Record<string, JudgmentAnswer>;\n usage: JudgmentUsage;\n uses_byok?: boolean;\n}\n\nexport interface JudgmentModelInfo {\n id: string;\n owned_by: string;\n type: 'judgment';\n context_window: number;\n available: boolean;\n unavailable_reason?: string;\n pricing: {\n input_per_million: number | null;\n output_per_million: number | null;\n currency: 'USD';\n input_display: string;\n output_display: string;\n note: string;\n };\n}\n\nexport interface JudgmentModelsResponse {\n object: 'list';\n endpoint: string;\n context_tokens: number;\n data: JudgmentModelInfo[];\n question_types: JudgmentQuestionType[];\n default_model: string;\n}\n\nexport class JudgmentNamespace {\n private config: Required<CencoriConfig>;\n\n constructor(config: Required<CencoriConfig>) {\n this.config = config;\n }\n\n /**\n * Judge a state against typed questions. Returns one answer per\n * question id with probabilities your code can branch on.\n */\n async judge(request: JudgeRequest): Promise<JudgeResult> {\n if (request.state === undefined || request.state === null) {\n throw new Error('judgment.judge requires `state`');\n }\n if (!request.questions || Object.keys(request.questions).length === 0) {\n throw new Error('judgment.judge requires a non-empty `questions` object');\n }\n const response = await fetch(`${this.config.baseUrl}/api/v1/judgments`, {\n method: 'POST',\n headers: {\n 'CENCORI_API_KEY': this.config.apiKey,\n 'Content-Type': 'application/json',\n ...this.config.headers,\n },\n body: JSON.stringify(request),\n });\n if (!response.ok) {\n throw await this.error(response, 'judgment.judge');\n }\n return (await response.json()) as JudgeResult;\n }\n\n /** List the judgment models with their pricing. No API key required. */\n async listModels(): Promise<JudgmentModelsResponse> {\n const response = await fetch(`${this.config.baseUrl}/api/v1/judgments`, {\n headers: this.config.headers,\n });\n if (!response.ok) {\n throw await this.error(response, 'judgment.listModels');\n }\n return (await response.json()) as JudgmentModelsResponse;\n }\n\n private async error(response: Response, op: string): Promise<Error> {\n const data = await response.json().catch(() => ({}));\n const message =\n (data && typeof data === 'object' && 'message' in data && typeof data.message === 'string')\n ? data.message\n : `${op} failed with status ${response.status}`;\n const code =\n (data && typeof data === 'object' && 'error' in data && typeof data.error === 'string')\n ? data.error\n : 'request_failed';\n const err = new Error(message) as Error & { code?: string; details?: unknown };\n err.code = code;\n err.details = data;\n return err;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA8HO,IAAM,oBAAN,MAAwB;AAAA,EAG3B,YAAY,QAAiC;AACzC,SAAK,SAAS;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,MAAM,SAA6C;AACrD,QAAI,QAAQ,UAAU,UAAa,QAAQ,UAAU,MAAM;AACvD,YAAM,IAAI,MAAM,iCAAiC;AAAA,IACrD;AACA,QAAI,CAAC,QAAQ,aAAa,OAAO,KAAK,QAAQ,SAAS,EAAE,WAAW,GAAG;AACnE,YAAM,IAAI,MAAM,wDAAwD;AAAA,IAC5E;AACA,UAAM,WAAW,MAAM,MAAM,GAAG,KAAK,OAAO,OAAO,qBAAqB;AAAA,MACpE,QAAQ;AAAA,MACR,SAAS;AAAA,QACL,mBAAmB,KAAK,OAAO;AAAA,QAC/B,gBAAgB;AAAA,QAChB,GAAG,KAAK,OAAO;AAAA,MACnB;AAAA,MACA,MAAM,KAAK,UAAU,OAAO;AAAA,IAChC,CAAC;AACD,QAAI,CAAC,SAAS,IAAI;AACd,YAAM,MAAM,KAAK,MAAM,UAAU,gBAAgB;AAAA,IACrD;AACA,WAAQ,MAAM,SAAS,KAAK;AAAA,EAChC;AAAA;AAAA,EAGA,MAAM,aAA8C;AAChD,UAAM,WAAW,MAAM,MAAM,GAAG,KAAK,OAAO,OAAO,qBAAqB;AAAA,MACpE,SAAS,KAAK,OAAO;AAAA,IACzB,CAAC;AACD,QAAI,CAAC,SAAS,IAAI;AACd,YAAM,MAAM,KAAK,MAAM,UAAU,qBAAqB;AAAA,IAC1D;AACA,WAAQ,MAAM,SAAS,KAAK;AAAA,EAChC;AAAA,EAEA,MAAc,MAAM,UAAoB,IAA4B;AAChE,UAAM,OAAO,MAAM,SAAS,KAAK,EAAE,MAAM,OAAO,CAAC,EAAE;AACnD,UAAM,UACD,QAAQ,OAAO,SAAS,YAAY,aAAa,QAAQ,OAAO,KAAK,YAAY,WAC5E,KAAK,UACL,GAAG,EAAE,uBAAuB,SAAS,MAAM;AACrD,UAAM,OACD,QAAQ,OAAO,SAAS,YAAY,WAAW,QAAQ,OAAO,KAAK,UAAU,WACxE,KAAK,QACL;AACV,UAAM,MAAM,IAAI,MAAM,OAAO;AAC7B,QAAI,OAAO;AACX,QAAI,UAAU;AACd,WAAO;AAAA,EACX;AACJ;","names":[]}
@@ -0,0 +1,54 @@
1
+ // src/judgment/index.ts
2
+ var JudgmentNamespace = class {
3
+ constructor(config) {
4
+ this.config = config;
5
+ }
6
+ /**
7
+ * Judge a state against typed questions. Returns one answer per
8
+ * question id with probabilities your code can branch on.
9
+ */
10
+ async judge(request) {
11
+ if (request.state === void 0 || request.state === null) {
12
+ throw new Error("judgment.judge requires `state`");
13
+ }
14
+ if (!request.questions || Object.keys(request.questions).length === 0) {
15
+ throw new Error("judgment.judge requires a non-empty `questions` object");
16
+ }
17
+ const response = await fetch(`${this.config.baseUrl}/api/v1/judgments`, {
18
+ method: "POST",
19
+ headers: {
20
+ "CENCORI_API_KEY": this.config.apiKey,
21
+ "Content-Type": "application/json",
22
+ ...this.config.headers
23
+ },
24
+ body: JSON.stringify(request)
25
+ });
26
+ if (!response.ok) {
27
+ throw await this.error(response, "judgment.judge");
28
+ }
29
+ return await response.json();
30
+ }
31
+ /** List the judgment models with their pricing. No API key required. */
32
+ async listModels() {
33
+ const response = await fetch(`${this.config.baseUrl}/api/v1/judgments`, {
34
+ headers: this.config.headers
35
+ });
36
+ if (!response.ok) {
37
+ throw await this.error(response, "judgment.listModels");
38
+ }
39
+ return await response.json();
40
+ }
41
+ async error(response, op) {
42
+ const data = await response.json().catch(() => ({}));
43
+ const message = data && typeof data === "object" && "message" in data && typeof data.message === "string" ? data.message : `${op} failed with status ${response.status}`;
44
+ const code = data && typeof data === "object" && "error" in data && typeof data.error === "string" ? data.error : "request_failed";
45
+ const err = new Error(message);
46
+ err.code = code;
47
+ err.details = data;
48
+ return err;
49
+ }
50
+ };
51
+ export {
52
+ JudgmentNamespace
53
+ };
54
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/judgment/index.ts"],"sourcesContent":["/**\n * Judgment API — structured decisions from decision models.\n *\n * Send a state plus typed questions (noul / choice / score) and get back\n * probabilities — never generated text. Any `judgment`-type catalog model\n * works here through the same endpoint (TypeSafe's Jev today).\n *\n * @example\n * const result = await cencori.judgment.judge({\n * state: 'I was charged twice. Please refund the duplicate.',\n * questions: {\n * department: {\n * type: 'choice',\n * instructions: 'Which team should handle this?',\n * criteria: { billing: 'Payments, invoicing, refunds', technical: 'Bugs, outages' },\n * },\n * requests_refund: { type: 'noul', instructions: 'Does the customer request a refund?' },\n * },\n * });\n * console.log(result.answers.department.choice); // 'billing'\n *\n * @example\n * const { data: models } = await cencori.judgment.listModels();\n */\n\nimport type { CencoriConfig } from '../types';\n\nexport type JudgmentQuestionType = 'noul' | 'choice' | 'score';\n\n/** Text, a JSON object, or an array of text values. */\nexport type JudgmentState = string | Record<string, unknown> | unknown[];\n\nexport interface NoulQuestion {\n type: 'noul';\n instructions: string | Record<string, unknown>;\n criteria?: { true?: string | Record<string, unknown> | null; false?: string | Record<string, unknown> | null } | null;\n}\n\nexport interface ChoiceQuestion {\n type: 'choice';\n instructions: string | Record<string, unknown>;\n /** 1–255 options: option name → rubric description (`null` needs none). */\n criteria: Record<string, string | Record<string, unknown> | null>;\n}\n\nexport interface ScoreQuestion {\n type: 'score';\n instructions: string | Record<string, unknown>;\n /** 2–10 ordered level descriptions, lowest first. */\n criteria: string[];\n}\n\nexport type JudgmentQuestion = NoulQuestion | ChoiceQuestion | ScoreQuestion;\n\nexport interface JudgeRequest {\n /** What the questions are about: text, a JSON object, or an array. */\n state: JudgmentState;\n /** Non-empty map of question id → typed question. Answers come back under the same ids. */\n questions: Record<string, JudgmentQuestion>;\n /** Judgment model id. Default: `jev-latest`. */\n model?: string;\n}\n\nexport interface NoulAnswer {\n type: 'noul';\n /** Probability the answer is yes, 0–1. */\n noul: number;\n}\n\nexport interface ChoiceAnswer {\n type: 'choice';\n choice: string;\n confidence: number;\n probabilities: Record<string, number>;\n}\n\nexport interface ScoreAnswer {\n type: 'score';\n score: number;\n confidence: number;\n legend: Record<string, string>;\n probabilities: Record<string, number>;\n}\n\nexport type JudgmentAnswer = NoulAnswer | ChoiceAnswer | ScoreAnswer;\n\nexport interface JudgmentUsage {\n input_tokens: number;\n output_tokens: number;\n}\n\nexport interface JudgeResult {\n object: 'judgment';\n /** The model id that answered (aliases resolve upstream, e.g. `jev-latest` → `jev-1.13.0`). */\n model: string;\n answers: Record<string, JudgmentAnswer>;\n usage: JudgmentUsage;\n uses_byok?: boolean;\n}\n\nexport interface JudgmentModelInfo {\n id: string;\n owned_by: string;\n type: 'judgment';\n context_window: number;\n available: boolean;\n unavailable_reason?: string;\n pricing: {\n input_per_million: number | null;\n output_per_million: number | null;\n currency: 'USD';\n input_display: string;\n output_display: string;\n note: string;\n };\n}\n\nexport interface JudgmentModelsResponse {\n object: 'list';\n endpoint: string;\n context_tokens: number;\n data: JudgmentModelInfo[];\n question_types: JudgmentQuestionType[];\n default_model: string;\n}\n\nexport class JudgmentNamespace {\n private config: Required<CencoriConfig>;\n\n constructor(config: Required<CencoriConfig>) {\n this.config = config;\n }\n\n /**\n * Judge a state against typed questions. Returns one answer per\n * question id with probabilities your code can branch on.\n */\n async judge(request: JudgeRequest): Promise<JudgeResult> {\n if (request.state === undefined || request.state === null) {\n throw new Error('judgment.judge requires `state`');\n }\n if (!request.questions || Object.keys(request.questions).length === 0) {\n throw new Error('judgment.judge requires a non-empty `questions` object');\n }\n const response = await fetch(`${this.config.baseUrl}/api/v1/judgments`, {\n method: 'POST',\n headers: {\n 'CENCORI_API_KEY': this.config.apiKey,\n 'Content-Type': 'application/json',\n ...this.config.headers,\n },\n body: JSON.stringify(request),\n });\n if (!response.ok) {\n throw await this.error(response, 'judgment.judge');\n }\n return (await response.json()) as JudgeResult;\n }\n\n /** List the judgment models with their pricing. No API key required. */\n async listModels(): Promise<JudgmentModelsResponse> {\n const response = await fetch(`${this.config.baseUrl}/api/v1/judgments`, {\n headers: this.config.headers,\n });\n if (!response.ok) {\n throw await this.error(response, 'judgment.listModels');\n }\n return (await response.json()) as JudgmentModelsResponse;\n }\n\n private async error(response: Response, op: string): Promise<Error> {\n const data = await response.json().catch(() => ({}));\n const message =\n (data && typeof data === 'object' && 'message' in data && typeof data.message === 'string')\n ? data.message\n : `${op} failed with status ${response.status}`;\n const code =\n (data && typeof data === 'object' && 'error' in data && typeof data.error === 'string')\n ? data.error\n : 'request_failed';\n const err = new Error(message) as Error & { code?: string; details?: unknown };\n err.code = code;\n err.details = data;\n return err;\n }\n}\n"],"mappings":";AA8HO,IAAM,oBAAN,MAAwB;AAAA,EAG3B,YAAY,QAAiC;AACzC,SAAK,SAAS;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,MAAM,SAA6C;AACrD,QAAI,QAAQ,UAAU,UAAa,QAAQ,UAAU,MAAM;AACvD,YAAM,IAAI,MAAM,iCAAiC;AAAA,IACrD;AACA,QAAI,CAAC,QAAQ,aAAa,OAAO,KAAK,QAAQ,SAAS,EAAE,WAAW,GAAG;AACnE,YAAM,IAAI,MAAM,wDAAwD;AAAA,IAC5E;AACA,UAAM,WAAW,MAAM,MAAM,GAAG,KAAK,OAAO,OAAO,qBAAqB;AAAA,MACpE,QAAQ;AAAA,MACR,SAAS;AAAA,QACL,mBAAmB,KAAK,OAAO;AAAA,QAC/B,gBAAgB;AAAA,QAChB,GAAG,KAAK,OAAO;AAAA,MACnB;AAAA,MACA,MAAM,KAAK,UAAU,OAAO;AAAA,IAChC,CAAC;AACD,QAAI,CAAC,SAAS,IAAI;AACd,YAAM,MAAM,KAAK,MAAM,UAAU,gBAAgB;AAAA,IACrD;AACA,WAAQ,MAAM,SAAS,KAAK;AAAA,EAChC;AAAA;AAAA,EAGA,MAAM,aAA8C;AAChD,UAAM,WAAW,MAAM,MAAM,GAAG,KAAK,OAAO,OAAO,qBAAqB;AAAA,MACpE,SAAS,KAAK,OAAO;AAAA,IACzB,CAAC;AACD,QAAI,CAAC,SAAS,IAAI;AACd,YAAM,MAAM,KAAK,MAAM,UAAU,qBAAqB;AAAA,IAC1D;AACA,WAAQ,MAAM,SAAS,KAAK;AAAA,EAChC;AAAA,EAEA,MAAc,MAAM,UAAoB,IAA4B;AAChE,UAAM,OAAO,MAAM,SAAS,KAAK,EAAE,MAAM,OAAO,CAAC,EAAE;AACnD,UAAM,UACD,QAAQ,OAAO,SAAS,YAAY,aAAa,QAAQ,OAAO,KAAK,YAAY,WAC5E,KAAK,UACL,GAAG,EAAE,uBAAuB,SAAS,MAAM;AACrD,UAAM,OACD,QAAQ,OAAO,SAAS,YAAY,WAAW,QAAQ,OAAO,KAAK,UAAU,WACxE,KAAK,QACL;AACV,UAAM,MAAM,IAAI,MAAM,OAAO;AAC7B,QAAI,OAAO;AACX,QAAI,UAAU;AACd,WAAO;AAAA,EACX;AACJ;","names":[]}
@@ -27,6 +27,44 @@ __export(vercel_exports, {
27
27
  module.exports = __toCommonJS(vercel_exports);
28
28
 
29
29
  // src/vercel/cencori-chat-model.ts
30
+ function extractStreamError(chunk) {
31
+ const error = chunk.error;
32
+ if (typeof error === "string" && error) return error;
33
+ if (error && typeof error === "object") {
34
+ const record = error;
35
+ if (typeof record.message === "string" && record.message) {
36
+ return record.message;
37
+ }
38
+ try {
39
+ return JSON.stringify(error);
40
+ } catch {
41
+ return "Provider stream error";
42
+ }
43
+ }
44
+ if (typeof chunk.message === "string" && chunk.message && chunk.delta === void 0 && chunk.finish_reason === void 0 && chunk.tool_calls === void 0 && chunk.toolCalls === void 0 && chunk.usage === void 0) {
45
+ return chunk.message;
46
+ }
47
+ return void 0;
48
+ }
49
+ function extractHttpError(body, fallback) {
50
+ if (!body || typeof body !== "object") return fallback;
51
+ const record = body;
52
+ if (typeof record.message === "string" && record.message) return record.message;
53
+ const error = record.error;
54
+ if (typeof error === "string" && error) {
55
+ return typeof record.message === "string" && record.message ? record.message : error;
56
+ }
57
+ if (error && typeof error === "object") {
58
+ const nested = error;
59
+ if (typeof nested.message === "string" && nested.message) return nested.message;
60
+ try {
61
+ return JSON.stringify(error);
62
+ } catch {
63
+ return fallback;
64
+ }
65
+ }
66
+ return fallback;
67
+ }
30
68
  var CencoriChatLanguageModel = class {
31
69
  constructor(modelId, settings) {
32
70
  this.specificationVersion = "v3";
@@ -165,8 +203,10 @@ var CencoriChatLanguageModel = class {
165
203
  signal: options.abortSignal
166
204
  });
167
205
  if (!response.ok) {
168
- const error = await response.json().catch(() => ({ error: "Unknown error" }));
169
- throw new Error(`Cencori API error: ${error.error || response.statusText}`);
206
+ const body = await response.json().catch(() => ({ error: "Unknown error" }));
207
+ throw new Error(
208
+ `Cencori API error: ${extractHttpError(body, response.statusText)}`
209
+ );
170
210
  }
171
211
  const data = await response.json();
172
212
  const content = [];
@@ -217,8 +257,10 @@ var CencoriChatLanguageModel = class {
217
257
  signal: options.abortSignal
218
258
  });
219
259
  if (!response.ok) {
220
- const error = await response.json().catch(() => ({ error: "Unknown error" }));
221
- throw new Error(`Cencori API error: ${error.error || response.statusText}`);
260
+ const body = await response.json().catch(() => ({ error: "Unknown error" }));
261
+ throw new Error(
262
+ `Cencori API error: ${extractHttpError(body, response.statusText)}`
263
+ );
222
264
  }
223
265
  const reader = response.body?.getReader();
224
266
  if (!reader) {
@@ -273,6 +315,15 @@ var CencoriChatLanguageModel = class {
273
315
  }
274
316
  try {
275
317
  const chunk = JSON.parse(data);
318
+ const streamError = extractStreamError(
319
+ chunk
320
+ );
321
+ if (streamError) {
322
+ controller.error(
323
+ new Error(`Cencori API error: ${streamError}`)
324
+ );
325
+ return;
326
+ }
276
327
  if (chunk.delta) {
277
328
  if (!started) {
278
329
  started = true;
@@ -288,8 +339,9 @@ var CencoriChatLanguageModel = class {
288
339
  delta: chunk.delta
289
340
  });
290
341
  }
291
- if (chunk.tool_calls && chunk.tool_calls.length > 0) {
292
- for (const tc of chunk.tool_calls) {
342
+ const toolCalls = chunk.tool_calls && chunk.tool_calls.length > 0 ? chunk.tool_calls : chunk.toolCalls && chunk.toolCalls.length > 0 ? chunk.toolCalls : void 0;
343
+ if (toolCalls) {
344
+ for (const tc of toolCalls) {
293
345
  controller.enqueue({
294
346
  type: "tool-call",
295
347
  toolCallId: tc.id,
@@ -300,6 +352,14 @@ var CencoriChatLanguageModel = class {
300
352
  }
301
353
  }
302
354
  if (chunk.finish_reason) {
355
+ if (chunk.finish_reason === "error") {
356
+ controller.error(
357
+ new Error(
358
+ `Cencori API error: ${extractStreamError(chunk) ?? "stream failed with error"}`
359
+ )
360
+ );
361
+ return;
362
+ }
303
363
  if (started) {
304
364
  controller.enqueue({
305
365
  type: "text-end",