@spendgraph/prompt 0.7.0 → 0.8.0

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.
@@ -1 +1 @@
1
- import{makePrompt as l,noRecorder as d}from"../prompt/index.js";import{applyVariables as i}from"../render/index.js";let u=!1;function c(o){return(o.inputTokens??0)===0&&(o.outputTokens??0)===0}function k(){u||(u=!0,console.warn("[prompt] call() recorded a call with no tokens. This package never sees the provider response, so return inputTokens and outputTokens from your callback or every cost on this prompt reads as zero."))}function m(o,t,n){const a=n.onEmptyTokens??k,r=s=>{try{n.onReportError?.(s)}catch{}};return{enabled:!0,write(s,e){e.status!=="failed"&&(c(e)&&a(e),o.ingest.send([{eventId:e.rolloutId,model:e.model,inputTokens:e.inputTokens??0,outputTokens:e.outputTokens??0,cacheReadTokens:e.cacheReadTokens,cacheWriteTokens:e.cacheWriteTokens,citationTokens:e.citationTokens,reasoningTokens:e.reasoningTokens,metadata:{...n.metadata,prompt:t}}]).catch(r))}}}function f(o,t={}){const n=o.variables??{},a=(o.blocks??[]).map(s=>({title:i(s.title,n),body:i(s.body,n)})),r=i(o.question??"",n);return l({id:o.name,name:o.name,slug:null,blocks:a,question:r,fields:o.fields??[],versionId:null,models:o.models??[]},"custom",t.sdk?m(t.sdk,o.name,t):d)}export{f as buildCustomPrompt};
1
+ import{makePrompt as l,noRecorder as c}from"../prompt/index.js";import{applyVariables as i}from"../render/index.js";let u=!1;function k(o){return(o.inputTokens??0)===0&&(o.outputTokens??0)===0}function m(){u||(u=!0,console.warn("[prompt] call() recorded a call with no tokens. This package never sees the provider response, so return inputTokens and outputTokens from your callback or every cost on this prompt reads as zero."))}function p(o,t,n){const s=n.onEmptyTokens??m,a=r=>{try{n.onReportError?.(r)}catch{}};return{enabled:!0,write(r,e){if(e.status!=="failed")return k(e)&&s(e),o.ingest.priced({eventId:e.rolloutId,model:e.model,inputTokens:e.inputTokens??0,outputTokens:e.outputTokens??0,cacheReadTokens:e.cacheReadTokens,cacheWriteTokens:e.cacheWriteTokens,citationTokens:e.citationTokens,reasoningTokens:e.reasoningTokens,metadata:{...n.metadata,prompt:t}}).catch(d=>{a(d)})}}}function b(o,t={}){const n=o.variables??{},s=(o.blocks??[]).map(r=>({title:i(r.title,n),body:i(r.body,n)})),a=i(o.question??"",n);return l({id:o.name,name:o.name,slug:null,blocks:s,question:a,fields:o.fields??[],versionId:null,models:o.models??[]},"custom",t.sdk?p(t.sdk,o.name,t):c)}export{b as buildCustomPrompt};
@@ -9,9 +9,9 @@ export type { RolloutInput, RolloutRecord } from "@spendgraph/sdk";
9
9
  export { serializeFields, validateFields } from "@spendgraph/sdk";
10
10
  export { Budget } from "./budget/index.js";
11
11
  export type { CacheHit, CacheOptions } from "./cache/index.js";
12
+ export { PullCache } from "./cache/index.js";
12
13
  export type { Worded } from "./fingerprint/index.js";
13
14
  export { fingerprint } from "./fingerprint/index.js";
14
- export { PullCache } from "./cache/index.js";
15
15
  export type { PromptShape, Recorder } from "./prompt/index.js";
16
16
  export { bind, makePrompt, noRecorder } from "./prompt/index.js";
17
17
  export { pullWithCache } from "./pull/index.js";
package/dist/internals.js CHANGED
@@ -1 +1 @@
1
- import{serializeFields as o,validateFields as t}from"@spendgraph/sdk";import{Budget as m}from"./budget/index.js";import{fingerprint as l}from"./fingerprint/index.js";import{PullCache as a}from"./cache/index.js";import{bind as x,makePrompt as n,noRecorder as u}from"./prompt/index.js";import{pullWithCache as h}from"./pull/index.js";import{mapLimit as g,newRolloutId as C}from"./run/index.js";export{m as Budget,a as PullCache,x as bind,l as fingerprint,n as makePrompt,g as mapLimit,C as newRolloutId,u as noRecorder,h as pullWithCache,o as serializeFields,t as validateFields};
1
+ import{serializeFields as o,validateFields as t}from"@spendgraph/sdk";import{Budget as m}from"./budget/index.js";import{PullCache as l}from"./cache/index.js";import{fingerprint as a}from"./fingerprint/index.js";import{bind as x,makePrompt as n,noRecorder as u}from"./prompt/index.js";import{pullWithCache as h}from"./pull/index.js";import{mapLimit as g,newRolloutId as C}from"./run/index.js";export{m as Budget,l as PullCache,x as bind,a as fingerprint,n as makePrompt,g as mapLimit,C as newRolloutId,u as noRecorder,h as pullWithCache,o as serializeFields,t as validateFields};
@@ -1 +1 @@
1
- import{serializeFields as R}from"@spendgraph/sdk";import{findVariables as S,renderMessages as h}from"../render/index.js";import{recordable as v}from"../render/record.js";import{newRolloutId as E}from"../run/id.js";import{fingerprint as $}from"../fingerprint/index.js";import{bind as j}from"./bind.js";const x={enabled:!1,write:()=>{}},A=50;let D=!1;function z(e){D||e.length<=A||(D=!0,console.warn(`[prompt] ${e.length} messages is over the ${A} a rollout can record. The call still runs; the rollout will be rejected. Trim the history you pass.`))}function O(e,r,l){const s=Object.keys(e).filter(i=>l.has(i)&&!r.has(i));return s.length===0||console.warn(`[prompt] ${s.map(i=>`{${i}}`).join(", ")} appears in this prompt and you passed a value for it, but no field declares it, so it was not substituted and the placeholder goes to the model as written. Add it to the prompt's fields.`),s}function P(e){for(let r=e.length-1;r>=0;r--)if(e[r].role==="user")return e[r].content;return""}function L(e,r,l=x){const s=new Set(e.fields.map(t=>t.name)),i=new Set(S(e.blocks,e.question));let w=!1;const u=(t={})=>(w||(w=O(t,s,i).length>0),R(t,e.fields)),q=(t={},f={})=>h(e.blocks,e.question,u(t),f.history),b={id:e.id,name:e.name,slug:e.slug,fields:e.fields,versionId:e.versionId,fingerprint:$({name:e.name,blocks:e.blocks,question:e.question}),models:e.models,source:r,serialize:u,format:q,async call(t,f,d={}){const k=u(t),a=h(e.blocks,e.question,k,d.history);z(a);const m=d.tools?.trace(d.query??P(a)),T=d.rolloutId??E(),g=l.enabled&&(d.report??!0),y=Date.now(),I=()=>({rolloutId:T,versionId:e.versionId,fields:k,rendered:v(a)});let o;try{o=await f({messages:a,turn:m})}catch(n){if(g){const c=m?.record();l.write(e.id,{...I(),model:"unknown",output:"",status:"failed",error:n instanceof Error?n.message:String(n),latencyMs:Date.now()-y,offeredTools:c?.offeredTools,steps:c?.steps})}throw n}const M=Date.now()-y;let p;if(g){const n=m?.record(),c=l.write(e.id,{...I(),model:o.model,output:o.output,status:o.status??"completed",error:o.error,inputTokens:o.inputTokens,outputTokens:o.outputTokens,cacheReadTokens:o.cacheReadTokens,cacheWriteTokens:o.cacheWriteTokens,citationTokens:o.citationTokens,reasoningTokens:o.reasoningTokens,latencyMs:M,caseId:o.caseId,seed:o.seed,offeredTools:o.offeredTools??n?.offeredTools,steps:o.steps??n?.steps});p=c instanceof Promise?c:void 0}return{...o,rolloutId:T,...p?{pricing:p}:{}}},bind:t=>j(b,t)};return b}export{A as MAX_RECORDED_MESSAGES,L as makePrompt,x as noRecorder};
1
+ import{serializeFields as R}from"@spendgraph/sdk";import{fingerprint as S}from"../fingerprint/index.js";import{findVariables as v,renderMessages as h}from"../render/index.js";import{recordable as E}from"../render/record.js";import{newRolloutId as $}from"../run/id.js";import{bind as j}from"./bind.js";const x={enabled:!1,write:()=>{}},A=50;let D=!1;function z(e){D||e.length<=A||(D=!0,console.warn(`[prompt] ${e.length} messages is over the ${A} a rollout can record. The call still runs; the rollout will be rejected. Trim the history you pass.`))}function O(e,r,l){const s=Object.keys(e).filter(i=>l.has(i)&&!r.has(i));return s.length===0||console.warn(`[prompt] ${s.map(i=>`{${i}}`).join(", ")} appears in this prompt and you passed a value for it, but no field declares it, so it was not substituted and the placeholder goes to the model as written. Add it to the prompt's fields.`),s}function P(e){for(let r=e.length-1;r>=0;r--)if(e[r].role==="user")return e[r].content;return""}function L(e,r,l=x){const s=new Set(e.fields.map(t=>t.name)),i=new Set(v(e.blocks,e.question));let w=!1;const u=(t={})=>(w||(w=O(t,s,i).length>0),R(t,e.fields)),q=(t={},f={})=>h(e.blocks,e.question,u(t),f.history),b={id:e.id,name:e.name,slug:e.slug,fields:e.fields,versionId:e.versionId,fingerprint:S({name:e.name,blocks:e.blocks,question:e.question}),models:e.models,source:r,serialize:u,format:q,async call(t,f,d={}){const k=u(t),a=h(e.blocks,e.question,k,d.history);z(a);const m=d.tools?.trace(d.query??P(a)),T=d.rolloutId??$(),g=l.enabled&&(d.report??!0),y=Date.now(),I=()=>({rolloutId:T,versionId:e.versionId,fields:k,rendered:E(a)});let o;try{o=await f({messages:a,turn:m})}catch(n){if(g){const c=m?.record();l.write(e.id,{...I(),model:"unknown",output:"",status:"failed",error:n instanceof Error?n.message:String(n),latencyMs:Date.now()-y,offeredTools:c?.offeredTools,steps:c?.steps})}throw n}const M=Date.now()-y;let p;if(g){const n=m?.record(),c=l.write(e.id,{...I(),model:o.model,output:o.output,status:o.status??"completed",error:o.error,inputTokens:o.inputTokens,outputTokens:o.outputTokens,cacheReadTokens:o.cacheReadTokens,cacheWriteTokens:o.cacheWriteTokens,citationTokens:o.citationTokens,reasoningTokens:o.reasoningTokens,latencyMs:M,caseId:o.caseId,seed:o.seed,offeredTools:o.offeredTools??n?.offeredTools,steps:o.steps??n?.steps});p=c instanceof Promise?c:void 0}return{...o,rolloutId:T,...p?{pricing:p}:{}}},bind:t=>j(b,t)};return b}export{A as MAX_RECORDED_MESSAGES,L as makePrompt,x as noRecorder};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spendgraph/prompt",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Pull a stored prompt or build one in code, render it, and record what it cost.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -44,11 +44,12 @@
44
44
  "README.md"
45
45
  ],
46
46
  "dependencies": {
47
- "@spendgraph/sdk": "^0.7.0"
47
+ "@spendgraph/sdk": "^0.8.0"
48
48
  },
49
49
  "devDependencies": {
50
- "@spendgraph/llms": "^0.7.0",
51
- "@spendgraph/tools": "^0.7.0",
50
+ "@spendgraph/config": "0.8.0",
51
+ "@spendgraph/llms": "^0.8.0",
52
+ "@spendgraph/tools": "^0.8.0",
52
53
  "typescript": "^5"
53
54
  },
54
55
  "engines": {
@@ -59,6 +60,7 @@
59
60
  },
60
61
  "scripts": {
61
62
  "build": "rm -rf dist && tsc -p tsconfig.json --emitDeclarationOnly && tsc -p tsconfig.json --declaration false --removeComments && node ../../scripts/minify.mjs dist",
62
- "test": "vitest run"
63
+ "test": "vitest run",
64
+ "typecheck": "tsc -p tsconfig.tests.json"
63
65
  }
64
66
  }