@traits-dev/core 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Justin Hambleton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # @traits-dev/core
2
+
3
+ Core SDK for traits.dev personality profiles: validate, compile, inject, and evaluate.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add @traits-dev/core
9
+ ```
10
+
11
+ ## Basic Usage
12
+
13
+ ```ts
14
+ import { compileProfile, validateProfile } from "@traits-dev/core";
15
+
16
+ const profilePath = "profiles/resolve.yaml";
17
+ const validation = validateProfile(profilePath);
18
+ if (validation.exitCode !== 0) throw new Error("Invalid profile");
19
+
20
+ const compiled = compileProfile(profilePath, {
21
+ model: "gpt-4o",
22
+ bundledProfilesDir: "profiles",
23
+ knowledgeBaseDir: "knowledge-base"
24
+ });
25
+
26
+ console.log(compiled.text);
27
+ ```
28
+
29
+ Public API is exported from `@traits-dev/core`. Monorepo/internal helpers are exported from `@traits-dev/core/internal`.
@@ -0,0 +1,352 @@
1
+ type Level = "very-low" | "low" | "medium" | "high" | "very-high";
2
+ type DimensionName = "formality" | "warmth" | "verbosity" | "directness" | "empathy" | "humor";
3
+ type HumorStyle = "none" | "dry" | "subtle-wit" | "playful";
4
+ type DimensionShorthand = Level;
5
+ type DimensionObject = {
6
+ target: Level;
7
+ adapt?: boolean;
8
+ floor?: Level;
9
+ ceiling?: Level;
10
+ };
11
+ type HumorDimensionObject = DimensionObject & {
12
+ style?: HumorStyle;
13
+ };
14
+ type DimensionValue = DimensionShorthand | DimensionObject;
15
+ type HumorDimensionValue = DimensionShorthand | HumorDimensionObject;
16
+ interface VocabularyConstraints {
17
+ preferred_terms?: string[];
18
+ forbidden_terms?: string[];
19
+ preferred_terms_remove?: string[];
20
+ forbidden_terms_remove?: string[];
21
+ }
22
+ interface ContextAdaptation {
23
+ when: string;
24
+ adjustments?: Partial<Record<DimensionName, DimensionValue | HumorDimensionValue>>;
25
+ inject?: string[];
26
+ priority?: number;
27
+ }
28
+ interface PersonalityProfile {
29
+ schema: string;
30
+ meta: {
31
+ name: string;
32
+ version: string;
33
+ description: string;
34
+ tags?: string[];
35
+ target_audience?: string;
36
+ [key: string]: unknown;
37
+ };
38
+ identity: {
39
+ role: string;
40
+ backstory?: string;
41
+ expertise_domains?: string[];
42
+ [key: string]: unknown;
43
+ };
44
+ voice: {
45
+ formality: DimensionValue;
46
+ warmth: DimensionValue;
47
+ verbosity: DimensionValue;
48
+ directness: DimensionValue;
49
+ empathy: DimensionValue;
50
+ humor: HumorDimensionValue;
51
+ [key: string]: DimensionValue | HumorDimensionValue | undefined;
52
+ };
53
+ vocabulary?: VocabularyConstraints;
54
+ behavioral_rules?: string[];
55
+ context_adaptations?: ContextAdaptation[];
56
+ localization?: Record<string, unknown>;
57
+ channel_adaptations?: Record<string, unknown>;
58
+ extends?: string;
59
+ behavioral_rules_remove?: string[];
60
+ context_adaptations_remove?: string[];
61
+ [key: string]: unknown;
62
+ }
63
+ interface ValidationDiagnostic {
64
+ code: string;
65
+ severity: "warning" | "error";
66
+ message: string;
67
+ location?: string;
68
+ promotedFrom?: string;
69
+ details?: unknown;
70
+ }
71
+ interface ValidationCheckSummary {
72
+ status: "pass" | "warning" | "error";
73
+ errors: number;
74
+ warnings: number;
75
+ }
76
+ interface ValidationResult {
77
+ profilePath?: string;
78
+ parentPath: string | null;
79
+ profile: PersonalityProfile | null;
80
+ strict: boolean;
81
+ warnings: ValidationDiagnostic[];
82
+ errors: ValidationDiagnostic[];
83
+ promotedWarnings: ValidationDiagnostic[];
84
+ effectiveErrors: ValidationDiagnostic[];
85
+ constraintCount: number;
86
+ constraintBreakdown: {
87
+ behavioral_rules: number;
88
+ vocabulary_preferred_terms: number;
89
+ vocabulary_forbidden_terms: number;
90
+ context_adaptations: number;
91
+ [key: string]: number;
92
+ };
93
+ checks: Record<string, ValidationCheckSummary>;
94
+ isValid: boolean;
95
+ exitCode: number;
96
+ }
97
+ interface CompiledPersonality {
98
+ text: string;
99
+ placement: {
100
+ model: string;
101
+ recommended_position: "start" | "after_tools" | "end";
102
+ rationale: string;
103
+ };
104
+ metadata: {
105
+ profile: string;
106
+ version: string;
107
+ schema_version: string;
108
+ model_target: string;
109
+ token_count: number;
110
+ safety_floor_included: boolean;
111
+ adaptive_dimensions: string[];
112
+ humor_style: string | null;
113
+ compilation_timestamp: string;
114
+ };
115
+ trace?: {
116
+ context_matches: string[];
117
+ adjustments_applied: Record<string, unknown>;
118
+ inject_rules: string[];
119
+ protected_refusal_terms_restored: string[];
120
+ pattern_selections: unknown[];
121
+ interaction_patterns: unknown[];
122
+ };
123
+ validation?: {
124
+ warnings: ValidationDiagnostic[];
125
+ errors: ValidationDiagnostic[];
126
+ strict: boolean;
127
+ };
128
+ }
129
+ interface ExtendsDiagnostics {
130
+ warnings: ValidationDiagnostic[];
131
+ errors: ValidationDiagnostic[];
132
+ }
133
+ interface ExtendsResult {
134
+ profile: PersonalityProfile;
135
+ parentPath: string | null;
136
+ diagnostics: ExtendsDiagnostics;
137
+ }
138
+ interface ContextResolution {
139
+ matched: ContextAdaptation[];
140
+ resolvedAdjustments: Record<string, DimensionValue>;
141
+ injectRules: string[];
142
+ }
143
+
144
+ type ResolveOptions = {
145
+ bundledProfilesDir?: string;
146
+ };
147
+
148
+ declare function loadProfileFile(filePath: string): PersonalityProfile;
149
+
150
+ declare function resolveExtends(profilePath: string, options?: ResolveOptions): ExtendsResult;
151
+
152
+ declare function normalizeProfile(profilePath: string, options?: ResolveOptions): PersonalityProfile;
153
+
154
+ declare function resolveActiveContext(profile: PersonalityProfile, context?: Record<string, unknown>): ContextResolution;
155
+
156
+ declare function validateResolvedProfile(profile: PersonalityProfile, options?: {
157
+ strict?: boolean;
158
+ }): ValidationResult;
159
+ declare function validateProfile(profilePath: string, options?: {
160
+ strict?: boolean;
161
+ bundledProfilesDir?: string;
162
+ }): ValidationResult;
163
+
164
+ type CompileOptions = {
165
+ model?: string;
166
+ context?: Record<string, unknown>;
167
+ explain?: boolean;
168
+ strict?: boolean;
169
+ bundledProfilesDir?: string;
170
+ knowledgeBaseDir?: string;
171
+ };
172
+ declare function compileResolvedProfile(profile: PersonalityProfile, options?: CompileOptions): CompiledPersonality;
173
+ declare function compileProfile(profilePath: string, options?: CompileOptions): CompiledPersonality;
174
+
175
+ type CompiledLike = string | Pick<CompiledPersonality, "text" | "placement">;
176
+ declare function injectPersonality({ compiledPersonality, system, model }: {
177
+ compiledPersonality: CompiledLike;
178
+ system?: string;
179
+ model?: string;
180
+ }): string;
181
+
182
+ type EvalSample = {
183
+ id?: string;
184
+ prompt?: string;
185
+ response?: string;
186
+ };
187
+ declare function validateEvalScenario(scenario: unknown): {
188
+ valid: boolean;
189
+ errors: string[];
190
+ };
191
+ declare function validateEvalScenarios(scenarios: unknown): {
192
+ valid: boolean;
193
+ count: number;
194
+ invalid: Array<{
195
+ index: number;
196
+ id: string | null;
197
+ errors: string[];
198
+ }>;
199
+ };
200
+
201
+ type Tier1Options = {
202
+ includeHelpfulness?: boolean;
203
+ strict?: boolean;
204
+ bundledProfilesDir?: string;
205
+ };
206
+ declare function evaluateTier1Response(profile: PersonalityProfile, responseText: string, options?: Tier1Options): {
207
+ score: number;
208
+ checks: {
209
+ vocabulary: {
210
+ preferred_total: number;
211
+ preferred_matched: number;
212
+ forbidden_total: number;
213
+ forbidden_matched: number;
214
+ pass: boolean;
215
+ };
216
+ structure: {
217
+ behavioral_rule_count: number;
218
+ response_non_empty: boolean;
219
+ pass: boolean;
220
+ };
221
+ helpfulness: {
222
+ char_count: number;
223
+ pass: boolean;
224
+ skipped: boolean;
225
+ };
226
+ };
227
+ };
228
+ declare function runTier1Evaluation(profile: PersonalityProfile, samples: EvalSample[], options?: Tier1Options): {
229
+ tier: number;
230
+ sample_count: number;
231
+ average_score: number;
232
+ samples: Array<{
233
+ id: string;
234
+ score: number;
235
+ checks: ReturnType<typeof evaluateTier1Response>["checks"];
236
+ }>;
237
+ };
238
+ declare function runTier1EvaluationForProfile(profilePath: string, samples: EvalSample[], options?: Tier1Options): {
239
+ validation: ValidationResult;
240
+ report: ReturnType<typeof runTier1Evaluation>;
241
+ };
242
+
243
+ type Tier2Options = {
244
+ includeHelpfulness?: boolean;
245
+ strict?: boolean;
246
+ bundledProfilesDir?: string;
247
+ openaiApiKey?: string;
248
+ embeddingModel?: string;
249
+ openaiBaseUrl?: string;
250
+ openAIBaseUrl?: string;
251
+ fetchImpl?: typeof fetch;
252
+ fetchTimeoutMs?: number;
253
+ fetchMaxRetries?: number;
254
+ fetchRetryBaseMs?: number;
255
+ embeddingFn?: (text: string) => Promise<number[]>;
256
+ knowledgeBaseDir?: string;
257
+ modelTarget?: string;
258
+ };
259
+ declare function runTier2Evaluation(profile: PersonalityProfile, samples: EvalSample[], options?: Tier2Options): Promise<{
260
+ tier: number;
261
+ provider: string;
262
+ helpfulness_included: boolean;
263
+ sample_count: number;
264
+ average_score: number;
265
+ dimension_averages: Record<string, number>;
266
+ helpfulness_average: number | null;
267
+ samples: Array<{
268
+ id: string;
269
+ score: number;
270
+ dimension_scores: Record<string, number>;
271
+ helpfulness_score: number | null;
272
+ }>;
273
+ }>;
274
+ declare function runTier2EvaluationForProfile(profilePath: string, samples: EvalSample[], options?: Tier2Options): Promise<{
275
+ validation: ValidationResult;
276
+ report: Awaited<ReturnType<typeof runTier2Evaluation>>;
277
+ }>;
278
+
279
+ type Tier3Options = {
280
+ includeHelpfulness?: boolean;
281
+ strict?: boolean;
282
+ bundledProfilesDir?: string;
283
+ provider?: string;
284
+ judgeFn?: (args: {
285
+ systemPrompt: string;
286
+ userPrompt: string;
287
+ }) => Promise<string>;
288
+ openaiApiKey?: string;
289
+ anthropicApiKey?: string;
290
+ judgeModel?: string;
291
+ openaiBaseUrl?: string;
292
+ openAIBaseUrl?: string;
293
+ anthropicBaseUrl?: string;
294
+ fetchImpl?: typeof fetch;
295
+ fetchTimeoutMs?: number;
296
+ fetchMaxRetries?: number;
297
+ fetchRetryBaseMs?: number;
298
+ };
299
+ declare function runTier3Evaluation(profile: PersonalityProfile, samples: EvalSample[], options?: Tier3Options): Promise<{
300
+ tier: number;
301
+ provider: string;
302
+ helpfulness_included: boolean;
303
+ sample_count: number;
304
+ average_score: number;
305
+ samples: Array<{
306
+ id: string;
307
+ score: number;
308
+ dimension_average: number;
309
+ formality: number;
310
+ warmth: number;
311
+ verbosity: number;
312
+ directness: number;
313
+ empathy: number;
314
+ humor: number;
315
+ helpfulness: number | null;
316
+ rationale: string;
317
+ }>;
318
+ }>;
319
+ declare function runTier3EvaluationForProfile(profilePath: string, samples: EvalSample[], options?: Tier3Options): Promise<{
320
+ validation: ValidationResult;
321
+ report: Awaited<ReturnType<typeof runTier3Evaluation>>;
322
+ }>;
323
+
324
+ type ImportOptions = {
325
+ provider?: string;
326
+ analysisFn?: (args: {
327
+ systemPrompt: string;
328
+ userPrompt: string;
329
+ }) => Promise<unknown>;
330
+ openaiApiKey?: string;
331
+ anthropicApiKey?: string;
332
+ model?: string;
333
+ openaiBaseUrl?: string;
334
+ openAIBaseUrl?: string;
335
+ anthropicBaseUrl?: string;
336
+ fetchImpl?: typeof fetch;
337
+ fetchTimeoutMs?: number;
338
+ fetchMaxRetries?: number;
339
+ fetchRetryBaseMs?: number;
340
+ profileName?: string;
341
+ description?: string;
342
+ };
343
+ declare function mapImportAnalysisToProfile(analysis: unknown, options?: ImportOptions): PersonalityProfile;
344
+ declare function renderImportedProfileYAML(profile: PersonalityProfile): string;
345
+ declare function runImportAnalysis(promptText: unknown, options?: ImportOptions): Promise<{
346
+ provider: string;
347
+ analysis: Record<string, unknown>;
348
+ profile: PersonalityProfile;
349
+ yaml: string;
350
+ }>;
351
+
352
+ export { runImportAnalysis as A, runTier1EvaluationForProfile as B, type CompileOptions as C, type DimensionName as D, type EvalSample as E, runTier2Evaluation as F, runTier2EvaluationForProfile as G, type HumorDimensionObject as H, type ImportOptions as I, runTier3Evaluation as J, runTier3EvaluationForProfile as K, type Level as L, validateEvalScenario as M, validateEvalScenarios as N, validateProfile as O, type PersonalityProfile as P, validateResolvedProfile as Q, type Tier1Options as T, type ValidationResult as V, type ValidationCheckSummary as a, type ValidationDiagnostic as b, type CompiledPersonality as c, type ContextAdaptation as d, type ContextResolution as e, type DimensionObject as f, type DimensionShorthand as g, type DimensionValue as h, type ExtendsDiagnostics as i, type ExtendsResult as j, type HumorDimensionValue as k, type HumorStyle as l, type Tier2Options as m, type Tier3Options as n, type VocabularyConstraints as o, compileProfile as p, compileResolvedProfile as q, runTier1Evaluation as r, evaluateTier1Response as s, injectPersonality as t, loadProfileFile as u, mapImportAnalysisToProfile as v, normalizeProfile as w, renderImportedProfileYAML as x, resolveActiveContext as y, resolveExtends as z };