@mdgate/ai 0.5.4 → 0.5.6
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.js +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import{ConvertError as N}from"@mdgate/core";var k=new Set(["image/jpeg","image/png","image/webp","image/gif","image/tiff","image/heic","image/bmp"]),q=new Set(["video/mp4","video/quicktime","video/webm","video/x-matroska","video/x-msvideo"]),B=["You convert a document image into GitHub-Flavored Markdown.","Transcribe all readable text exactly, in reading order.","Preserve structure: headings, paragraphs, lists, tables, block quotes, and code.","Use GFM tables when the image shows a table.","Describe charts, diagrams, stamps, signatures, and photos only when they carry meaning.","Do not mention that the source is an image.","Do not wrap the entire answer in a markdown code fence.","Do not add a preamble or postscript."].join(" "),D=["You transcribe audio into GitHub-Flavored Markdown.","Transcribe all spoken words exactly, in listening order.","Preserve structure when the speaker dictates headings, lists, or paragraphs.","Do not mention that the source is audio.","Do not wrap the entire answer in a markdown code fence.","Do not add a preamble or postscript."].join(" "),K=["You convert a video into GitHub-Flavored Markdown.","Transcribe spoken words exactly, in listening order.","Transcribe on-screen text and describe slides, diagrams, and scenes when they carry meaning.","Preserve structure: headings, paragraphs, lists, tables, and timestamps when they help reading order.","Do not mention that the source is a video.","Do not wrap the entire answer in a markdown code fence.","Do not add a preamble or postscript."].join(" ");function Y(j){let A=W("baseURL",j.baseURL).replace(/\/+$/,""),H=W("apiKey",j.apiKey),J=W("model",j.model);return{convertImage:(z)=>w({baseURL:A,apiKey:H,model:J},z),convertAudio:(z)=>F({baseURL:A,apiKey:H,model:J},z),convertVideo:(z)=>L({baseURL:A,apiKey:H,model:J},z)}}async function w(j,A){if(!(A.bytes instanceof Uint8Array)||A.bytes.length===0)throw N.unsupported("image bytes are required");if(!k.has(A.mime))throw N.unsupported(`image mime: ${String(A.mime)}`);let H=A.page===void 0?"Extract the content of this document image as markdown.":`Extract page ${A.page} of this document as markdown.`;return X(j,B,H,`data:${A.mime};base64,${Z(A.bytes)}`)}async function F(j,A){if(!(A.bytes instanceof Uint8Array)||A.bytes.length===0)throw N.unsupported("audio bytes are required");let H=typeof A.mime==="string"?A.mime.trim():"";if(H.length===0)throw N.unsupported("audio mime is required");return X(j,D,"Transcribe this audio as markdown.",`data:${H};base64,${Z(A.bytes)}`)}async function L(j,A){if(!(A.bytes instanceof Uint8Array)||A.bytes.length===0)throw N.unsupported("video bytes are required");if(!q.has(A.mime))throw N.unsupported(`video mime: ${String(A.mime)}`);return X(j,K,"Extract the content of this video as markdown.",`data:${A.mime};base64,${Z(A.bytes)}`)}async function X(j,A,H,J){let z;try{z=await fetch(`${j.baseURL}/chat/completions`,{method:"POST",headers:{authorization:`Bearer ${j.apiKey}`,"content-type":"application/json"},body:JSON.stringify({model:j.model,messages:[{role:"system",content:A},{role:"user",content:[{type:"text",text:H},{type:"image_url",image_url:{url:J}}]}]})})}catch(V){let I=V instanceof Error?V.message:String(V);throw N.io(Error(I))}let $=await z.text();if(!z.ok)throw N.io(Error(`HTTP ${z.status}: ${U($)}`));let G;try{G=JSON.parse($)}catch{throw N.io(Error("model response is not JSON"))}let Q=P(O(G));if(Q.length===0)throw N.io(Error("empty model response"));return Q.endsWith(`
|
|
2
2
|
`)?Q:`${Q}
|
|
3
|
-
`}function W(j,A){if(typeof A!=="string"||A.trim()==="")throw N.unsupported(`${j} is required`);return A.trim()}function O(j){if(typeof j!=="object"||j===null||!("choices"in j))throw N.io(Error("model response is missing choices"));let A=j.choices;if(!Array.isArray(A)||A.length===0)throw N.io(Error("model response is missing choices"));let J=A[0]?.message?.content??A[0]?.text;if(typeof J==="string")return J;if(Array.isArray(J))return J.map((z)=>{if(typeof z==="string")return z;if(typeof z==="object"&&z!==null&&"text"in z)return typeof z.text==="string"?z.text:"";return""}).join("");throw N.io(Error("model response has no text content"))}function P(j){let A=j.trim(),H=A.match(/^```(?:markdown|md)?\s*\n([\s\S]*?)\n```$/i);return(H?H[1]:A).trim()}function Z(j){let A="",H=32768;for(let J=0;J<j.length;J+=
|
|
3
|
+
`}function W(j,A){if(typeof A!=="string"||A.trim()==="")throw N.unsupported(`${j} is required`);return A.trim()}function O(j){if(typeof j!=="object"||j===null||!("choices"in j))throw N.io(Error("model response is missing choices"));let A=j.choices;if(!Array.isArray(A)||A.length===0)throw N.io(Error("model response is missing choices"));let J=A[0]?.message?.content??A[0]?.text;if(typeof J==="string")return J;if(Array.isArray(J))return J.map((z)=>{if(typeof z==="string")return z;if(typeof z==="object"&&z!==null&&"text"in z)return typeof z.text==="string"?z.text:"";return""}).join("");throw N.io(Error("model response has no text content"))}function P(j){let A=j.trim(),H=A.match(/^```(?:markdown|md)?\s*\n([\s\S]*?)\n```$/i);return(H?H[1]:A).trim()}function Z(j){let A="",H=32768;for(let J=0;J<j.length;J+=H)A+=String.fromCharCode(...j.subarray(J,J+H));return btoa(A)}function U(j){let A=j.replace(/\s+/g," ").trim();return A.length>240?`${A.slice(0,237)}...`:A}export{Y as ai};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdgate/ai",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.6",
|
|
4
4
|
"description": "Optional AI helper for mdgate: ai({ baseURL, apiKey, model }).convertImage / convertAudio / convertVideo",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"prepublishOnly": "bun run build"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@mdgate/core": "0.5.
|
|
26
|
-
"@mdgate/image": "0.5.
|
|
27
|
-
"@mdgate/video": "0.5.
|
|
25
|
+
"@mdgate/core": "0.5.6",
|
|
26
|
+
"@mdgate/image": "0.5.6",
|
|
27
|
+
"@mdgate/video": "0.5.6"
|
|
28
28
|
},
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public"
|