xmemory 2.0.0 → 2.1.1

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
@@ -15,7 +15,7 @@ import { XmemoryClient } from "xmemory";
15
15
 
16
16
  const xm = new XmemoryClient({
17
17
  url: "https://api.xmemory.ai", // or set XMEM_API_URL env var
18
- token: "<your-token>", // or set XMEM_AUTH_TOKEN env var
18
+ apiKey: "<your-api-key>", // or set XMEM_API_KEY env var
19
19
  });
20
20
 
21
21
  // Write and read from an existing instance
@@ -28,11 +28,13 @@ console.log(result.reader_result);
28
28
 
29
29
  ## Configuration
30
30
 
31
- | Parameter | Env var | Default | Description |
32
- |-------------|-------------------|----------------------------|----------------------------------------|
33
- | `url` | `XMEM_API_URL` | `https://api.xmemory.ai` | Base URL of the Xmemory API |
34
- | `token` | `XMEM_AUTH_TOKEN` | `undefined` | Bearer token for authentication |
35
- | `timeoutMs` | — | `60000` | Default request timeout in milliseconds |
31
+ | Parameter | Env var | Default | Description |
32
+ |-------------|------------------|---------------------------|-----------------------------------------|
33
+ | `url` | `XMEM_API_URL` | `https://api.xmemory.ai` | Base URL of the Xmemory API |
34
+ | `apiKey` | `XMEM_API_KEY` | `undefined` | Bearer API key for authentication |
35
+ | `timeoutMs` | — | `60000` | Default request timeout in milliseconds |
36
+
37
+ The legacy `token` option and `XMEM_AUTH_TOKEN` env var are still accepted for backwards compatibility but are deprecated and will be removed in a future release. Using them prints a deprecation warning. If both the new and legacy values are set, the new ones win.
36
38
 
37
39
  ## Creating a client
38
40
 
@@ -40,13 +42,13 @@ console.log(result.reader_result);
40
42
  import { XmemoryClient, xmemoryInstance } from "xmemory";
41
43
 
42
44
  // Option 1: constructor (no health check)
43
- const xm1 = new XmemoryClient({ token: "..." });
45
+ const xm1 = new XmemoryClient({ apiKey: "..." });
44
46
 
45
47
  // Option 2: factory with health check
46
- const xm2 = await XmemoryClient.create({ token: "..." });
48
+ const xm2 = await XmemoryClient.create({ apiKey: "..." });
47
49
 
48
50
  // Option 3: convenience function (same as Option 2)
49
- const xm3 = await xmemoryInstance({ token: "..." });
51
+ const xm3 = await xmemoryInstance({ apiKey: "..." });
50
52
  ```
51
53
 
52
54
  ## Admin operations
@@ -168,6 +170,19 @@ const schema = await inst.getSchema();
168
170
  console.log(schema.data_schema);
169
171
  ```
170
172
 
173
+ ### `inst.describe(options?)` → `DescribeResult`
174
+
175
+ Get agent-facing tool descriptions enriched with the instance's schema.
176
+
177
+ ```typescript
178
+ const desc = await inst.describe();
179
+ console.log(desc.asText()); // plain text for system prompts
180
+ const tools = desc.asAnthropicTools(); // Anthropic tool-use format
181
+ const tools = desc.asOpenaiTools(); // OpenAI function-calling format
182
+ ```
183
+
184
+ Results are cached for 5 minutes. Call `inst.clearDescribeCache()` to force a refresh.
185
+
171
186
  ## Error handling
172
187
 
173
188
  All errors throw `XmemoryAPIError`. Health check failures throw `XmemoryHealthCheckError` (a subclass).
@@ -176,7 +191,7 @@ All errors throw `XmemoryAPIError`. Health check failures throw `XmemoryHealthCh
176
191
  import { XmemoryClient, XmemoryAPIError, XmemoryHealthCheckError } from "xmemory";
177
192
 
178
193
  try {
179
- const xm = await XmemoryClient.create({ token: "..." });
194
+ const xm = await XmemoryClient.create({ apiKey: "..." });
180
195
  } catch (e) {
181
196
  if (e instanceof XmemoryHealthCheckError) {
182
197
  console.error("Server unreachable:", e.message);
package/dist/client.d.ts CHANGED
@@ -22,9 +22,10 @@ export interface AdminNamespace {
22
22
  export declare class XmemoryClient {
23
23
  private readonly _baseUrl;
24
24
  private readonly _timeoutMs;
25
- private readonly _token;
25
+ private readonly _apiKey;
26
26
  readonly admin: AdminNamespace;
27
27
  constructor(options?: XmemoryClientOptions);
28
+ private static _resolveApiKey;
28
29
  /** Factory that performs a health check before returning the client. */
29
30
  static create(options?: XmemoryClientOptions): Promise<XmemoryClient>;
30
31
  /** Return an InstanceHandle scoped to the given instance ID. */
package/dist/client.js CHANGED
@@ -5,21 +5,41 @@ import { InstanceHandle } from "./instance.js";
5
5
  import { XmemoryAPIError, XmemoryHealthCheckError, buildInstanceSchema, } from "./types.js";
6
6
  const DEFAULT_BASE_URL = "https://api.xmemory.ai";
7
7
  const DEFAULT_TIMEOUT_MS = 60_000;
8
+ const ORANGE = "\x1b[38;5;208m";
9
+ const RESET = "\x1b[0m";
10
+ function deprecationWarning(message) {
11
+ console.warn(`${ORANGE}[xmemory] DEPRECATION: ${message}${RESET}`);
12
+ }
8
13
  // ---------------------------------------------------------------------------
9
14
  // Client
10
15
  // ---------------------------------------------------------------------------
11
16
  export class XmemoryClient {
12
17
  _baseUrl;
13
18
  _timeoutMs;
14
- _token;
19
+ _apiKey;
15
20
  admin;
16
21
  constructor(options = {}) {
17
22
  const env = typeof process !== "undefined" ? process.env : undefined;
18
23
  this._baseUrl = (options.url ?? env?.XMEM_API_URL ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
19
- this._token = options.token ?? env?.XMEM_AUTH_TOKEN;
24
+ this._apiKey = XmemoryClient._resolveApiKey(options, env);
20
25
  this._timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
21
26
  this.admin = this._buildAdmin();
22
27
  }
28
+ static _resolveApiKey(options, env) {
29
+ if (options.apiKey != null)
30
+ return options.apiKey;
31
+ if (options.token != null) {
32
+ deprecationWarning("The `token` constructor option is deprecated and will be removed soon. Use `apiKey` instead.");
33
+ return options.token;
34
+ }
35
+ if (env?.XMEM_API_KEY)
36
+ return env.XMEM_API_KEY;
37
+ if (env?.XMEM_AUTH_TOKEN) {
38
+ deprecationWarning("The `XMEM_AUTH_TOKEN` environment variable is deprecated and will be removed soon. Use `XMEM_API_KEY` instead.");
39
+ return env.XMEM_AUTH_TOKEN;
40
+ }
41
+ return undefined;
42
+ }
23
43
  /** Factory that performs a health check before returning the client. */
24
44
  static async create(options = {}) {
25
45
  const client = new XmemoryClient(options);
@@ -64,8 +84,8 @@ export class XmemoryClient {
64
84
  };
65
85
  if (hasBody)
66
86
  h["Content-Type"] = "application/json";
67
- if (this._token)
68
- h["Authorization"] = `Bearer ${this._token}`;
87
+ if (this._apiKey)
88
+ h["Authorization"] = `Bearer ${this._apiKey}`;
69
89
  return h;
70
90
  }
71
91
  /** Core request — returns the raw API wrapper `{ids, items, errors}`. */
package/dist/index.d.ts CHANGED
@@ -3,9 +3,9 @@
3
3
  */
4
4
  export { XmemoryClient, xmemoryInstance } from "./client.js";
5
5
  export type { AdminNamespace } from "./client.js";
6
- export { InstanceHandle } from "./instance.js";
6
+ export { DescribeResult, InstanceHandle } from "./instance.js";
7
7
  export { XmemoryAPIError, XmemoryHealthCheckError } from "./types.js";
8
8
  export { SchemaType } from "./types.js";
9
9
  export type { SchemaTypeValue, ExtractionLogic, ReadMode, WriteQueueStatus } from "./types.js";
10
10
  export type { XmemoryClientOptions, RequestOptions, ReadOptions, WriteOptions, ExtractOptions, CreateInstanceOptions, GenerateSchemaOptions, } from "./types.js";
11
- export type { ClusterInfo, InstanceInfo, InstanceSchemaInfo, ReadResult, WriteResult, AsyncWriteResult, WriteStatusResult, ExtractResult, GenerateSchemaResult, } from "./types.js";
11
+ export type { ClusterInfo, InstanceInfo, InstanceSchemaInfo, ReadResult, WriteResult, AsyncWriteResult, WriteStatusResult, ExtractResult, GenerateSchemaResult, ToolDescription, ToolParameterDescription, RawDescribeResult, } from "./types.js";
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@
4
4
  // Client
5
5
  export { XmemoryClient, xmemoryInstance } from "./client.js";
6
6
  // Instance handle
7
- export { InstanceHandle } from "./instance.js";
7
+ export { DescribeResult, InstanceHandle } from "./instance.js";
8
8
  // Error classes
9
9
  export { XmemoryAPIError, XmemoryHealthCheckError } from "./types.js";
10
10
  // Enums
@@ -1,10 +1,34 @@
1
1
  /**
2
2
  * InstanceHandle — scoped data operations on a single xmemory instance.
3
3
  */
4
- import type { AsyncWriteResult, ExtractOptions, ExtractResult, InstanceSchemaInfo, ReadOptions, ReadResult, RequestOneFn, RequestOptions, WriteOptions, WriteResult, WriteStatusResult } from "./types.js";
4
+ import type { AsyncWriteResult, ExtractOptions, ExtractResult, InstanceSchemaInfo, RawDescribeResult, ReadOptions, ReadResult, RequestOneFn, RequestOptions, ToolDescription, WriteOptions, WriteResult, WriteStatusResult } from "./types.js";
5
+ export declare class DescribeResult {
6
+ readonly instanceId: string;
7
+ readonly instanceName: string;
8
+ readonly schemaSummary: string;
9
+ readonly tools: readonly ToolDescription[];
10
+ constructor(raw: RawDescribeResult);
11
+ /**
12
+ * Plain-text representation suitable for injecting into an LLM system prompt.
13
+ *
14
+ * By default, tools are presented as method calls (matching the SDK).
15
+ * Set `includeHttp` to `true` to also show HTTP method and path for
16
+ * raw REST callers.
17
+ */
18
+ asText(options?: {
19
+ includeHttp?: boolean;
20
+ }): string;
21
+ /** Tool definitions in the Anthropic tool-use format. */
22
+ asAnthropicTools(): Record<string, unknown>[];
23
+ /** Tool definitions in the OpenAI function-calling format. */
24
+ asOpenaiTools(): Record<string, unknown>[];
25
+ }
5
26
  export declare class InstanceHandle {
6
27
  readonly id: string;
7
28
  private readonly _requestOne;
29
+ private _describeCache;
30
+ private _describeCacheAt;
31
+ private _describeTtlMs;
8
32
  constructor(id: string, requestOne: RequestOneFn);
9
33
  read(query: string, options?: ReadOptions): Promise<ReadResult>;
10
34
  write(text: string, options?: WriteOptions): Promise<WriteResult>;
@@ -12,4 +36,13 @@ export declare class InstanceHandle {
12
36
  writeStatus(writeId: string, options?: RequestOptions): Promise<WriteStatusResult>;
13
37
  extract(text: string, options?: ExtractOptions): Promise<ExtractResult>;
14
38
  getSchema(options?: RequestOptions): Promise<InstanceSchemaInfo>;
39
+ /**
40
+ * Return agent-facing tool descriptions enriched with the instance schema.
41
+ *
42
+ * Results are cached locally with a TTL (default 5 min).
43
+ * Call `clearDescribeCache()` to force a refresh.
44
+ */
45
+ describe(options?: RequestOptions): Promise<DescribeResult>;
46
+ /** Clear the cached describe result so the next `describe()` call fetches fresh data. */
47
+ clearDescribeCache(): void;
15
48
  }
package/dist/instance.js CHANGED
@@ -1,9 +1,105 @@
1
1
  /**
2
2
  * InstanceHandle — scoped data operations on a single xmemory instance.
3
3
  */
4
+ const DEFAULT_DESCRIBE_TTL_MS = 300_000; // 5 minutes
5
+ export class DescribeResult {
6
+ instanceId;
7
+ instanceName;
8
+ schemaSummary;
9
+ tools;
10
+ constructor(raw) {
11
+ this.instanceId = raw.instance_id;
12
+ this.instanceName = raw.instance_name;
13
+ this.schemaSummary = raw.schema_summary;
14
+ this.tools = raw.tools;
15
+ }
16
+ /**
17
+ * Plain-text representation suitable for injecting into an LLM system prompt.
18
+ *
19
+ * By default, tools are presented as method calls (matching the SDK).
20
+ * Set `includeHttp` to `true` to also show HTTP method and path for
21
+ * raw REST callers.
22
+ */
23
+ asText(options) {
24
+ const includeHttp = options?.includeHttp ?? false;
25
+ const lines = [];
26
+ lines.push(`Instance: ${this.instanceName} (${this.instanceId})`);
27
+ if (this.schemaSummary) {
28
+ lines.push(`\n${this.schemaSummary}`);
29
+ }
30
+ lines.push("\nAvailable tools:\n");
31
+ for (const tool of this.tools) {
32
+ const paramsSig = tool.parameters
33
+ .map((p) => p.name + (p.required ? "" : "?"))
34
+ .join(", ");
35
+ lines.push(`## ${tool.name}(${paramsSig})`);
36
+ lines.push(tool.description);
37
+ lines.push(`When to use: ${tool.when_to_use}`);
38
+ if (includeHttp) {
39
+ lines.push(`HTTP: ${tool.http_method} ${tool.http_path}`);
40
+ }
41
+ if (tool.parameters.length > 0) {
42
+ lines.push("Parameters:");
43
+ for (const p of tool.parameters) {
44
+ const req = p.required ? "required" : "optional";
45
+ lines.push(` - ${p.name} (${p.type}, ${req}): ${p.description}`);
46
+ if (p.enum) {
47
+ lines.push(` Allowed values: ${p.enum.join(", ")}`);
48
+ }
49
+ if (p.default != null) {
50
+ lines.push(` Default: ${p.default}`);
51
+ }
52
+ }
53
+ }
54
+ lines.push("");
55
+ }
56
+ return lines.join("\n");
57
+ }
58
+ /** Tool definitions in the Anthropic tool-use format. */
59
+ asAnthropicTools() {
60
+ return this.tools.map((tool) => {
61
+ const { properties, required } = buildJsonSchemaProps(tool.parameters);
62
+ return {
63
+ name: tool.name,
64
+ description: `${tool.description}\n\nWhen to use: ${tool.when_to_use}`,
65
+ input_schema: { type: "object", properties, required },
66
+ };
67
+ });
68
+ }
69
+ /** Tool definitions in the OpenAI function-calling format. */
70
+ asOpenaiTools() {
71
+ return this.tools.map((tool) => {
72
+ const { properties, required } = buildJsonSchemaProps(tool.parameters);
73
+ return {
74
+ type: "function",
75
+ function: {
76
+ name: tool.name,
77
+ description: `${tool.description}\n\nWhen to use: ${tool.when_to_use}`,
78
+ parameters: { type: "object", properties, required },
79
+ },
80
+ };
81
+ });
82
+ }
83
+ }
84
+ function buildJsonSchemaProps(params) {
85
+ const properties = {};
86
+ const required = [];
87
+ for (const p of params) {
88
+ const prop = { type: p.type, description: p.description };
89
+ if (p.enum)
90
+ prop.enum = p.enum;
91
+ properties[p.name] = prop;
92
+ if (p.required)
93
+ required.push(p.name);
94
+ }
95
+ return { properties, required };
96
+ }
4
97
  export class InstanceHandle {
5
98
  id;
6
99
  _requestOne;
100
+ _describeCache = null;
101
+ _describeCacheAt = 0;
102
+ _describeTtlMs = DEFAULT_DESCRIBE_TTL_MS;
7
103
  constructor(id, requestOne) {
8
104
  this.id = id;
9
105
  this._requestOne = requestOne;
@@ -65,4 +161,28 @@ export class InstanceHandle {
65
161
  timeoutMs: options?.timeoutMs,
66
162
  });
67
163
  }
164
+ /**
165
+ * Return agent-facing tool descriptions enriched with the instance schema.
166
+ *
167
+ * Results are cached locally with a TTL (default 5 min).
168
+ * Call `clearDescribeCache()` to force a refresh.
169
+ */
170
+ async describe(options) {
171
+ const now = Date.now();
172
+ if (this._describeCache && now - this._describeCacheAt < this._describeTtlMs) {
173
+ return this._describeCache;
174
+ }
175
+ const raw = await this._requestOne("GET", `/instances/${this.id}/describe`, {
176
+ timeoutMs: options?.timeoutMs,
177
+ });
178
+ const result = new DescribeResult(raw);
179
+ this._describeCache = result;
180
+ this._describeCacheAt = now;
181
+ return result;
182
+ }
183
+ /** Clear the cached describe result so the next `describe()` call fetches fresh data. */
184
+ clearDescribeCache() {
185
+ this._describeCache = null;
186
+ this._describeCacheAt = 0;
187
+ }
68
188
  }
package/dist/types.d.ts CHANGED
@@ -58,8 +58,32 @@ export interface ExtractResult {
58
58
  export interface GenerateSchemaResult {
59
59
  readonly data_schema: Record<string, unknown>;
60
60
  }
61
+ export interface ToolParameterDescription {
62
+ readonly name: string;
63
+ readonly type: string;
64
+ readonly description: string;
65
+ readonly required: boolean;
66
+ readonly enum?: string[];
67
+ readonly default?: string;
68
+ }
69
+ export interface ToolDescription {
70
+ readonly name: string;
71
+ readonly description: string;
72
+ readonly when_to_use: string;
73
+ readonly parameters: ToolParameterDescription[];
74
+ readonly http_method: string;
75
+ readonly http_path: string;
76
+ }
77
+ export interface RawDescribeResult {
78
+ readonly instance_id: string;
79
+ readonly instance_name: string;
80
+ readonly schema_summary: string;
81
+ readonly tools: ToolDescription[];
82
+ }
61
83
  export interface XmemoryClientOptions {
62
84
  url?: string;
85
+ apiKey?: string;
86
+ /** @deprecated Use `apiKey` instead. Will be removed in a future release. */
63
87
  token?: string;
64
88
  timeoutMs?: number;
65
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xmemory",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "xmemory",
5
5
  "keywords": [
6
6
  "xmemory"