@fungi.computer/grove 0.3.0 → 0.4.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.
@@ -0,0 +1 @@
1
+ import{a as e,m as t,n,s as r}from"./preload-helper-DY31Aviq.js";import{t as i}from"./provider-env-CcCbsTJq.js";import{a}from"./diagnostics-CxvqSC4Q.js";import{n as o,t as s}from"./models-BM_0GHMS.js";import{n as c}from"./headers-FK4rgixo.js";import{h as l,i as u}from"./transform-messages-CgFTI6NF.js";import{t as d}from"./sanitize-unicode-DmIE6hZJ.js";import{n as f,t as p}from"./error-body-pkrba6pf.js";import{a as m,c as h,d as g,f as _,h as v,i as y,l as b,m as x,n as S,o as C,p as w,r as T,s as E,t as D,u as O}from"./google-shared-CRsSlHta.js";var k=`v1`,A=`gcp-vertex-credentials`,j=0,M=(e,t,r)=>{let i=new a,o=n(t);return(async()=>{let t={role:`assistant`,content:[],api:`google-vertex`,provider:e.provider,model:e.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:`pending`,timestamp:Date.now()};try{if(r?.fetch&&r.fetch!==globalThis.fetch)throw Error(`Custom fetch is not supported by the Google Vertex adapter`);let n=B(r),a=n?F(e,n,r?.headers):P(e,H(r),U(r),r?.headers,r?.env),c=W(e,o,r),l=await r?.onPayload?.(c,e);l!==void 0&&(c=l);let u=await b(()=>a.models.generateContentStream(c),r);i.push({type:`start`,partial:t});let d=null,f=t.content,p=()=>f.length-1;for await(let n of u){await r?.onProviderStreamEvent?.(n,e),t.responseId||=n.responseId;let a=n.candidates?.[0];if(a?.content?.parts)for(let e of a.content.parts){if(e.text!==void 0){let n=y(e);(!d||n&&d.type!==`thinking`||!n&&d.type!==`text`)&&(d&&(d.type===`text`?i.push({type:`text_end`,contentIndex:f.length-1,content:d.text,partial:t}):i.push({type:`thinking_end`,contentIndex:p(),content:d.thinking,partial:t})),n?(d={type:`thinking`,thinking:``,thinkingSignature:void 0},t.content.push(d),i.push({type:`thinking_start`,contentIndex:p(),partial:t})):(d={type:`text`,text:``},t.content.push(d),i.push({type:`text_start`,contentIndex:p(),partial:t}))),d.type===`thinking`?(d.thinking+=e.text,d.thinkingSignature=h(d.thinkingSignature,e.thoughtSignature),i.push({type:`thinking_delta`,contentIndex:p(),delta:e.text,partial:t})):(d.text+=e.text,d.textSignature=h(d.textSignature,e.thoughtSignature),i.push({type:`text_delta`,contentIndex:p(),delta:e.text,partial:t}))}if(e.functionCall){d&&=(d.type===`text`?i.push({type:`text_end`,contentIndex:p(),content:d.text,partial:t}):i.push({type:`thinking_end`,contentIndex:p(),content:d.thinking,partial:t}),null);let n=e.functionCall.id,r={type:`toolCall`,id:!n||t.content.some(e=>e.type===`toolCall`&&e.id===n)?`${e.functionCall.name}_${Date.now()}_${++j}`:n,name:e.functionCall.name||``,arguments:e.functionCall.args??{},...e.thoughtSignature&&{thoughtSignature:e.thoughtSignature}};t.content.push(r),i.push({type:`toolcall_start`,contentIndex:p(),partial:t}),i.push({type:`toolcall_delta`,contentIndex:p(),delta:JSON.stringify(r.arguments),partial:t}),i.push({type:`toolcall_end`,contentIndex:p(),toolCall:r,partial:t})}}a?.finishReason&&(t.rawStopReason=a.finishReason,t.stopReason=m(a.finishReason),t.content.some(e=>e.type===`toolCall`)&&t.stopReason===`stop`&&(t.stopReason=`toolUse`)),n.usageMetadata&&(t.usage={input:(n.usageMetadata.promptTokenCount||0)-(n.usageMetadata.cachedContentTokenCount||0),output:(n.usageMetadata.candidatesTokenCount||0)+(n.usageMetadata.thoughtsTokenCount||0),cacheRead:n.usageMetadata.cachedContentTokenCount||0,cacheWrite:0,reasoning:n.usageMetadata.thoughtsTokenCount||0,totalTokens:n.usageMetadata.totalTokenCount||0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},s(e,t.usage))}if(d&&(d.type===`text`?i.push({type:`text_end`,contentIndex:p(),content:d.text,partial:t}):i.push({type:`thinking_end`,contentIndex:p(),content:d.thinking,partial:t})),r?.signal?.aborted)throw Error(`Request was aborted`);if(t.stopReason===`pending`)throw Error(`Google Vertex stream ended without a finish reason`);if(t.stopReason===`aborted`||t.stopReason===`error`){let e=t.rawStopReason?`Provider stopped with: ${t.rawStopReason}`:`An unknown error occurred`;throw Error(e)}i.push({type:`done`,reason:t.stopReason,message:t}),i.end()}catch(e){for(let e of t.content)`index`in e&&delete e.index;t.stopReason=r?.signal?.aborted?`aborted`:`error`,t.errorMessage=p(f(e)),i.push({type:`error`,reason:t.stopReason,error:t}),i.end()}})(),i},N=(e,t,n)=>{let r={...u(e,t,n,void 0),toolChoice:n?.toolChoice};if(!n?.reasoning)return M(e,t,{...r,thinking:{enabled:!1}});let i=o(e,n.reasoning);if(i===`off`)return M(e,t,{...r,thinking:{enabled:!1}});let a=E(e,i);return w(e)?M(e,t,{...r,thinking:{enabled:!0,level:_(a)}}):M(e,t,{...r,thinking:{enabled:!0,budgetTokens:G(e,a,n.thinkingBudgets)}})};function P(e,t,n,r,i){let a=z(i);return new x({vertexai:!0,project:t,location:n,apiVersion:k,...a?{googleAuthOptions:a}:{},httpOptions:I(e,r)})}function F(e,t,n){return new x({vertexai:!0,apiKey:t,apiVersion:k,httpOptions:I(e,n)})}function I(e,t){let n={},r=L(e.baseUrl);r&&(n.baseUrl=r,n.baseUrlResourceScope=v.COLLECTION,R(r)&&(n.apiVersion=``));let i=c({"User-Agent":l(),...e.headers,...t});return i&&(n.headers=i),Object.keys(n).length>0?n:void 0}function L(e){let t=e.trim();if(!(!t||t.includes(`{location}`)))return t}function R(e){try{return new URL(e).pathname.split(`/`).some(e=>/^v\d+(?:beta\d*)?$/.test(e))}catch{return/(?:^|\/)v\d+(?:beta\d*)?(?:\/|$)/.test(e)}}function z(e){let t=i(`GOOGLE_APPLICATION_CREDENTIALS`,e);return t?{keyFilename:t}:void 0}function B(e){let t=e?.apiKey?.trim();if(!(!t||t===A||V(t)))return t}function V(e){return/^<[^>]+>$/.test(e)}function H(e){let t=e?.project||i(`GOOGLE_CLOUD_PROJECT`,e?.env)||i(`GCLOUD_PROJECT`,e?.env);if(!t)throw Error(`Vertex AI requires a project ID. Set GOOGLE_CLOUD_PROJECT/GCLOUD_PROJECT or pass project in options.`);return t}function U(e){let t=e?.location||i(`GOOGLE_CLOUD_LOCATION`,e?.env);if(!t)throw Error(`Vertex AI requires a location. Set GOOGLE_CLOUD_LOCATION or pass location in options.`);return t}function W(n,i,a={}){let o=D(n,i),s=r(i.messages),c=e(i.messages),l={};a.temperature!==void 0&&(l.temperature=a.temperature),a.maxTokens!==void 0&&(l.maxOutputTokens=a.maxTokens);let u=O(n.id),f=c.length>0?C(c,a.toolChoice,u):void 0,p=s?t(s):``,m={...Object.keys(l).length>0&&l,...p&&{systemInstruction:d(p)},...c.length>0&&{tools:S(c,!1,u)},...f!==void 0&&{toolConfig:{functionCallingConfig:{mode:f}}}};if(a.thinking?.enabled&&n.reasoning){let e={includeThoughts:!0};a.thinking.level===void 0?a.thinking.budgetTokens!==void 0&&(e.thinkingBudget=a.thinking.budgetTokens):e.thinkingLevel=g(a.thinking.level),m.thinkingConfig=e}else n.reasoning&&a.thinking&&!a.thinking.enabled&&(m.thinkingConfig=T(n));if(a.signal){if(a.signal.aborted)throw Error(`Request aborted`);m.abortSignal=a.signal}return{model:n.id,contents:o,config:m}}function G(e,t,n){return n?.[t]===void 0?e.id.includes(`2.5-pro`)?{minimal:128,low:2048,medium:8192,high:32768}[t]:e.id.includes(`2.5-flash`)?{minimal:128,low:2048,medium:8192,high:24576}[t]:-1:n[t]}export{M as stream,N as streamSimple};