behavior-wrapped 0.2.12 → 0.2.14

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.
@@ -1,162 +0,0 @@
1
- #!/usr/bin/env node
2
- import { discoverAllSessionsAsync, readRecordsAsync, sessionsInDefaultWindow } from "../server/discovery.mjs";
3
- import { buildPhraseCandidates, judgePhraseCard } from "../server/phrase-card.mjs";
4
- import { buildInteractionToneCandidates, judgeInteractionTone } from "../server/interaction-tone.mjs";
5
- import { buildSessionTopicCandidates, judgeSessionTopics } from "../server/session-topics.mjs";
6
- import { buildWorkaroundTrajectories, judgeWorkarounds } from "../server/instrumental-workarounds.mjs";
7
-
8
- const apiKey = process.env.OPENROUTER_API_KEY;
9
- if (!apiKey) throw new Error("OPENROUTER_API_KEY is required.");
10
-
11
- const configuredModels = [
12
- { label: "Gemma 4 31B", id: "google/gemma-4-31b-it:free", reasoningEffort: "none" },
13
- { label: "GPT-OSS 20B", id: "openai/gpt-oss-20b:free", reasoningEffort: "low", minimumMaxTokens: 16_384 },
14
- { label: "GPT-5.6 Luna (ZDR)", id: "openai/gpt-5.6-luna", reasoningEffort: "none", zdr: true },
15
- ];
16
- const models = process.env.COMPARE_MODEL
17
- ? configuredModels.filter((model) => model.id.includes(process.env.COMPARE_MODEL))
18
- : configuredModels;
19
- if (!models.length) throw new Error(`No configured model matched COMPARE_MODEL=${process.env.COMPARE_MODEL}.`);
20
-
21
- const providersSeen = new Set();
22
- const usageSeen = [];
23
- function privacyFilteredFetch(model) {
24
- return async (url, options = {}) => {
25
- const requestBody = JSON.parse(options.body);
26
- requestBody.provider = { ...(requestBody.provider || {}), data_collection: "deny", ...(model.zdr ? { zdr: true } : {}) };
27
- requestBody.reasoning = { ...(requestBody.reasoning || {}), effort: model.reasoningEffort, exclude: true };
28
- requestBody.max_tokens = Math.max(Number(requestBody.max_tokens) || 0, model.minimumMaxTokens || 0);
29
- let response;
30
- for (let attempt = 0; attempt < 4; attempt++) {
31
- response = await fetch(url, { ...options, body: JSON.stringify(requestBody) });
32
- if (response.status !== 429 || attempt === 3) break;
33
- const waitMs = 5_000 * (2 ** attempt);
34
- process.stdout.write(`capacity retry in ${waitMs / 1000}s... `);
35
- await new Promise((resolve) => setTimeout(resolve, waitMs));
36
- }
37
- try {
38
- const responseBody = await response.clone().json();
39
- if (responseBody?.provider) providersSeen.add(responseBody.provider);
40
- if (responseBody?.usage) usageSeen.push({
41
- provider: responseBody.provider || null,
42
- promptTokens: Number(responseBody.usage.prompt_tokens) || 0,
43
- completionTokens: Number(responseBody.usage.completion_tokens) || 0,
44
- totalTokens: Number(responseBody.usage.total_tokens) || 0,
45
- costUsd: Number(responseBody.usage.cost) || 0,
46
- });
47
- } catch { /* The judge functions report malformed responses. */ }
48
- return response;
49
- };
50
- }
51
-
52
- function errorSummary(error) {
53
- return {
54
- error: error?.message || String(error),
55
- ...(error?.judgeDetails?.http_status ? { status: error.judgeDetails.http_status } : {}),
56
- ...(error?.judgeDetails?.upstream_code ? { code: error.judgeDetails.upstream_code } : {}),
57
- };
58
- }
59
-
60
- async function attempt(label, operation) {
61
- process.stdout.write(` ${label}... `);
62
- const startedAt = Date.now();
63
- try {
64
- const value = await operation();
65
- console.log(`ok (${((Date.now() - startedAt) / 1000).toFixed(1)}s)`);
66
- return { ok: true, value };
67
- } catch (error) {
68
- console.log(`failed (${((Date.now() - startedAt) / 1000).toFixed(1)}s)`);
69
- return { ok: false, ...errorSummary(error) };
70
- }
71
- }
72
-
73
- function summarize(result) {
74
- const phrase = result.phrase?.ok ? {
75
- phrase: result.phrase.value.phrase,
76
- occurrences: result.phrase.value.occurrences,
77
- latencyMs: result.phrase.value.latencyMs,
78
- } : result.phrase;
79
- const tone = result.tone?.ok ? {
80
- frustratedMessages: result.tone.value.frustratedMessages,
81
- gratefulMessages: result.tone.value.gratefulMessages,
82
- frustrationQuote: result.tone.value.frustrationQuote,
83
- latencyMs: result.tone.value.latencyMs,
84
- } : result.tone;
85
- const topics = result.topics?.ok ? {
86
- topics: result.topics.value.topics.map(({ topic, percentage }) => ({ topic, percentage })),
87
- latencyMs: result.topics.value.latencyMs,
88
- } : result.topics;
89
- const workarounds = result.workarounds?.ok ? {
90
- confirmed: result.workarounds.value.card.count,
91
- borderline: result.workarounds.value.review.borderline.length,
92
- examples: result.workarounds.value.review.occurrences.map(({ summary, confidence, disclosure }) => ({ summary, confidence, disclosure })),
93
- latencyMs: result.workarounds.value.review.latencyMs,
94
- } : result.workarounds;
95
- return { phrase, tone, topics, workarounds };
96
- }
97
-
98
- function summarizeUsage(records) {
99
- return records.reduce((total, record) => ({
100
- requests: total.requests + 1,
101
- promptTokens: total.promptTokens + record.promptTokens,
102
- completionTokens: total.completionTokens + record.completionTokens,
103
- totalTokens: total.totalTokens + record.totalTokens,
104
- costUsd: Number((total.costUsd + record.costUsd).toFixed(12)),
105
- }), { requests: 0, promptTokens: 0, completionTokens: 0, totalTokens: 0, costUsd: 0 });
106
- }
107
-
108
- console.log("Preparing the same 30-day, locally redacted Behavior Wrapped input for both models...");
109
- const catalog = await discoverAllSessionsAsync();
110
- const sessions = sessionsInDefaultWindow(catalog.sessions, { days: 30 });
111
- const records = [];
112
- for (const publicSession of sessions) {
113
- const session = catalog.index.get(publicSession.id);
114
- records.push({ sessionId: session.id, agent: session.agent, records: await readRecordsAsync(session.file, session.agent) });
115
- }
116
-
117
- const phraseCandidates = buildPhraseCandidates(records, { maximumCandidates: 100 });
118
- const toneCandidates = buildInteractionToneCandidates(records);
119
- const topicBundle = buildSessionTopicCandidates(records);
120
- const workaroundBundle = buildWorkaroundTrajectories(records);
121
- console.log(`Snapshot: ${sessions.length} sessions; ${phraseCandidates.length} phrase candidates; ${toneCandidates.length} tone candidates; ${topicBundle.candidates.length} topic candidates; ${workaroundBundle.chunks.length} workaround chunks.`);
122
-
123
- const comparison = {};
124
- for (const model of models) {
125
- providersSeen.clear();
126
- usageSeen.length = 0;
127
- console.log(`\n${model.label} (${model.id})`);
128
- const options = { model: model.id, fetchImpl: privacyFilteredFetch(model) };
129
- const phrase = phraseCandidates.length
130
- ? await attempt("favorite phrase", () => judgePhraseCard(phraseCandidates, apiKey, options))
131
- : { ok: false, error: "No candidates" };
132
- const tone = toneCandidates.length
133
- ? await attempt("interaction tone", () => judgeInteractionTone(toneCandidates, apiKey, options))
134
- : { ok: false, error: "No candidates" };
135
- const topics = topicBundle.candidates.length
136
- ? await attempt("session topics", () => judgeSessionTopics(topicBundle, apiKey, options))
137
- : { ok: false, error: "No candidates" };
138
- const workarounds = workaroundBundle.chunks.length
139
- ? await attempt("workarounds", () => judgeWorkarounds(workaroundBundle, apiKey, {
140
- ...options,
141
- onProgress: ({ index, total }) => process.stdout.write(index === 1 ? `[${index}/${total}] ` : `${index}/${total} `),
142
- }))
143
- : { ok: false, error: "No candidates" };
144
- comparison[model.label] = {
145
- providers: [...providersSeen],
146
- privacy: { dataCollectionDenied: true, zeroDataRetentionRequired: Boolean(model.zdr) },
147
- usage: summarizeUsage(usageSeen),
148
- ...summarize({ phrase, tone, topics, workarounds }),
149
- };
150
- }
151
-
152
- console.log("\nCOMPARISON_JSON");
153
- console.log(JSON.stringify({
154
- input: {
155
- sessions: sessions.length,
156
- phraseCandidates: phraseCandidates.length,
157
- toneCandidates: toneCandidates.length,
158
- topicCandidates: topicBundle.candidates.length,
159
- workaroundChunks: workaroundBundle.chunks.length,
160
- },
161
- models: comparison,
162
- }, null, 2));