hammer-ai 0.2.2 → 0.2.4

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/dist/node.js CHANGED
@@ -1,4 +1,4 @@
1
- import { encoding_for_model } from 'tiktoken';
1
+ import { encodingForModel } from 'js-tiktoken';
2
2
  import { mkdir, writeFile, rename } from 'fs/promises';
3
3
  import { dirname } from 'path';
4
4
 
@@ -7,10 +7,10 @@ var TiktokenEstimator = class {
7
7
  encoder;
8
8
  constructor() {
9
9
  try {
10
- this.encoder = encoding_for_model("gpt-4");
10
+ this.encoder = encodingForModel("gpt-4");
11
11
  } catch (err) {
12
12
  throw new Error(
13
- `Failed to initialize tiktoken encoder (WASM may not be available in this environment): ${err instanceof Error ? err.message : err}`
13
+ `Failed to initialize tiktoken encoder: ${err instanceof Error ? err.message : err}`
14
14
  );
15
15
  }
16
16
  }
@@ -19,10 +19,7 @@ var TiktokenEstimator = class {
19
19
  return this.encoder.encode(text).length;
20
20
  }
21
21
  dispose() {
22
- if (this.encoder) {
23
- this.encoder.free();
24
- this.encoder = null;
25
- }
22
+ this.encoder = null;
26
23
  }
27
24
  };
28
25
  async function atomicWriteJSON(filePath, data) {
package/dist/node.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/tiktoken-estimator.ts","../src/conversation-history-writer.ts"],"names":[],"mappings":";;;;;AAWO,IAAM,oBAAN,MAAkD;AAAA,EAC7C,OAAA;AAAA,EAER,WAAA,GAAc;AACV,IAAA,IAAI;AACA,MAAA,IAAA,CAAK,OAAA,GAAU,mBAAmB,OAAO,CAAA;AAAA,IAC7C,SAAS,GAAA,EAAK;AACV,MAAA,MAAM,IAAI,KAAA;AAAA,QACN,CAAA,uFAAA,EAA0F,GAAA,YAAe,KAAA,GAAQ,GAAA,CAAI,UAAU,GAAG,CAAA;AAAA,OACtI;AAAA,IACJ;AAAA,EACJ;AAAA,EAEA,eAAe,IAAA,EAAsB;AACjC,IAAA,IAAI,CAAC,IAAA,CAAK,OAAA,EAAS,MAAM,IAAI,MAAM,qCAAqC,CAAA;AACxE,IAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,MAAA,CAAO,IAAI,CAAA,CAAE,MAAA;AAAA,EACrC;AAAA,EAEA,OAAA,GAAgB;AACZ,IAAA,IAAI,KAAK,OAAA,EAAS;AACd,MAAA,IAAA,CAAK,QAAQ,IAAA,EAAK;AAClB,MAAA,IAAA,CAAK,OAAA,GAAU,IAAA;AAAA,IACnB;AAAA,EACJ;AACJ;ACDA,eAAsB,eAAA,CAClB,UACA,IAAA,EACa;AACb,EAAA,MAAM,MAAM,OAAA,CAAQ,QAAQ,GAAG,EAAE,SAAA,EAAW,MAAM,CAAA;AAClD,EAAA,MAAM,UAAU,QAAA,GAAW,MAAA;AAC3B,EAAA,MAAM,SAAA,CAAU,SAAS,IAAA,CAAK,SAAA,CAAU,MAAM,IAAA,EAAM,CAAC,GAAG,OAAO,CAAA;AAC/D,EAAA,MAAM,MAAA,CAAO,SAAS,QAAQ,CAAA;AAClC;AAuBO,SAAS,0BACZ,QAAA,EAC0B;AAC1B,EAAA,OAAO,QAAA,CAAS,GAAA,CAAI,CAAC,CAAA,MAAO;AAAA,IACxB,MAAM,CAAA,CAAE,IAAA;AAAA,IACR,MAAM,CAAA,CAAE,IAAA;AAAA,IACR,SAAS,CAAA,CAAE,OAAA;AAAA,IACX,WAAW,CAAA,CAAE,SAAA;AAAA,IACb,GAAI,EAAE,EAAA,GAAK,EAAE,IAAI,CAAA,CAAE,EAAA,KAAO,EAAC;AAAA,IAC3B,GAAI,EAAE,UAAA,GAAa,EAAE,YAAY,CAAA,CAAE,UAAA,KAAe;AAAC,GACvD,CAAE,CAAA;AACN","file":"node.js","sourcesContent":["/**\n * Precise token estimator using tiktoken (cl100k_base encoding).\n *\n * Separated into its own module so environments can opt into the tiktoken WASM\n * dependency explicitly. Browser runtimes should reach it through async\n * estimator helpers instead of importing it from hot paths.\n */\n\nimport { encoding_for_model, type Tiktoken } from \"tiktoken\"\nimport type { TokenEstimator } from \"./memory-layer\"\n\nexport class TiktokenEstimator implements TokenEstimator {\n private encoder: Tiktoken | null\n\n constructor() {\n try {\n this.encoder = encoding_for_model(\"gpt-4\")\n } catch (err) {\n throw new Error(\n `Failed to initialize tiktoken encoder (WASM may not be available in this environment): ${err instanceof Error ? err.message : err}`,\n )\n }\n }\n\n estimateTokens(text: string): number {\n if (!this.encoder) throw new Error(\"TiktokenEstimator has been disposed\")\n return this.encoder.encode(text).length\n }\n\n dispose(): void {\n if (this.encoder) {\n this.encoder.free()\n this.encoder = null\n }\n }\n}\n","/**\n * Shared conversation history utilities.\n *\n * Provides:\n * - `atomicWriteJSON()` — crash-safe file writing (tmp + rename)\n * - `ConversationHistoryEntry` — debug-friendly message format\n * - `formatConversationHistory()` — converts MemoryMessages to debug entries\n *\n * Used by:\n * - Voice agent (Bun server writes conversation_history.json alongside voice-memory.json)\n * - Hammer agent (ConversationManager, UnifiedAgent)\n *\n * NOTE: Server-only module (requires Node/Bun fs). Not imported by browser/mobile code.\n */\n\n/// <reference types=\"node\" />\n\nimport { writeFile, rename, mkdir } from \"fs/promises\"\nimport { dirname } from \"path\"\nimport type { MemoryMessage } from \"./memory-layer\"\n\n// ============================================================================\n// Atomic File Writing\n// ============================================================================\n\n/**\n * Atomically write JSON data to a file using tmp + rename.\n *\n * 1. Creates parent directories if needed\n * 2. Writes to `filePath.tmp`\n * 3. Renames to `filePath` (atomic on most filesystems)\n *\n * Prevents data corruption if the process crashes mid-write.\n */\nexport async function atomicWriteJSON(\n filePath: string,\n data: unknown,\n): Promise<void> {\n await mkdir(dirname(filePath), { recursive: true })\n const tmpFile = filePath + \".tmp\"\n await writeFile(tmpFile, JSON.stringify(data, null, 2), \"utf-8\")\n await rename(tmpFile, filePath)\n}\n\n// ============================================================================\n// Conversation History\n// ============================================================================\n\n/** Debug-friendly conversation history entry. */\nexport interface ConversationHistoryEntry {\n turn: number\n role: string\n content: string\n timestamp: number\n id?: string\n tokenCount?: number\n}\n\n/**\n * Convert raw MemoryMessages to a flat, debug-friendly array.\n *\n * The output is designed for easy reading in a JSON viewer — no nested\n * compressed-state or cursor objects. Each entry is one message with\n * its turn number, role, content, and timestamp.\n */\nexport function formatConversationHistory(\n messages: MemoryMessage[],\n): ConversationHistoryEntry[] {\n return messages.map((m) => ({\n turn: m.turn,\n role: m.role,\n content: m.content,\n timestamp: m.timestamp,\n ...(m.id ? { id: m.id } : {}),\n ...(m.tokenCount ? { tokenCount: m.tokenCount } : {}),\n }))\n}\n"]}
1
+ {"version":3,"sources":["../src/tiktoken-estimator.ts","../src/conversation-history-writer.ts"],"names":[],"mappings":";;;;;AAaO,IAAM,oBAAN,MAAkD;AAAA,EAC7C,OAAA;AAAA,EAER,WAAA,GAAc;AACV,IAAA,IAAI;AACA,MAAA,IAAA,CAAK,OAAA,GAAU,iBAAiB,OAAO,CAAA;AAAA,IAC3C,SAAS,GAAA,EAAK;AACV,MAAA,MAAM,IAAI,KAAA;AAAA,QACN,CAAA,uCAAA,EAA0C,GAAA,YAAe,KAAA,GAAQ,GAAA,CAAI,UAAU,GAAG,CAAA;AAAA,OACtF;AAAA,IACJ;AAAA,EACJ;AAAA,EAEA,eAAe,IAAA,EAAsB;AACjC,IAAA,IAAI,CAAC,IAAA,CAAK,OAAA,EAAS,MAAM,IAAI,MAAM,qCAAqC,CAAA;AACxE,IAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,MAAA,CAAO,IAAI,CAAA,CAAE,MAAA;AAAA,EACrC;AAAA,EAEA,OAAA,GAAgB;AACZ,IAAA,IAAA,CAAK,OAAA,GAAU,IAAA;AAAA,EACnB;AACJ;ACAA,eAAsB,eAAA,CAClB,UACA,IAAA,EACa;AACb,EAAA,MAAM,MAAM,OAAA,CAAQ,QAAQ,GAAG,EAAE,SAAA,EAAW,MAAM,CAAA;AAClD,EAAA,MAAM,UAAU,QAAA,GAAW,MAAA;AAC3B,EAAA,MAAM,SAAA,CAAU,SAAS,IAAA,CAAK,SAAA,CAAU,MAAM,IAAA,EAAM,CAAC,GAAG,OAAO,CAAA;AAC/D,EAAA,MAAM,MAAA,CAAO,SAAS,QAAQ,CAAA;AAClC;AAuBO,SAAS,0BACZ,QAAA,EAC0B;AAC1B,EAAA,OAAO,QAAA,CAAS,GAAA,CAAI,CAAC,CAAA,MAAO;AAAA,IACxB,MAAM,CAAA,CAAE,IAAA;AAAA,IACR,MAAM,CAAA,CAAE,IAAA;AAAA,IACR,SAAS,CAAA,CAAE,OAAA;AAAA,IACX,WAAW,CAAA,CAAE,SAAA;AAAA,IACb,GAAI,EAAE,EAAA,GAAK,EAAE,IAAI,CAAA,CAAE,EAAA,KAAO,EAAC;AAAA,IAC3B,GAAI,EAAE,UAAA,GAAa,EAAE,YAAY,CAAA,CAAE,UAAA,KAAe;AAAC,GACvD,CAAE,CAAA;AACN","file":"node.js","sourcesContent":["/**\n * Precise token estimator using tiktoken (cl100k_base encoding).\n *\n * Separated into its own module so environments can opt into the tiktoken WASM\n * dependency explicitly. Browser runtimes should reach it through async\n * estimator helpers instead of importing it from hot paths.\n */\n\nimport { encodingForModel } from \"js-tiktoken\"\nimport type { TokenEstimator } from \"./memory-layer\"\n\ntype JsTiktoken = ReturnType<typeof encodingForModel>\n\nexport class TiktokenEstimator implements TokenEstimator {\n private encoder: JsTiktoken | null\n\n constructor() {\n try {\n this.encoder = encodingForModel(\"gpt-4\")\n } catch (err) {\n throw new Error(\n `Failed to initialize tiktoken encoder: ${err instanceof Error ? err.message : err}`,\n )\n }\n }\n\n estimateTokens(text: string): number {\n if (!this.encoder) throw new Error(\"TiktokenEstimator has been disposed\")\n return this.encoder.encode(text).length\n }\n\n dispose(): void {\n this.encoder = null\n }\n}\n","/**\n * Shared conversation history utilities.\n *\n * Provides:\n * - `atomicWriteJSON()` — crash-safe file writing (tmp + rename)\n * - `ConversationHistoryEntry` — debug-friendly message format\n * - `formatConversationHistory()` — converts MemoryMessages to debug entries\n *\n * Used by:\n * - Voice agent (Bun server writes conversation_history.json alongside voice-memory.json)\n * - Hammer agent (ConversationManager, UnifiedAgent)\n *\n * NOTE: Server-only module (requires Node/Bun fs). Not imported by browser/mobile code.\n */\n\n/// <reference types=\"node\" />\n\nimport { writeFile, rename, mkdir } from \"fs/promises\"\nimport { dirname } from \"path\"\nimport type { MemoryMessage } from \"./memory-layer\"\n\n// ============================================================================\n// Atomic File Writing\n// ============================================================================\n\n/**\n * Atomically write JSON data to a file using tmp + rename.\n *\n * 1. Creates parent directories if needed\n * 2. Writes to `filePath.tmp`\n * 3. Renames to `filePath` (atomic on most filesystems)\n *\n * Prevents data corruption if the process crashes mid-write.\n */\nexport async function atomicWriteJSON(\n filePath: string,\n data: unknown,\n): Promise<void> {\n await mkdir(dirname(filePath), { recursive: true })\n const tmpFile = filePath + \".tmp\"\n await writeFile(tmpFile, JSON.stringify(data, null, 2), \"utf-8\")\n await rename(tmpFile, filePath)\n}\n\n// ============================================================================\n// Conversation History\n// ============================================================================\n\n/** Debug-friendly conversation history entry. */\nexport interface ConversationHistoryEntry {\n turn: number\n role: string\n content: string\n timestamp: number\n id?: string\n tokenCount?: number\n}\n\n/**\n * Convert raw MemoryMessages to a flat, debug-friendly array.\n *\n * The output is designed for easy reading in a JSON viewer — no nested\n * compressed-state or cursor objects. Each entry is one message with\n * its turn number, role, content, and timestamp.\n */\nexport function formatConversationHistory(\n messages: MemoryMessage[],\n): ConversationHistoryEntry[] {\n return messages.map((m) => ({\n turn: m.turn,\n role: m.role,\n content: m.content,\n timestamp: m.timestamp,\n ...(m.id ? { id: m.id } : {}),\n ...(m.tokenCount ? { tokenCount: m.tokenCount } : {}),\n }))\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hammer-ai",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Infrastructure for building tool-calling bash-format chat agents: LLM client, agent loop, memory, tool registry, validation, and web runtime.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -39,21 +39,13 @@
39
39
  "author": "",
40
40
  "license": "MIT",
41
41
  "dependencies": {
42
+ "js-tiktoken": "^1.0.21",
42
43
  "xstate": "5",
43
44
  "zod": "^3.25.76"
44
45
  },
45
46
  "devDependencies": {
46
47
  "@types/node": "^22.0.0",
47
- "tiktoken": "^1.0.22",
48
48
  "tsup": "^8.4.0",
49
49
  "typescript": "^5.9.0"
50
- },
51
- "peerDependencies": {
52
- "tiktoken": "^1.0.22"
53
- },
54
- "peerDependenciesMeta": {
55
- "tiktoken": {
56
- "optional": true
57
- }
58
50
  }
59
51
  }