@lunora/ai 1.0.0-alpha.48 → 1.0.0-alpha.49
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/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/packem_shared/createAi-CIGute6L.mjs +1 -0
- package/dist/packem_shared/{defineRag-DUOcnSgC.mjs → defineRag-B1Z_pMVl.mjs} +5 -5
- package/dist/rag/index.d.mts +1 -1
- package/dist/rag/index.d.ts +1 -1
- package/dist/rag/index.mjs +1 -1
- package/package.json +2 -2
- package/dist/packem_shared/createAi-C2ExoUDR.mjs +0 -1
- package/dist/packem_shared/{types.d-BcLGTChd.d.mts → types.d-DYRXBfIT.d.mts} +9 -9
- package/dist/packem_shared/{types.d-BcLGTChd.d.ts → types.d-DYRXBfIT.d.ts} +9 -9
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { L as LunoraAiOptions, a as LunoraAi } from "./packem_shared/types.d-
|
|
2
|
-
export { A as AI_GATEWAY_ACCOUNT_ID_ENV, b as AI_GATEWAY_ID_ENV, c as AI_GATEWAY_TOKEN_ENV, type d as AiBindingLike, type e as AiGatewayMetadata, type f as AiGatewayOptions, type E as EmbeddingModelInput, type M as ModelInput, type R as ResolvedAiGateway, type W as WorkersAiProviderLike, g as buildAiGatewayMetadataFields, r as resolveAiGateway } from "./packem_shared/types.d-
|
|
1
|
+
import { L as LunoraAiOptions, a as LunoraAi } from "./packem_shared/types.d-DYRXBfIT.mjs";
|
|
2
|
+
export { A as AI_GATEWAY_ACCOUNT_ID_ENV, b as AI_GATEWAY_ID_ENV, c as AI_GATEWAY_TOKEN_ENV, type d as AiBindingLike, type e as AiGatewayMetadata, type f as AiGatewayOptions, type E as EmbeddingModelInput, type M as ModelInput, type R as ResolvedAiGateway, type W as WorkersAiProviderLike, g as buildAiGatewayMetadataFields, r as resolveAiGateway } from "./packem_shared/types.d-DYRXBfIT.mjs";
|
|
3
3
|
export { type EmbeddingModel, type LanguageModel, embed, embedMany, generateObject, generateText, hasToolCall, jsonSchema, streamObject, streamText, tool } from 'ai';
|
|
4
4
|
export { createWorkersAI } from 'workers-ai-provider';
|
|
5
5
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { L as LunoraAiOptions, a as LunoraAi } from "./packem_shared/types.d-
|
|
2
|
-
export { A as AI_GATEWAY_ACCOUNT_ID_ENV, b as AI_GATEWAY_ID_ENV, c as AI_GATEWAY_TOKEN_ENV, type d as AiBindingLike, type e as AiGatewayMetadata, type f as AiGatewayOptions, type E as EmbeddingModelInput, type M as ModelInput, type R as ResolvedAiGateway, type W as WorkersAiProviderLike, g as buildAiGatewayMetadataFields, r as resolveAiGateway } from "./packem_shared/types.d-
|
|
1
|
+
import { L as LunoraAiOptions, a as LunoraAi } from "./packem_shared/types.d-DYRXBfIT.js";
|
|
2
|
+
export { A as AI_GATEWAY_ACCOUNT_ID_ENV, b as AI_GATEWAY_ID_ENV, c as AI_GATEWAY_TOKEN_ENV, type d as AiBindingLike, type e as AiGatewayMetadata, type f as AiGatewayOptions, type E as EmbeddingModelInput, type M as ModelInput, type R as ResolvedAiGateway, type W as WorkersAiProviderLike, g as buildAiGatewayMetadataFields, r as resolveAiGateway } from "./packem_shared/types.d-DYRXBfIT.js";
|
|
3
3
|
export { type EmbeddingModel, type LanguageModel, embed, embedMany, generateObject, generateText, hasToolCall, jsonSchema, streamObject, streamText, tool } from 'ai';
|
|
4
4
|
export { createWorkersAI } from 'workers-ai-provider';
|
|
5
5
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{default as a}from"./packem_shared/createAi-
|
|
1
|
+
import{default as a}from"./packem_shared/createAi-CIGute6L.mjs";import{AI_GATEWAY_ACCOUNT_ID_ENV as o,AI_GATEWAY_ID_ENV as A,AI_GATEWAY_TOKEN_ENV as _,buildAiGatewayMetadataFields as m,resolveAiGateway as l}from"./packem_shared/AI_GATEWAY_ACCOUNT_ID_ENV-CPzc-So1.mjs";import{embed as T,embedMany as E,generateObject as d,generateText as x,hasToolCall as I,jsonSchema as b,streamObject as c,streamText as f,tool as i}from"ai";import{createWorkersAI as N}from"workers-ai-provider";export{o as AI_GATEWAY_ACCOUNT_ID_ENV,A as AI_GATEWAY_ID_ENV,_ as AI_GATEWAY_TOKEN_ENV,m as buildAiGatewayMetadataFields,a as createAi,N as createWorkersAI,T as embed,E as embedMany,d as generateObject,x as generateText,I as hasToolCall,b as jsonSchema,l as resolveAiGateway,c as streamObject,f as streamText,i as tool};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as t}from"@lunora/errors";import{createWorkersAI as c}from"workers-ai-provider";import{buildAiGatewayMetadataFields as v,resolveAiGateway as A}from"./AI_GATEWAY_ACCOUNT_ID_ENV-CPzc-So1.mjs";const I=(r,d,s)=>{const i=v(s);if(r!==void 0)return i!==void 0&&r.metadata===void 0?{...r,metadata:i}:r;if(d===void 0)return;const a=A(d,s,"workers-ai-binding");if(a!==void 0)return i===void 0?{id:a.gatewayId}:{id:a.gatewayId,metadata:i}},N=r=>{const{binding:d,defaultEmbeddingModel:s,defaultModel:i,env:a,gateway:f,metadata:g,provider:m}=r;if(!m&&!d)throw new t("INTERNAL","@lunora/ai: createAi requires a `binding` (env.AI) or a pre-built `provider`");const u=I(f,a,g),n=m??c({binding:d,gateway:u}),p=e=>{if(e===void 0){if(!i)throw new t("INTERNAL","@lunora/ai: no model supplied and no `defaultModel` configured — pass a model id or an AI SDK model");return n(i)}return typeof e=="string"?n(e):e},w=e=>{const o=n.textEmbeddingModel;if(typeof o!="function")throw new t("INTERNAL","@lunora/ai: the Workers AI provider does not expose `textEmbeddingModel`; pass an AI SDK EmbeddingModel (e.g. from @ai-sdk/openai) to embed()");return o.call(n,e)};return{embeddingModel:e=>{if(typeof e=="object")return e;const o=e??s;if(!o)throw new t("INTERNAL","@lunora/ai: no embedding model supplied and no `defaultEmbeddingModel` configured — pass an embedding model id or an AI SDK EmbeddingModel");return w(o)},model:p,run:async(e,o,l)=>{if(!d)throw new t("INTERNAL","@lunora/ai: ai.run requires the `binding` (env.AI) — it is unavailable when only a custom `provider` was supplied");const b=u!==void 0&&l?.gateway===void 0?{...l,gateway:u}:l;return d.run(e,o,b)},workersai:n}};export{N as default};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import{LunoraError as x}from"@lunora/errors";import{tool as G,jsonSchema as J,embed as ee}from"ai";import te from"./fixedWindowChunks-XJRXHEoz.mjs";import ne from"./hybridRank-U6PmGuz1.mjs";const
|
|
1
|
+
import{LunoraError as x}from"@lunora/errors";import{tool as G,jsonSchema as J,embed as ee}from"ai";import te from"./fixedWindowChunks-XJRXHEoz.mjs";import{contentHash as ne}from"./contentHash-BIn6ECP8.mjs";import ae from"./hybridRank-U6PmGuz1.mjs";const re=8,oe=async(e,c,p)=>{if(!Number.isInteger(c)||c<1)throw new RangeError("concurrentMap: `limit` must be a positive integer");if(e.length===0)return[];const b=Math.max(1,Math.min(c,e.length)),y=Array.from({length:e.length});let f=0,k=!1,v;const E=async()=>{for(;;){if(k)return;const I=f;if(f+=1,I>=e.length)return;try{y[I]=await p(e[I],I)}catch(N){k||(k=!0,v=N);return}}},h=Array.from({length:b},()=>E());if(await Promise.all(h),k)throw v;return y},ie=1e3,se=200,ce=5,de=20,ue=100,z="__ragChunk",V="__ragSource",A="__ragText",$="__ragHash",T="__ragChunks",M="__ragImportance",F="__ragModel",me=new Set([z,T,$,M,F,V,A]),he=/^[\w.-]{1,40}$/,P=e=>e===void 0?"":`${encodeURIComponent(e)}#`,_=(e,c,p)=>`${P(e)}${c}#${String(p)}`,K=(e,c)=>{const p=P(c),b=p!==""&&e.startsWith(p)?e.slice(p.length):e,y=b.lastIndexOf("#"),f=y===-1?Number.NaN:Number(b.slice(y+1));return y===-1||!Number.isInteger(f)||f<0?{chunkIndex:0,sourceId:b}:{chunkIndex:f,sourceId:b.slice(0,y)}},pe=async e=>ne(new TextEncoder().encode(e)),O=e=>{if(!e)return;const c=Object.entries(e).filter(([p])=>!me.has(p));return c.length>0?Object.fromEntries(c):void 0},Q=new Set,le=e=>{Q.has(e)||(Q.add(e),console.warn(`[@lunora/ai/rag] index "${e}" is used without a namespace — in a multi-tenant/sharded
|
|
2
2
|
app this shares one tenant's chunks (text included) with every other tenant, since
|
|
3
3
|
Vectorize indexes are account-global. Pass \`namespace\` (the shard/tenant key) on both
|
|
4
|
-
index() and retrieve(). Single-tenant apps suppress this via { allowSharedNamespace: true }.`))},
|
|
5
|
-
${
|
|
4
|
+
index() and retrieve(). Single-tenant apps suppress this via { allowSharedNamespace: true }.`))},ge=e=>e.map(c=>`[source:${c.sourceId}#${String(c.chunkIndex)}]
|
|
5
|
+
${c.text}`).join(`
|
|
6
6
|
|
|
7
|
-
`),
|
|
8
|
-
`)}})},X=t=>{if(typeof t=="string"){const n=e.filters?.[t];if(!n)throw new x("NOT_FOUND",`@lunora/ai/rag: unknown named filter "${t}" — must be one of the keys declared in RagConfig.filters`);return n.filter}return t},Y=(t,n)=>t.matches.map(o=>{const i=o.metadata??{},a=K(o.id,n),d=i[A],r=i[M],u=typeof r=="number"&&r>=0&&r<=1?r:1;return{chunkIndex:a.chunkIndex,id:o.id,importance:u,metadata:O(i),score:o.score*u,sourceId:a.sourceId,text:typeof d=="string"?d:""}}),Z=async(t,n)=>{if(!f)return t;const o=t.map(r=>r.id),[i,a]=await Promise.all([
|
|
7
|
+
`),fe=(e,c)=>{if(typeof e=="object")return e;if(c===void 0)throw new x("INTERNAL","@lunora/ai/rag: `embeddingModel` is a Workers AI model id (or omitted) but the bound context has no `ai` (env.AI). Pass an AI SDK EmbeddingModel object to embed without Workers AI, or bind a context whose `ctx.ai` is wired.");return c.embeddingModel(e)},we=e=>{const c=e.modelId;return typeof c=="string"&&c.length>0?c:void 0},xe=e=>{if(!(typeof e!="object"||e===null)){for(const c of Object.values(e))if(typeof c=="object"&&c!==null){const{cost:p}=c;if(typeof p=="number"&&Number.isFinite(p))return p}}},Se=e=>{if(typeof e.index!="string"||e.index.length===0)throw new x("BAD_REQUEST","@lunora/ai/rag: `index` must be a non-empty Vectorize index name");const c=e.chunkSize??ie,p=e.chunkOverlap??se;if(!Number.isInteger(c)||c<1)throw new x("BAD_REQUEST","@lunora/ai/rag: `chunkSize` must be a positive integer");if(!Number.isInteger(p)||p<0||p>=c)throw new x("BAD_REQUEST","@lunora/ai/rag: `chunkOverlap` must be a non-negative integer smaller than `chunkSize`");const b=e.topK??ce;if(!Number.isInteger(b)||b<1)throw new x("BAD_REQUEST","@lunora/ai/rag: `topK` must be a positive integer");if(e.embeddingModelVersion!==void 0&&!he.test(e.embeddingModelVersion))throw new x("BAD_REQUEST",'@lunora/ai/rag: `embeddingModelVersion` must match /^[A-Za-z0-9._-]{1,40}$/ (a short, stable tag like "bge-v1.5")');const y=e.chunk??(h=>te(h,c,p)),{textStore:f}=e,k=f?ue:de,v=e.embeddingModelVersion,E=h=>v===void 0?h:h===void 0?v:`${v}::${h}`;return h=>{let I;const N=typeof h.trace=="function"?h.trace:void 0,B=async t=>{I??=fe(e.embeddingModel,h.ai);const n=I,o=async d=>{const{embedding:r,providerMetadata:u,usage:m}=await ee({model:n,value:t});if(d!==void 0){const s=m.tokens;typeof s=="number"&&Number.isFinite(s)&&d.setAttribute("gen_ai.usage.input_tokens",s);const l=xe(u);l!==void 0&&d.setAttribute("gen_ai.usage.cost",l)}return r};if(N===void 0)return o();const i=we(n),a=typeof h.conversationId=="string"&&h.conversationId.length>0?h.conversationId:void 0;return N("ai.embed",(d,r)=>o(r),{"gen_ai.operation.name":"embeddings",...i===void 0?{}:{"gen_ai.request.model":i},...a===void 0?{}:{"gen_ai.conversation.id":a}})},R=t=>{if(t===void 0){if(e.requireNamespace)throw new x("BAD_REQUEST",`@lunora/ai/rag: index "${e.index}" requires a namespace (requireNamespace is set) — pass the tenant/shard key on index()/retrieve()/remove()`);e.allowSharedNamespace||le(e.index)}},q=async(t,n)=>{const[o]=await h.vectors.getByIds(e.index,[_(n,t,0)],n),i=o?.metadata?.[$],a=o?.metadata?.[T];return{chunks:typeof a=="number"&&Number.isInteger(a)&&a>0?a:void 0,hash:typeof i=="string"?i:void 0}},C=async(t,n,o,i)=>{const a=Array.from({length:o-n},(d,r)=>_(i,t,n+r));a.length!==0&&(await h.vectors.deleteByIds(e.index,a,i),await f?.remove?.(a,{namespace:i}),await e.lexicalStore?.remove?.(a,{namespace:i}))},W=async t=>{if(R(t.namespace),t.importance!==void 0&&(typeof t.importance!="number"||t.importance<0||t.importance>1))throw new x("BAD_REQUEST","@lunora/ai/rag: `importance` must be a number in [0, 1]");const n=E(t.namespace),o=await pe(t.text),i=await q(t.id,n);if(i.hash===o&&i.chunks!==void 0)return{chunks:i.chunks,ids:Array.from({length:i.chunks},(r,u)=>_(n,t.id,u)),unchanged:!0};const a=y(t.text),d=a.map((r,u)=>_(n,t.id,u));if(a.length===0&&t.allowEmptySources===!1)throw new x("BAD_REQUEST",`@lunora/ai/rag: source "${t.id}" produced zero chunks — set allowEmptySources: true to allow this`);if(a.length>0){const r=a.map((u,m)=>({chunkIndex:m,id:d[m],sourceId:t.id,text:u}));f&&await f.put(r,{namespace:n}),e.lexicalStore&&await e.lexicalStore.index(r,{namespace:n})}return await oe(a,re,async(r,u)=>{const m=d[u],s={...t.metadata,[z]:u,[V]:t.id};f||(s[A]=r),t.importance!==void 0&&(s[M]=t.importance),u===0&&(s[$]=o,s[T]=a.length,v!==void 0&&(s[F]=v)),await h.vectors.upsert(e.index,{embed:B,id:m,input:r,metadata:s,namespace:n}),t.onChunk?.({chunkIndex:u,id:m,text:r,total:a.length})}),i.chunks!==void 0&&i.chunks>a.length&&await C(t.id,a.length,i.chunks,n),{chunks:a.length,ids:d,unchanged:!1}},H=async t=>{R(t.namespace);const n=E(t.namespace),o=(await q(t.id,n)).chunks??1;await C(t.id,0,o,n)},D=async(t,n)=>{const o=new Map;if(t.length===0)return o;if(f){const a=await f.getMany(t,{namespace:n});for(const[d,r]of t.entries()){const u=a[d];typeof u=="string"&&o.set(r,u)}return o}const i=await h.vectors.getByIds(e.index,t,n);for(const a of i){const d=a.metadata?.[A];typeof d=="string"&&o.set(a.id,d)}return o},L=async(t,n,o)=>{const i=n?.chunkContext?.before??0,a=n?.chunkContext?.after??0;if(i===0&&a===0)return t;if(!Number.isInteger(i)||i<0||!Number.isInteger(a)||a<0)throw new x("BAD_REQUEST","@lunora/ai/rag: `chunkContext.before`/`chunkContext.after` must be non-negative integers");const d=new Map(t.map(s=>[s.id,s.text])),r=new Set;for(const s of t)for(let l=-i;l<=a;l+=1){const w=s.chunkIndex+l,g=_(o,s.sourceId,w);l!==0&&w>=0&&!d.has(g)&&r.add(g)}const u=await D([...r],o),m=(s,l)=>{const w=_(o,s,l);return d.get(w)??u.get(w)};return t.map(s=>{const l=[];for(let w=-i;w<=a;w+=1){const g=w===0?s.text:m(s.sourceId,s.chunkIndex+w);g!==void 0&&l.push(g)}return{...s,text:l.join(`
|
|
8
|
+
`)}})},X=t=>{if(typeof t=="string"){const n=e.filters?.[t];if(!n)throw new x("NOT_FOUND",`@lunora/ai/rag: unknown named filter "${t}" — must be one of the keys declared in RagConfig.filters`);return n.filter}return t},Y=(t,n)=>t.matches.map(o=>{const i=o.metadata??{},a=K(o.id,n),d=i[A],r=i[M],u=typeof r=="number"&&r>=0&&r<=1?r:1;return{chunkIndex:a.chunkIndex,id:o.id,importance:u,metadata:O(i),score:o.score*u,sourceId:a.sourceId,text:typeof d=="string"?d:""}}),Z=async(t,n)=>{if(!f)return t;const o=t.map(r=>r.id),[i,a]=await Promise.all([D(o,n),h.vectors.getByIds(e.index,o,n)]),d=new Map(a.map(r=>[r.id,r.metadata]));return t.flatMap(r=>{const u=i.get(r.id);if(u===void 0)return[];const m=d.get(r.id),s=m?.[M],l=typeof s=="number"&&s>=0&&s<=1?s:r.importance,w=(r.importance===0?0:r.score/r.importance)*l;return[{...r,importance:l,metadata:O(m)??r.metadata,score:w,text:u}]})},U=async(t,n)=>{R(n?.namespace);const o=E(n?.namespace),i=X(n?.filter),a=e.rlsFilter?await e.rlsFilter(h.auth):void 0,d=a?{...i,...a}:i,r=Math.min(n?.topK??b,k),u=await h.vectors.query(e.index,{embed:B,filter:d,input:t,namespace:o,returnMetadata:f?"indexed":"all",topK:r});let m=await Z(Y(u,o),o);const s=n?.minScore;if(s!==void 0&&(m=m.filter(g=>g.score>=s)),e.lexicalStore){const g=(await e.lexicalStore.search(t,{filter:d,namespace:o,topK:e.lexicalTopK??r})).map(S=>{const j=K(S.id,o);return{chunkIndex:j.chunkIndex,id:S.id,importance:1,metadata:void 0,score:S.score,sourceId:j.sourceId,text:S.text}});m=[...ae(m,g)]}m.sort((g,S)=>S.score-g.score),m=[...await L(m,n,o)];const l=[],w=new Set;for(const g of m)w.has(g.sourceId)||(w.add(g.sourceId),l.push({id:g.sourceId,metadata:g.metadata,weight:g.importance}));return n?.onRetrieve?.({matches:m.length,query:t}),{chunks:m,context:ge(m),sources:l}};return{asTool:t=>G({description:t?.description??`Search the "${e.index}" knowledge base for passages relevant to a natural-language query.`,execute:async({query:n})=>U(n,{namespace:t?.namespace,topK:t?.topK}),inputSchema:J({properties:{query:{description:"The natural-language search query.",type:"string"}},required:["query"],type:"object"})}),index:W,remove:H,retrieve:U}}};export{Se as default};
|
package/dist/rag/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Tool } from 'ai';
|
|
2
|
-
import { E as EmbeddingModelInput, a as LunoraAi } from "../packem_shared/types.d-
|
|
2
|
+
import { E as EmbeddingModelInput, a as LunoraAi } from "../packem_shared/types.d-DYRXBfIT.mjs";
|
|
3
3
|
/**
|
|
4
4
|
* Built-in fixed-window chunker: split into `size`-char windows overlapping by
|
|
5
5
|
* `overlap` chars. Deliberately simple and deterministic — the zero-config
|
package/dist/rag/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Tool } from 'ai';
|
|
2
|
-
import { E as EmbeddingModelInput, a as LunoraAi } from "../packem_shared/types.d-
|
|
2
|
+
import { E as EmbeddingModelInput, a as LunoraAi } from "../packem_shared/types.d-DYRXBfIT.js";
|
|
3
3
|
/**
|
|
4
4
|
* Built-in fixed-window chunker: split into `size`-char windows overlapping by
|
|
5
5
|
* `overlap` chars. Deliberately simple and deterministic — the zero-config
|
package/dist/rag/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{default as r}from"../packem_shared/fixedWindowChunks-XJRXHEoz.mjs";import{default as a}from"../packem_shared/defineRag-
|
|
1
|
+
import{default as r}from"../packem_shared/fixedWindowChunks-XJRXHEoz.mjs";import{default as a}from"../packem_shared/defineRag-B1Z_pMVl.mjs";import{contentHash as s,guessMimeTypeFromExtension as d}from"../packem_shared/contentHash-BIn6ECP8.mjs";import{default as n}from"../packem_shared/hybridRank-U6PmGuz1.mjs";import{default as i}from"../packem_shared/bm25LexicalStore-RA9sesFC.mjs";export{i as bm25LexicalStore,s as contentHash,a as defineRag,r as fixedWindowChunks,d as guessMimeTypeFromExtension,n as hybridRank};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/ai",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.49",
|
|
4
4
|
"description": "Workers AI helper for Lunora: provider-agnostic AI SDK access from functions, Workers AI by default",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"access": "public"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@lunora/errors": "1.0.0-alpha.
|
|
56
|
+
"@lunora/errors": "1.0.0-alpha.15",
|
|
57
57
|
"ai": "7.0.37",
|
|
58
58
|
"workers-ai-provider": "4.0.0"
|
|
59
59
|
},
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{LunoraError as t}from"@lunora/errors";import{createWorkersAI as c}from"workers-ai-provider";import{buildAiGatewayMetadataFields as v,resolveAiGateway as A}from"./AI_GATEWAY_ACCOUNT_ID_ENV-CPzc-So1.mjs";const I=(i,o,s)=>{const r=v(s);if(i!==void 0)return r!==void 0&&i.metadata===void 0?{...i,metadata:r}:i;if(o===void 0)return;const a=A(o,s,"workers-ai-binding");if(a!==void 0)return r===void 0?{id:a.gatewayId}:{id:a.gatewayId,metadata:r}},y=(i,o)=>c({binding:i,gateway:o}),h=i=>{const{binding:o,defaultEmbeddingModel:s,defaultModel:r,env:a,gateway:f,metadata:g,provider:m}=i;if(!m&&!o)throw new t("INTERNAL","@lunora/ai: createAi requires a `binding` (env.AI) or a pre-built `provider`");const u=I(f,a,g),n=m??y(o,u),p=e=>{if(e===void 0){if(!r)throw new t("INTERNAL","@lunora/ai: no model supplied and no `defaultModel` configured — pass a model id or an AI SDK model");return n(r)}return typeof e=="string"?n(e):e},w=e=>{const d=n.textEmbeddingModel;if(typeof d!="function")throw new t("INTERNAL","@lunora/ai: the Workers AI provider does not expose `textEmbeddingModel`; pass an AI SDK EmbeddingModel (e.g. from @ai-sdk/openai) to embed()");return d.call(n,e)};return{embeddingModel:e=>{if(typeof e=="object")return e;const d=e??s;if(!d)throw new t("INTERNAL","@lunora/ai: no embedding model supplied and no `defaultEmbeddingModel` configured — pass an embedding model id or an AI SDK EmbeddingModel");return w(d)},model:p,run:async(e,d,l)=>{if(!o)throw new t("INTERNAL","@lunora/ai: ai.run requires the `binding` (env.AI) — it is unavailable when only a custom `provider` was supplied");const b=u!==void 0&&l?.gateway===void 0?{...l,gateway:u}:l;return o.run(e,d,b)},workersai:n}};export{h as default};
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import { EmbeddingModel, LanguageModel } from 'ai';
|
|
2
|
+
/**
|
|
3
|
+
* Project {@link AiGatewayMetadata} to a plain string-map of only its defined,
|
|
4
|
+
* non-empty fields, or `undefined` when nothing is set. This is the shared source
|
|
5
|
+
* of truth behind both gateway-correlation forms: the `cf-aig-metadata` HTTP
|
|
6
|
+
* header (bring-your-own providers) and the Workers AI binding's native
|
|
7
|
+
* `gateway.metadata` option — both encode the same `{ functionPath, traceId }`.
|
|
8
|
+
* @experimental
|
|
9
|
+
*/
|
|
10
|
+
declare const buildAiGatewayMetadataFields: (metadata: AiGatewayMetadata | undefined) => Record<string, string> | undefined;
|
|
2
11
|
/**
|
|
3
12
|
* Which surface resolved the gateway. The two paths handle the auth token
|
|
4
13
|
* differently: a bring-your-own AI SDK provider sends it as the
|
|
@@ -10,15 +19,6 @@ import { EmbeddingModel, LanguageModel } from 'ai';
|
|
|
10
19
|
* @experimental
|
|
11
20
|
*/
|
|
12
21
|
type AiGatewayConsumer = "byo-provider" | "workers-ai-binding";
|
|
13
|
-
/**
|
|
14
|
-
* Project {@link AiGatewayMetadata} to a plain string-map of only its defined,
|
|
15
|
-
* non-empty fields, or `undefined` when nothing is set. This is the shared source
|
|
16
|
-
* of truth behind both gateway-correlation forms: the `cf-aig-metadata` HTTP
|
|
17
|
-
* header (bring-your-own providers) and the Workers AI binding's native
|
|
18
|
-
* `gateway.metadata` option — both encode the same `{ functionPath, traceId }`.
|
|
19
|
-
* @experimental
|
|
20
|
-
*/
|
|
21
|
-
declare const buildAiGatewayMetadataFields: (metadata: AiGatewayMetadata | undefined) => Record<string, string> | undefined;
|
|
22
22
|
/**
|
|
23
23
|
* Correlation metadata folded into the `cf-aig-metadata` request header so a
|
|
24
24
|
* gateway log entry can be tied back to the Lunora function + trace that made
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import { EmbeddingModel, LanguageModel } from 'ai';
|
|
2
|
+
/**
|
|
3
|
+
* Project {@link AiGatewayMetadata} to a plain string-map of only its defined,
|
|
4
|
+
* non-empty fields, or `undefined` when nothing is set. This is the shared source
|
|
5
|
+
* of truth behind both gateway-correlation forms: the `cf-aig-metadata` HTTP
|
|
6
|
+
* header (bring-your-own providers) and the Workers AI binding's native
|
|
7
|
+
* `gateway.metadata` option — both encode the same `{ functionPath, traceId }`.
|
|
8
|
+
* @experimental
|
|
9
|
+
*/
|
|
10
|
+
declare const buildAiGatewayMetadataFields: (metadata: AiGatewayMetadata | undefined) => Record<string, string> | undefined;
|
|
2
11
|
/**
|
|
3
12
|
* Which surface resolved the gateway. The two paths handle the auth token
|
|
4
13
|
* differently: a bring-your-own AI SDK provider sends it as the
|
|
@@ -10,15 +19,6 @@ import { EmbeddingModel, LanguageModel } from 'ai';
|
|
|
10
19
|
* @experimental
|
|
11
20
|
*/
|
|
12
21
|
type AiGatewayConsumer = "byo-provider" | "workers-ai-binding";
|
|
13
|
-
/**
|
|
14
|
-
* Project {@link AiGatewayMetadata} to a plain string-map of only its defined,
|
|
15
|
-
* non-empty fields, or `undefined` when nothing is set. This is the shared source
|
|
16
|
-
* of truth behind both gateway-correlation forms: the `cf-aig-metadata` HTTP
|
|
17
|
-
* header (bring-your-own providers) and the Workers AI binding's native
|
|
18
|
-
* `gateway.metadata` option — both encode the same `{ functionPath, traceId }`.
|
|
19
|
-
* @experimental
|
|
20
|
-
*/
|
|
21
|
-
declare const buildAiGatewayMetadataFields: (metadata: AiGatewayMetadata | undefined) => Record<string, string> | undefined;
|
|
22
22
|
/**
|
|
23
23
|
* Correlation metadata folded into the `cf-aig-metadata` request header so a
|
|
24
24
|
* gateway log entry can be tied back to the Lunora function + trace that made
|