talentos 1.4.22 → 1.5.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.
@@ -0,0 +1,177 @@
1
+ // SessionStart hook of the TalentOS Claude Code plugin. Prints a short
2
+ // company-context primer for Claude. Dependency-free and fast: the whole run
3
+ // stays inside 2 s, and anything that goes wrong prints nothing and exits 0,
4
+ // so a session never waits on, or is told about, TalentOS being offline.
5
+ //
6
+ // A hook cannot use the OAuth sign-in Claude Code holds for the plugin's MCP
7
+ // server, so the live line (who you are, your Talent, counts) needs the key
8
+ // `npx talentos@1 init` stored on this machine. Without a key it prints the
9
+ // static primer and makes no network call. The key only ever goes to the url
10
+ // it was stored with.
11
+ import { spawn } from "node:child_process";
12
+ import { readFileSync, statSync, utimesSync, writeFileSync } from "node:fs";
13
+ import { homedir, tmpdir } from "node:os";
14
+ import { join } from "node:path";
15
+ import { pathToFileURL } from "node:url";
16
+
17
+ const BUDGET_MS = 1900;
18
+ const FETCH_MS = 1500;
19
+ const SYNC_EVERY_MS = 6 * 60 * 60 * 1000;
20
+ const FIELD_MAX = 80;
21
+ const TRAILING_SLASHES = /\/+$/u;
22
+
23
+ export const STATIC_PRIMER = [
24
+ "TalentOS is connected to this session through the `talentos` MCP server (if it asks you to sign in, run /mcp, pick talentos, choose Authenticate).",
25
+ "It holds the company's shared memory, Skills, Routines and people, and the user's Talent.",
26
+ "You do the user's work yourself; use TalentOS for company context (call `search_memory` before answering how this team works or what the user prefers) and hand work to their Talent only when the user explicitly asks.",
27
+ "Slash commands: /talentos:catch-up, /talentos:remember, /talentos:ask-talent.",
28
+ ].join(" ");
29
+
30
+ /** One line of the primer from a `whoami` result; undefined when it is not one. */
31
+ export function livePrimer(who) {
32
+ const member = clean(who?.member?.name) || clean(who?.member?.email);
33
+ const organization = clean(who?.organization?.name);
34
+ if (!(member && organization)) {
35
+ return;
36
+ }
37
+ const talent = clean(who?.talent?.name);
38
+ const counts = who?.counts ?? {};
39
+ return [
40
+ `TalentOS: signed in as ${member} at ${organization}.`,
41
+ talent ? `Their Talent is ${talent}.` : undefined,
42
+ typeof counts.companySkills === "number" &&
43
+ typeof counts.mySkills === "number"
44
+ ? `${counts.companySkills} company and ${counts.mySkills} personal Skills.`
45
+ : undefined,
46
+ typeof counts.routines === "number"
47
+ ? `${counts.routines} Routines.`
48
+ : undefined,
49
+ STATIC_PRIMER,
50
+ ]
51
+ .filter(Boolean)
52
+ .join(" ");
53
+ }
54
+
55
+ /** One line, control characters (newlines included) turned into spaces. */
56
+ function clean(value) {
57
+ if (typeof value !== "string") {
58
+ return "";
59
+ }
60
+ return Array.from(value.slice(0, FIELD_MAX * 4), (char) => {
61
+ const code = char.charCodeAt(0);
62
+ return code < 32 || code === 127 ? " " : char;
63
+ })
64
+ .join("")
65
+ .trim()
66
+ .slice(0, FIELD_MAX);
67
+ }
68
+
69
+ export function readCredentials(env, read = readFileSync) {
70
+ const stored = (() => {
71
+ try {
72
+ const root =
73
+ env.XDG_CONFIG_HOME || join(env.HOME || homedir(), ".config");
74
+ return JSON.parse(read(join(root, "talentos", "config.json"), "utf8"));
75
+ } catch {
76
+ return {};
77
+ }
78
+ })();
79
+ const apiKey = env.TALENTOS_API_KEY || stored.apiKey;
80
+ const apiUrl = env.TALENTOS_API_URL || stored.apiUrl;
81
+ if (typeof apiKey !== "string" || typeof apiUrl !== "string" || !apiKey) {
82
+ return;
83
+ }
84
+ return { apiKey, apiUrl: apiUrl.trim().replace(TRAILING_SLASHES, "") };
85
+ }
86
+
87
+ async function fetchWhoami({ apiKey, apiUrl }) {
88
+ const response = await fetch(`${apiUrl}/mcp`, {
89
+ body: JSON.stringify({
90
+ id: 1,
91
+ jsonrpc: "2.0",
92
+ method: "tools/call",
93
+ params: { arguments: {}, name: "whoami" },
94
+ }),
95
+ headers: {
96
+ Accept: "application/json, text/event-stream",
97
+ Authorization: `Bearer ${apiKey}`,
98
+ "Content-Type": "application/json",
99
+ },
100
+ method: "POST",
101
+ signal: AbortSignal.timeout(FETCH_MS),
102
+ });
103
+ if (!response.ok) {
104
+ return;
105
+ }
106
+ const body = await response.json();
107
+ return body?.result?.isError ? undefined : body?.result?.structuredContent;
108
+ }
109
+
110
+ /** Fire-and-forget Skills sync, opt-in and at most once per SYNC_EVERY_MS. */
111
+ function startSkillsSync(env) {
112
+ const stamp = join(
113
+ tmpdir(),
114
+ `talentos-skills-sync-${process.getuid?.() ?? 0}`
115
+ );
116
+ try {
117
+ if (Date.now() - statSync(stamp).mtimeMs < SYNC_EVERY_MS) {
118
+ return;
119
+ }
120
+ utimesSync(stamp, new Date(), new Date());
121
+ } catch {
122
+ writeFileSync(stamp, "");
123
+ }
124
+ spawn(
125
+ "npx",
126
+ [
127
+ "-y",
128
+ "talentos@1",
129
+ "skills",
130
+ "sync",
131
+ "--for",
132
+ "claude",
133
+ "--global",
134
+ "--company",
135
+ "--quiet",
136
+ ],
137
+ {
138
+ detached: true,
139
+ env,
140
+ shell: process.platform === "win32",
141
+ stdio: "ignore",
142
+ windowsHide: true,
143
+ }
144
+ ).unref();
145
+ }
146
+
147
+ async function main(env) {
148
+ setTimeout(() => process.exit(0), BUDGET_MS).unref();
149
+ const credentials = readCredentials(env);
150
+ let primer = STATIC_PRIMER;
151
+ if (credentials) {
152
+ try {
153
+ primer = livePrimer(await fetchWhoami(credentials));
154
+ } catch {
155
+ primer = undefined;
156
+ }
157
+ if (env.TALENTOS_SKILLS_SYNC === "1" && primer) {
158
+ try {
159
+ startSkillsSync(env);
160
+ } catch {
161
+ // Sync is a courtesy; the primer matters more.
162
+ }
163
+ }
164
+ }
165
+ if (primer) {
166
+ process.stdout.write(
167
+ `${JSON.stringify({ hookSpecificOutput: { additionalContext: primer, hookEventName: "SessionStart" } })}\n`
168
+ );
169
+ }
170
+ }
171
+
172
+ if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
173
+ main(process.env).then(
174
+ () => process.exit(0),
175
+ () => process.exit(0)
176
+ );
177
+ }