cognia-sdk 0.1.2 → 0.1.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.
package/README.md CHANGED
@@ -33,6 +33,22 @@ const proposals = await session.end();
33
33
  Injecting something never counts as using it. Only `outcome()` creates evidence, and only for items this context
34
34
  returned. Dogfood, canary and operator traffic is classified server-side and never counts.
35
35
 
36
+
37
+ ## Auth: key or delegated token
38
+
39
+ ```ts
40
+ // a developer credential or an SDK credential (cognia_sk_…)
41
+ new Cognia({ apiKey: process.env.COGNIA_API_KEY })
42
+
43
+ // delegated auth: the host already holds a Cognia bearer (e.g. the MCP OAuth access token)
44
+ new Cognia({ tokenProvider: async () => await getCogniaToken() }) // called on every request, never cached, never in errors
45
+ ```
46
+
47
+ If your identity lives in a connected client (Claude, ChatGPT, OpenClaw…) and the host hides its token, ask the connected
48
+ Cognia to run `sdk_link_start`: it shows a one-time code and a Cloud link; you approve on the website and receive an
49
+ **SDK credential** — a separate, narrow credential that can read context and report outcomes only. Details:
50
+ `docs/SDK_CREDENTIALS.md` in the Cognia repository.
51
+
36
52
  ## Scope
37
53
 
38
54
  `mine` = the identity's own memories and Skills. `organization` (default) = plus everything its organizations share
@@ -59,12 +59,14 @@ class Cognia {
59
59
  budget;
60
60
  timeoutMs;
61
61
  key;
62
+ tokenProvider;
62
63
  f;
63
64
  onError;
64
65
  constructor(o) {
65
- if (!o.apiKey)
66
- throw new Error("Cognia: apiKey is required");
67
- this.key = o.apiKey;
66
+ if (!o.apiKey && !o.tokenProvider)
67
+ throw new Error("Cognia: apiKey or tokenProvider is required");
68
+ this.key = o.apiKey ?? null;
69
+ this.tokenProvider = o.tokenProvider ?? null;
68
70
  this.baseUrl = (o.baseUrl ?? "https://api.cognia.fun").replace(/\/$/, "");
69
71
  this.scope = o.scope ?? "organization";
70
72
  this.budget = o.budget;
@@ -76,7 +78,11 @@ class Cognia {
76
78
  const ac = new AbortController();
77
79
  const t = timeoutMs ? setTimeout(() => ac.abort(), timeoutMs) : null;
78
80
  try {
79
- const r = await this.f(this.baseUrl + path, { method, headers: { authorization: `Bearer ${this.key}`, "content-type": "application/json", "user-agent": "cognia-sdk/0.1.1" }, body: body === undefined ? undefined : JSON.stringify(body), signal: ac.signal });
81
+ // delegated auth: the provider is asked on EVERY request; its answer is used once and never stored on the client
82
+ const token = this.tokenProvider ? await this.tokenProvider() : this.key;
83
+ if (!token)
84
+ throw new CogniaError(`Cognia ${method} ${path} → no credential`, 0, null);
85
+ const r = await this.f(this.baseUrl + path, { method, headers: { authorization: `Bearer ${token}`, "content-type": "application/json", "user-agent": "cognia-sdk/0.1.3" }, body: body === undefined ? undefined : JSON.stringify(body), signal: ac.signal });
80
86
  let j = null;
81
87
  try {
82
88
  j = await r.json();
@@ -1,7 +1,10 @@
1
1
  import type { ContextRequest, ContextResponse, ContextResult, OutcomeInput, OutcomeResponse, ExtractResponse, Scope } from "./types.js";
2
2
  export interface CogniaOptions {
3
- /** the agent's credential (cognia_sk_…); never logged, never put in a URL */
4
- apiKey: string;
3
+ /** the agent's credential (cognia_sk_…, a developer or SDK credential); never logged, never put in a URL */
4
+ apiKey?: string;
5
+ /** Delegated auth: called at REQUEST time to obtain the bearer (e.g. an MCP OAuth access token the host already
6
+ * holds); async so the host can refresh. The SDK never caches the token and never puts it in errors or onError. */
7
+ tokenProvider?: () => Promise<string> | string;
5
8
  baseUrl?: string;
6
9
  /** default scope for context(); the identity's grants decide what that means */
7
10
  scope?: Scope;
@@ -33,6 +36,7 @@ export declare class Cognia {
33
36
  } | undefined;
34
37
  readonly timeoutMs: number;
35
38
  private readonly key;
39
+ private readonly tokenProvider;
36
40
  private readonly f;
37
41
  private readonly onError;
38
42
  constructor(o: CogniaOptions);
@@ -54,12 +54,14 @@ export class Cognia {
54
54
  budget;
55
55
  timeoutMs;
56
56
  key;
57
+ tokenProvider;
57
58
  f;
58
59
  onError;
59
60
  constructor(o) {
60
- if (!o.apiKey)
61
- throw new Error("Cognia: apiKey is required");
62
- this.key = o.apiKey;
61
+ if (!o.apiKey && !o.tokenProvider)
62
+ throw new Error("Cognia: apiKey or tokenProvider is required");
63
+ this.key = o.apiKey ?? null;
64
+ this.tokenProvider = o.tokenProvider ?? null;
63
65
  this.baseUrl = (o.baseUrl ?? "https://api.cognia.fun").replace(/\/$/, "");
64
66
  this.scope = o.scope ?? "organization";
65
67
  this.budget = o.budget;
@@ -71,7 +73,11 @@ export class Cognia {
71
73
  const ac = new AbortController();
72
74
  const t = timeoutMs ? setTimeout(() => ac.abort(), timeoutMs) : null;
73
75
  try {
74
- const r = await this.f(this.baseUrl + path, { method, headers: { authorization: `Bearer ${this.key}`, "content-type": "application/json", "user-agent": "cognia-sdk/0.1.1" }, body: body === undefined ? undefined : JSON.stringify(body), signal: ac.signal });
76
+ // delegated auth: the provider is asked on EVERY request; its answer is used once and never stored on the client
77
+ const token = this.tokenProvider ? await this.tokenProvider() : this.key;
78
+ if (!token)
79
+ throw new CogniaError(`Cognia ${method} ${path} → no credential`, 0, null);
80
+ const r = await this.f(this.baseUrl + path, { method, headers: { authorization: `Bearer ${token}`, "content-type": "application/json", "user-agent": "cognia-sdk/0.1.3" }, body: body === undefined ? undefined : JSON.stringify(body), signal: ac.signal });
75
81
  let j = null;
76
82
  try {
77
83
  j = await r.json();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cognia-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Cognia for your model calls: one line gives any agent the Memories and Skills it may use, with provenance and honest receipts. Fails open.",
5
5
  "license": "MIT",
6
6
  "author": "Cognia",