openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.2 kB
JavaScript
import{n as validateJsonSchemaValue}from"../../schema-validator-_8KiTdxO.mjs";import{S as createAssistantMessageEventStream}from"../../llm-CyOhT4cY.mjs";import"../../json-schema-runtime-2xhM839K.mjs";import{r as quoteUnsafeIntegerLiterals}from"../../json-unsafe-integers-jXdM5NTH.mjs";import{t as buildAssistantMessage}from"../../stream-message-shared-BaDpqUWO.mjs";import{f as createEmptyTransportUsage,i as applyProviderPayloadHook,y as failTransportStream}from"../../provider-transport-runtime-CHA37wCS.mjs";
//!extensions/apple-fm/stream.ts
function createAppleFmStream(e){return(t,n,r)=>{const i=createAssistantMessageEventStream();const a=buildAssistantMessage({model:t,content:[],usage:createEmptyTransportUsage(),stopReason:`stop`});i.push({type:`start`,partial:a});(async()=>{try{r?.signal?.throwIfAborted();const o=r?.responseFormat;const s={systemPrompt:n.systemPrompt,messages:n.messages,tools:n.tools??[],maxTokens:r?.maxTokens??t.maxTokens,temperature:r?.temperature,responseFormat:o?structuredClone(o):void 0};const c=await applyProviderPayloadHook(r,s,t)??s;if(!c||typeof c!==`object`)throw new Error(`Apple Foundation Models requires an object request.`);r?.signal?.throwIfAborted();const l=await e.run(c,{signal:r?.signal});r?.signal?.throwIfAborted();if(o&&l.toolCalls.length===0){const e=`Apple Foundation Models returned an invalid structured response: an unsafe numeric value cannot be validated without losing precision.`;if(quoteUnsafeIntegerLiterals(l.text)!==l.text)throw new Error(e);let t;try{t=JSON.parse(l.text,(t,n)=>{if(typeof n===`number`&&(!Number.isFinite(n)||Number.isInteger(n)&&!Number.isSafeInteger(n)))throw new Error(e);return n})}catch(e){if(!(e instanceof SyntaxError))throw e}if(t===void 0)throw new Error(`Apple Foundation Models returned an invalid structured response: malformed JSON.`);const n=validateJsonSchemaValue({schema:o,cacheKey:`apple-fm.structured-response`,value:t,applyDefaults:false});if(!n.ok)throw new Error(`Apple Foundation Models returned an invalid structured response at ${n.errors.map(e=>e.path).join(`, `)}.`)}a.usage.input=l.inputTokens;a.usage.output=l.outputTokens;a.usage.totalTokens=l.inputTokens+l.outputTokens;a.usage.cacheTelemetry={state:`unavailable`};a.usage.contextUsage={state:`available`,promptTokens:l.inputTokens,totalTokens:a.usage.totalTokens};if(l.text){a.content.push({type:`text`,text:l.text});i.push({type:`text_start`,contentIndex:0,partial:a});i.push({type:`text_delta`,contentIndex:0,delta:l.text,partial:a});i.push({type:`text_end`,contentIndex:0,content:l.text,partial:a})}for(const e of l.toolCalls){const t={type:`toolCall`,...e};const n={...t,partialJson:JSON.stringify(e.arguments)};const r=a.content.length;a.content.push(t);i.push({type:`toolcall_start`,contentIndex:r,partial:a});i.push({type:`toolcall_delta`,contentIndex:r,delta:n.partialJson,partial:{...a,content:[...a.content.slice(0,-1),n]}});i.push({type:`toolcall_end`,contentIndex:r,toolCall:t,partial:a})}a.stopReason=l.toolCalls.length?`toolUse`:`stop`;i.push({type:`done`,reason:a.stopReason,message:a});i.end(a)}catch(e){failTransportStream({stream:i,output:a,error:e,signal:r?.signal})}})();return i}}export{createAppleFmStream};