@warpmetrics/warp 0.0.2 → 0.0.7

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Warpmetrics
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -7,14 +7,14 @@ Warp is a lightweight SDK that wraps your existing OpenAI or Anthropic client an
7
7
  ## Install
8
8
 
9
9
  ```bash
10
- npm install warpmetrics
10
+ npm install @warpmetrics/warp
11
11
  ```
12
12
 
13
13
  ## Quick start
14
14
 
15
15
  ```js
16
16
  import OpenAI from 'openai';
17
- import { warp, run, group, add, outcome } from 'warpmetrics';
17
+ import { warp, run, group, add, outcome } from '@warpmetrics/warp';
18
18
 
19
19
  const openai = warp(new OpenAI(), { apiKey: 'wm_...' });
20
20
 
@@ -100,9 +100,9 @@ outcome(r, 'completed', {
100
100
  Resolve any target (run, group, or LLM response) to its string ID. Useful for passing IDs to your frontend or storing them.
101
101
 
102
102
  ```js
103
- ref(r) // 'wm_run_a1b2c3d4e5f6'
104
- ref(response) // 'wm_call_x9y8z7w6v5u4'
105
- ref('wm_run_abc') // pass-through
103
+ ref(r) // 'wm_run_01jkx3ndek0gh4r5tmqp9a3bcv'
104
+ ref(response) // 'wm_call_01jkx3ndef8mn2q7kpvhc4e9ws'
105
+ ref('wm_run_01jkx3ndek0gh4r5tmqp9a3bcv') // pass-through
106
106
  ```
107
107
 
108
108
  ### `cost(target)`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@warpmetrics/warp",
3
- "version": "0.0.2",
3
+ "version": "0.0.7",
4
4
  "description": "Measure your agents, not your LLM calls.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -11,7 +11,8 @@
11
11
  }
12
12
  },
13
13
  "files": [
14
- "src"
14
+ "src",
15
+ "!src/**/*.test.js"
15
16
  ],
16
17
  "scripts": {
17
18
  "test": "vitest run",
@@ -35,7 +36,7 @@
35
36
  "license": "MIT",
36
37
  "repository": {
37
38
  "type": "git",
38
- "url": "https://github.com/nikolaionken/warpmetrics"
39
+ "url": "https://github.com/warpmetrics/warp"
39
40
  },
40
41
  "dependencies": {
41
42
  "ulid": "^3.0.2"
@@ -1,41 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { createRequire } from 'module';
3
- import { run, flush } from '../index.js';
4
- import { setupBeforeEach } from '../../test/setup.js';
5
-
6
- const { version } = createRequire(import.meta.url)('../../package.json');
7
-
8
- setupBeforeEach();
9
-
10
- describe('transport', () => {
11
- it('sends batched events with correct auth header', async () => {
12
- run('test');
13
- await flush();
14
-
15
- expect(global.fetch).toHaveBeenCalledTimes(1);
16
- const [url, opts] = global.fetch.mock.calls[0];
17
- expect(url).toBe('https://api.warpmetrics.com/v1/events');
18
- expect(opts.headers['Authorization']).toBe('Bearer wm_test_123');
19
- expect(opts.headers['X-SDK-Version']).toBe(version);
20
- });
21
-
22
- it('re-queues events on failure and succeeds on retry', async () => {
23
- global.fetch.mockRejectedValueOnce(new Error('Network error'));
24
-
25
- run('test');
26
- await flush();
27
- expect(global.fetch).toHaveBeenCalledTimes(1);
28
-
29
- global.fetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({}) });
30
- await flush();
31
- expect(global.fetch).toHaveBeenCalledTimes(2);
32
-
33
- const body = JSON.parse(global.fetch.mock.calls[1][1].body);
34
- expect(body.runs).toHaveLength(1);
35
- });
36
-
37
- it('skips flush when queue is empty', async () => {
38
- await flush();
39
- expect(global.fetch).not.toHaveBeenCalled();
40
- });
41
- });
@@ -1,101 +0,0 @@
1
- import { describe, it, expect, vi } from 'vitest';
2
- import { warp, flush } from '../index.js';
3
- import { responseRegistry, costRegistry } from '../core/registry.js';
4
- import {
5
- setupBeforeEach,
6
- createMockOpenAI, createMockAnthropic,
7
- OPENAI_RESPONSE, OPENAI_RESPONSES_API_RESPONSE, ANTHROPIC_RESPONSE,
8
- } from '../../test/setup.js';
9
-
10
- setupBeforeEach();
11
-
12
- describe('warp() — OpenAI', () => {
13
- it('intercepts chat.completions.create and tracks the call', async () => {
14
- const client = createMockOpenAI(OPENAI_RESPONSE);
15
- const wrapped = warp(client);
16
-
17
- const result = await wrapped.chat.completions.create({
18
- model: 'gpt-4o-mini',
19
- messages: [{ role: 'user', content: 'Hi' }],
20
- });
21
-
22
- expect(result.choices[0].message.content).toBe('Hello!');
23
- expect(responseRegistry.has(result)).toBe(true);
24
- expect(costRegistry.has(result)).toBe(true);
25
-
26
- await flush();
27
- const body = JSON.parse(global.fetch.mock.calls[0][1].body);
28
- expect(body.calls).toHaveLength(1);
29
- expect(body.calls[0].model).toBe('gpt-4o-mini');
30
- expect(body.calls[0].tokens.total).toBe(15);
31
- expect(body.calls[0].status).toBe('success');
32
- });
33
-
34
- it('tracks errors without swallowing them', async () => {
35
- const error = new Error('Rate limit exceeded');
36
- const client = createMockOpenAI(null);
37
- client.chat.completions.create = vi.fn().mockRejectedValue(error);
38
- const wrapped = warp(client);
39
-
40
- await expect(
41
- wrapped.chat.completions.create({ model: 'gpt-4o', messages: [] })
42
- ).rejects.toThrow('Rate limit exceeded');
43
-
44
- await flush();
45
- const body = JSON.parse(global.fetch.mock.calls[0][1].body);
46
- expect(body.calls).toHaveLength(1);
47
- expect(body.calls[0].status).toBe('error');
48
- expect(body.calls[0].error).toBe('Rate limit exceeded');
49
- });
50
-
51
- it('accepts config options on first call', () => {
52
- const client = createMockOpenAI(OPENAI_RESPONSE);
53
- warp(client, { apiKey: 'wm_live_xyz', debug: true });
54
- });
55
- });
56
-
57
- describe('warp() — OpenAI Responses API', () => {
58
- it('intercepts responses.create and tracks the call', async () => {
59
- const client = createMockOpenAI(OPENAI_RESPONSE, OPENAI_RESPONSES_API_RESPONSE);
60
- const wrapped = warp(client);
61
-
62
- const result = await wrapped.responses.create({
63
- model: 'gpt-4o',
64
- input: 'Hi',
65
- });
66
-
67
- expect(result.output_text).toBe('Hello from Responses API!');
68
- expect(responseRegistry.has(result)).toBe(true);
69
- expect(costRegistry.has(result)).toBe(true);
70
-
71
- await flush();
72
- const body = JSON.parse(global.fetch.mock.calls[0][1].body);
73
- expect(body.calls).toHaveLength(1);
74
- expect(body.calls[0].model).toBe('gpt-4o');
75
- expect(body.calls[0].tokens.prompt).toBe(8);
76
- expect(body.calls[0].tokens.completion).toBe(6);
77
- expect(body.calls[0].status).toBe('success');
78
- });
79
- });
80
-
81
- describe('warp() — Anthropic', () => {
82
- it('intercepts messages.create and tracks the call', async () => {
83
- const client = createMockAnthropic(ANTHROPIC_RESPONSE);
84
- const wrapped = warp(client);
85
-
86
- const result = await wrapped.messages.create({
87
- model: 'claude-3-5-sonnet-latest',
88
- messages: [{ role: 'user', content: 'Hi' }],
89
- });
90
-
91
- expect(result.content[0].text).toBe('Hello from Claude!');
92
- expect(responseRegistry.has(result)).toBe(true);
93
-
94
- await flush();
95
- const body = JSON.parse(global.fetch.mock.calls[0][1].body);
96
- expect(body.calls).toHaveLength(1);
97
- expect(body.calls[0].provider).toBe('anthropic');
98
- expect(body.calls[0].tokens.prompt).toBe(12);
99
- expect(body.calls[0].tokens.completion).toBe(8);
100
- });
101
- });
package/src/e2e.test.js DELETED
@@ -1,41 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { warp, run, group, add, outcome, ref, cost, flush } from './index.js';
3
- import { setupBeforeEach, createMockOpenAI, OPENAI_RESPONSE } from '../test/setup.js';
4
-
5
- setupBeforeEach();
6
-
7
- describe('end-to-end', () => {
8
- it('full agent flow: warp -> run -> group -> add -> outcome -> flush', async () => {
9
- const client = createMockOpenAI(OPENAI_RESPONSE);
10
- const openai = warp(client);
11
-
12
- const r = run('code-review', { link: 'ticket:123', name: 'Review PR' });
13
- const planning = group('planning', { name: 'Plan Phase' });
14
-
15
- const response = await openai.chat.completions.create({
16
- model: 'gpt-4o-mini',
17
- messages: [{ role: 'user', content: 'Plan a review' }],
18
- });
19
-
20
- add(planning, response);
21
- add(r, planning);
22
- outcome(r, 'completed', { reason: 'Looks good', source: 'ci' });
23
-
24
- expect(ref(r)).toBe(r.id);
25
- expect(ref(planning)).toBe(planning.id);
26
- expect(ref(response)).toMatch(/^wm_call_/);
27
- expect(cost(r)).toBeGreaterThan(0);
28
- expect(cost(response)).toBeGreaterThan(0);
29
- expect(cost(r)).toEqual(cost(response));
30
-
31
- await flush();
32
-
33
- const body = JSON.parse(global.fetch.mock.calls[0][1].body);
34
- expect(body.runs).toHaveLength(1);
35
- expect(body.groups).toHaveLength(1);
36
- expect(body.calls).toHaveLength(1);
37
- expect(body.links).toHaveLength(2);
38
- expect(body.outcomes).toHaveLength(1);
39
- expect(body.outcomes[0].name).toBe('completed');
40
- });
41
- });
@@ -1,65 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { warp, run, group, add, flush } from '../index.js';
3
- import { runRegistry, groupRegistry } from '../core/registry.js';
4
- import { setupBeforeEach, createMockOpenAI, OPENAI_RESPONSE } from '../../test/setup.js';
5
-
6
- setupBeforeEach();
7
-
8
- describe('add()', () => {
9
- it('links a group to a run', async () => {
10
- const r = run('test');
11
- const g = group('step');
12
- add(r, g);
13
-
14
- const runData = runRegistry.get(r.id);
15
- expect(runData.groups).toContain(g.id);
16
-
17
- const groupData = groupRegistry.get(g.id);
18
- expect(groupData.parentId).toBe(r.id);
19
-
20
- await flush();
21
- const body = JSON.parse(global.fetch.mock.calls[0][1].body);
22
- const link = body.links.find(l => l.childId === g.id);
23
- expect(link).toBeDefined();
24
- expect(link.parentId).toBe(r.id);
25
- expect(link.type).toBe('group');
26
- });
27
-
28
- it('links an LLM response to a group', async () => {
29
- const client = createMockOpenAI(OPENAI_RESPONSE);
30
- const wrapped = warp(client);
31
- const response = await wrapped.chat.completions.create({ model: 'gpt-4o-mini', messages: [] });
32
-
33
- const g = group('step');
34
- add(g, response);
35
-
36
- const groupData = groupRegistry.get(g.id);
37
- expect(groupData.calls).toHaveLength(1);
38
- expect(groupData.calls[0]).toMatch(/^wm_call_/);
39
- });
40
-
41
- it('accepts multiple items at once', () => {
42
- const r = run('test');
43
- const g1 = group('a');
44
- const g2 = group('b');
45
- add(r, g1, g2);
46
-
47
- const data = runRegistry.get(r.id);
48
- expect(data.groups).toHaveLength(2);
49
- });
50
-
51
- it('nests groups inside groups', () => {
52
- const r = run('test');
53
- const parent = group('outer');
54
- const child = group('inner');
55
-
56
- add(parent, child);
57
- add(r, parent);
58
-
59
- const parentData = groupRegistry.get(parent.id);
60
- expect(parentData.groups).toContain(child.id);
61
-
62
- const childData = groupRegistry.get(child.id);
63
- expect(childData.parentId).toBe(parent.id);
64
- });
65
- });
@@ -1,42 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { warp, run, group, add, cost } from '../index.js';
3
- import { setupBeforeEach, createMockOpenAI, OPENAI_RESPONSE } from '../../test/setup.js';
4
-
5
- setupBeforeEach();
6
-
7
- describe('cost()', () => {
8
- it('returns cost for an LLM response object', async () => {
9
- const client = createMockOpenAI(OPENAI_RESPONSE);
10
- const wrapped = warp(client);
11
- const response = await wrapped.chat.completions.create({ model: 'gpt-4o-mini', messages: [] });
12
-
13
- const c = cost(response);
14
- expect(c).toBeGreaterThan(0);
15
- // gpt-4o-mini: prompt=$0.15/1M, completion=$0.60/1M
16
- // 10 prompt + 5 completion -> expected ~0.0000045
17
- expect(c).toBeCloseTo(0.0000045, 7);
18
- });
19
-
20
- it('aggregates cost across a run with calls', async () => {
21
- const client = createMockOpenAI(OPENAI_RESPONSE);
22
- const wrapped = warp(client);
23
-
24
- const r = run('test');
25
- const g = group('step');
26
-
27
- const r1 = await wrapped.chat.completions.create({ model: 'gpt-4o-mini', messages: [] });
28
- const r2 = await wrapped.chat.completions.create({ model: 'gpt-4o-mini', messages: [] });
29
-
30
- add(g, r1, r2);
31
- add(r, g);
32
-
33
- const singleCost = cost(r1);
34
- const runCost = cost(r);
35
- expect(runCost).toBeCloseTo(singleCost * 2, 10);
36
- });
37
-
38
- it('returns 0 for unknown targets', () => {
39
- expect(cost({})).toBe(0);
40
- expect(cost('wm_run_nonexistent')).toBe(0);
41
- });
42
- });
@@ -1,22 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { group } from '../index.js';
3
- import { groupRegistry } from '../core/registry.js';
4
- import { setupBeforeEach } from '../../test/setup.js';
5
-
6
- setupBeforeEach();
7
-
8
- describe('group()', () => {
9
- it('returns a frozen object with id and _type', () => {
10
- const g = group('planner');
11
- expect(g.id).toMatch(/^wm_grp_/);
12
- expect(g._type).toBe('group');
13
- expect(Object.isFrozen(g)).toBe(true);
14
- });
15
-
16
- it('stores data in groupRegistry', () => {
17
- const g = group('planner', { name: 'Planning Phase' });
18
- const data = groupRegistry.get(g.id);
19
- expect(data.label).toBe('planner');
20
- expect(data.name).toBe('Planning Phase');
21
- });
22
- });
@@ -1,40 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { warp, run, outcome, flush } from '../index.js';
3
- import { setupBeforeEach, createMockOpenAI, OPENAI_RESPONSE } from '../../test/setup.js';
4
-
5
- setupBeforeEach();
6
-
7
- describe('outcome()', () => {
8
- it('enqueues an outcome event for a run', async () => {
9
- const r = run('test');
10
- outcome(r, 'completed', { reason: 'All good' });
11
- await flush();
12
-
13
- const body = JSON.parse(global.fetch.mock.calls[0][1].body);
14
- const o = body.outcomes.find(e => e.name === 'completed');
15
- expect(o).toBeDefined();
16
- expect(o.targetId).toBe(r.id);
17
- expect(o.reason).toBe('All good');
18
- });
19
-
20
- it('works with a ref string', async () => {
21
- outcome('wm_run_abc123', 'shipped');
22
- await flush();
23
-
24
- const body = JSON.parse(global.fetch.mock.calls[0][1].body);
25
- expect(body.outcomes[0].targetId).toBe('wm_run_abc123');
26
- });
27
-
28
- it('works on an LLM response', async () => {
29
- const client = createMockOpenAI(OPENAI_RESPONSE);
30
- const wrapped = warp(client);
31
- const response = await wrapped.chat.completions.create({ model: 'gpt-4o-mini', messages: [] });
32
-
33
- outcome(response, 'helpful');
34
- await flush();
35
-
36
- const body = JSON.parse(global.fetch.mock.calls[0][1].body);
37
- const o = body.outcomes.find(e => e.name === 'helpful');
38
- expect(o.targetId).toMatch(/^wm_call_/);
39
- });
40
- });
@@ -1,35 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { warp, run, group, ref } from '../index.js';
3
- import { setupBeforeEach, createMockOpenAI, OPENAI_RESPONSE } from '../../test/setup.js';
4
-
5
- setupBeforeEach();
6
-
7
- describe('ref()', () => {
8
- it('passes through strings', () => {
9
- expect(ref('wm_run_abc')).toBe('wm_run_abc');
10
- });
11
-
12
- it('extracts id from Run', () => {
13
- const r = run('test');
14
- expect(ref(r)).toBe(r.id);
15
- });
16
-
17
- it('extracts id from Group', () => {
18
- const g = group('test');
19
- expect(ref(g)).toBe(g.id);
20
- });
21
-
22
- it('resolves LLM response to call id', async () => {
23
- const client = createMockOpenAI(OPENAI_RESPONSE);
24
- const wrapped = warp(client);
25
- const response = await wrapped.chat.completions.create({ model: 'gpt-4o-mini', messages: [] });
26
-
27
- const id = ref(response);
28
- expect(id).toMatch(/^wm_call_/);
29
- });
30
-
31
- it('returns undefined for unknown objects', () => {
32
- expect(ref({})).toBeUndefined();
33
- expect(ref(null)).toBeUndefined();
34
- });
35
- });
@@ -1,34 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { run, flush } from '../index.js';
3
- import { runRegistry } from '../core/registry.js';
4
- import { setupBeforeEach } from '../../test/setup.js';
5
-
6
- setupBeforeEach();
7
-
8
- describe('run()', () => {
9
- it('returns a frozen object with id and _type', () => {
10
- const r = run('code-review');
11
- expect(r.id).toMatch(/^wm_run_/);
12
- expect(r._type).toBe('run');
13
- expect(Object.isFrozen(r)).toBe(true);
14
- });
15
-
16
- it('stores data in runRegistry', () => {
17
- const r = run('code-review', { name: 'PR #42', link: 'https://github.com/pr/42' });
18
- const data = runRegistry.get(r.id);
19
- expect(data.label).toBe('code-review');
20
- expect(data.name).toBe('PR #42');
21
- expect(data.link).toBe('https://github.com/pr/42');
22
- expect(data.groups).toEqual([]);
23
- expect(data.calls).toEqual([]);
24
- });
25
-
26
- it('enqueues a run event for transport', async () => {
27
- run('test-label');
28
- await flush();
29
- expect(global.fetch).toHaveBeenCalledTimes(1);
30
- const body = JSON.parse(global.fetch.mock.calls[0][1].body);
31
- expect(body.runs).toHaveLength(1);
32
- expect(body.runs[0].label).toBe('test-label');
33
- });
34
- });