@@ -4,49 +4,53 @@ import { describe, expect, it } from 'bun:test'
44import { CodebuffClient } from '../client'
55
66describe ( 'Prompt Caching' , ( ) => {
7- it ( 'should be cheaper on second request' , async ( ) => {
8- const filler =
9- `Run UUID: ${ crypto . randomUUID ( ) } ` +
10- 'Ignore this text. This is just to make the prompt longer. ' . repeat ( 500 )
11- const prompt = 'respond with "hi"'
12-
13- const apiKey = process . env [ API_KEY_ENV_VAR ]
14- if ( ! apiKey ) {
15- throw new Error ( 'API key not found' )
16- }
17-
18- const client = new CodebuffClient ( {
19- apiKey,
20- } )
21- let cost1 = - 1
22- const run1 = await client . run ( {
23- prompt : `${ filler } \n\n${ prompt } ` ,
24- agent : 'base' ,
25- handleEvent : ( event ) => {
26- if ( event . type === 'finish' ) {
27- cost1 = event . totalCost
28- }
29- } ,
30- } )
31-
32- expect ( run1 . output . type ) . not . toEqual ( 'error' )
33- expect ( cost1 ) . toBeGreaterThanOrEqual ( 0 )
34-
35- let cost2 = - 1
36- const run2 = await client . run ( {
37- prompt,
38- agent : 'base' ,
39- previousRun : run1 ,
40- handleEvent : ( event ) => {
41- if ( event . type === 'finish' ) {
42- cost2 = event . totalCost
43- }
44- } ,
45- } )
46-
47- expect ( run2 . output . type ) . not . toEqual ( 'error' )
48- expect ( cost2 ) . toBeGreaterThanOrEqual ( 0 )
49-
50- expect ( cost1 ) . toBeGreaterThan ( cost2 )
51- } )
7+ it (
8+ 'should be cheaper on second request' ,
9+ async ( ) => {
10+ const filler =
11+ `Run UUID: ${ crypto . randomUUID ( ) } ` +
12+ 'Ignore this text. This is just to make the prompt longer. ' . repeat ( 500 )
13+ const prompt = 'respond with "hi"'
14+
15+ const apiKey = process . env [ API_KEY_ENV_VAR ]
16+ if ( ! apiKey ) {
17+ throw new Error ( 'API key not found' )
18+ }
19+
20+ const client = new CodebuffClient ( {
21+ apiKey,
22+ } )
23+ let cost1 = - 1
24+ const run1 = await client . run ( {
25+ prompt : `${ filler } \n\n${ prompt } ` ,
26+ agent : 'base' ,
27+ handleEvent : ( event ) => {
28+ if ( event . type === 'finish' ) {
29+ cost1 = event . totalCost
30+ }
31+ } ,
32+ } )
33+
34+ expect ( run1 . output . type ) . not . toEqual ( 'error' )
35+ expect ( cost1 ) . toBeGreaterThanOrEqual ( 0 )
36+
37+ let cost2 = - 1
38+ const run2 = await client . run ( {
39+ prompt,
40+ agent : 'base' ,
41+ previousRun : run1 ,
42+ handleEvent : ( event ) => {
43+ if ( event . type === 'finish' ) {
44+ cost2 = event . totalCost
45+ }
46+ } ,
47+ } )
48+
49+ expect ( run2 . output . type ) . not . toEqual ( 'error' )
50+ expect ( cost2 ) . toBeGreaterThanOrEqual ( 0 )
51+
52+ expect ( cost1 ) . toBeGreaterThan ( cost2 )
53+ } ,
54+ { timeout : 20_000 } ,
55+ )
5256} )
0 commit comments