UNPKG

grix-connector

Version:

Connect local AI coding agents (Claude, Codex, Gemini, Qwen, DeepSeek, Cursor, OpenCode, Pi, OpenHuman, Reasonix) to the Grix scheduling platform. Also serves as an OpenClaw plugin for Grix channel transport.

2 lines (1 loc) 1.9 kB
import{readReplyFile as l,buildAttachmentExtra as p}from"./attachment-file.js";import{normalizeString as n}from"../util/normalize-string.js";function i(t){return t.replace(/\/+$/u,"")}function u(t){return n(t)?JSON.parse(t):{}}function d(t){const e=new URL(n(t));if(e.protocol!=="ws:"&&e.protocol!=="wss:")throw new Error("ws_url must start with ws:// or wss://");const o=i(e.pathname).replace(/\/ws$/u,"");if(!o||o===i(e.pathname))throw new Error("ws_url must end with /ws");return e.protocol=e.protocol==="wss:"?"https:":"http:",e.pathname=`${o}/oss/presign`,e.search="",e.hash="",e.toString()}async function w(t){const e=await fetch(d(t.wsURL),{method:"POST",headers:{Authorization:`Bearer ${n(t.apiKey)}`,"Content-Type":"application/json"},body:JSON.stringify({session_id:n(t.sessionID),filename:n(t.fileName),content_type:n(t.contentType)})}),o=await e.text(),s=u(o);if(!e.ok||Number(s?.code??-1)!==0){const c=n(s?.msg)||e.statusText||"agent media presign failed";throw new Error(`agent media presign failed: ${c}`)}const r=n(s?.data?.upload_url),a=n(s?.data?.media_access_url);if(!r||!a)throw new Error("agent media presign returned incomplete upload_url/media_access_url");return{upload_url:r,access_url:a}}async function y(t){const e=await fetch(t.uploadURL,{method:"PUT",headers:{"Content-Type":n(t.contentType)},body:new Uint8Array(t.bytes)});if(!e.ok)throw new Error(`agent media upload failed: ${e.status} ${e.statusText}`)}async function m(t){const e=await l(t.filePath),o=await w({wsURL:t.wsURL,apiKey:t.apiKey,sessionID:t.sessionID,fileName:e.file_name,contentType:e.content_type});return await y({uploadURL:o.upload_url,contentType:e.content_type,bytes:e.bytes}),{...e,access_url:o.access_url,extra:p({attachmentType:e.attachment_type,fileName:e.file_name,accessURL:o.access_url,contentType:e.content_type})}}export{d as resolveAgentAPIPresignURL,m as uploadReplyFileToAgentMedia};