apple-llm 0.1.0 → 0.2.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,81 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;
2
+
3
+ var _chunkNRDZIP5Gcjs = require('./chunk-NRDZIP5G.cjs');
4
+
5
+ // src/attachments.ts
6
+ var _promises = require('fs/promises');
7
+ var _os = require('os'); var _os2 = _interopRequireDefault(_os);
8
+ var _path = require('path'); var _path2 = _interopRequireDefault(_path);
9
+ var MAX_IMAGE_BYTES = 20 * 1024 * 1024;
10
+ var EXTENSIONS = {
11
+ "image/png": ".png",
12
+ "image/jpeg": ".jpg",
13
+ "image/jpg": ".jpg",
14
+ "image/gif": ".gif",
15
+ "image/webp": ".webp",
16
+ "image/heic": ".heic",
17
+ "image/heif": ".heif",
18
+ "image/tiff": ".tiff",
19
+ "image/bmp": ".bmp"
20
+ };
21
+ var TempImages = (_class = class {constructor() { _class.prototype.__init.call(this); }
22
+
23
+ __init() {this.count = 0}
24
+ /** Write one image and return its path. */
25
+ async add(source, mediaType, signal) {
26
+ const { bytes, type } = await loadImage(source, mediaType, signal);
27
+ this.dir ??= await _promises.mkdtemp.call(void 0, _path2.default.join(_os2.default.tmpdir(), "apple-llm-img-"));
28
+ this.count += 1;
29
+ const file = _path2.default.join(this.dir, `image${this.count}${_nullishCoalesce(EXTENSIONS[type], () => ( ".img"))}`);
30
+ await _promises.writeFile.call(void 0, file, bytes);
31
+ return file;
32
+ }
33
+ async dispose() {
34
+ if (this.dir !== void 0) await _promises.rm.call(void 0, this.dir, { recursive: true, force: true }).catch(() => void 0);
35
+ this.dir = void 0;
36
+ }
37
+ }, _class);
38
+ async function loadImage(source, mediaType, signal) {
39
+ if (source instanceof Uint8Array) return { bytes: checked(source), type: _nullishCoalesce(mediaType, () => ( sniff(source))) };
40
+ if (source instanceof ArrayBuffer) {
41
+ const bytes2 = new Uint8Array(source);
42
+ return { bytes: checked(bytes2), type: _nullishCoalesce(mediaType, () => ( sniff(bytes2))) };
43
+ }
44
+ const text = source instanceof URL ? source.href : source;
45
+ const dataUrl = /^data:([^;,]+)?(;base64)?,(.*)$/s.exec(text);
46
+ if (dataUrl !== null) {
47
+ const bytes2 = dataUrl[2] !== void 0 ? Buffer.from(dataUrl[3], "base64") : Buffer.from(decodeURIComponent(dataUrl[3]), "utf8");
48
+ return { bytes: checked(bytes2), type: _nullishCoalesce(_nullishCoalesce(dataUrl[1], () => ( mediaType)), () => ( sniff(bytes2))) };
49
+ }
50
+ if (/^https?:\/\//i.test(text)) {
51
+ const response = await fetch(text, { signal });
52
+ if (!response.ok) throw new (0, _chunkNRDZIP5Gcjs.AppleLLMError)(`Could not download image ${text}: HTTP ${response.status}`);
53
+ const declared = Number(_nullishCoalesce(response.headers.get("content-length"), () => ( "0")));
54
+ if (declared > MAX_IMAGE_BYTES) throw tooLarge();
55
+ const bytes2 = new Uint8Array(await response.arrayBuffer());
56
+ const type = _optionalChain([response, 'access', _ => _.headers, 'access', _2 => _2.get, 'call', _3 => _3("content-type"), 'optionalAccess', _4 => _4.split, 'call', _5 => _5(";"), 'access', _6 => _6[0], 'optionalAccess', _7 => _7.trim, 'call', _8 => _8()]);
57
+ return { bytes: checked(bytes2), type: _nullishCoalesce(_nullishCoalesce(mediaType, () => ( type)), () => ( sniff(bytes2))) };
58
+ }
59
+ const bytes = Buffer.from(text, "base64");
60
+ if (bytes.length === 0) throw new (0, _chunkNRDZIP5Gcjs.AppleLLMError)("Image data is empty or not base64.");
61
+ return { bytes: checked(bytes), type: _nullishCoalesce(mediaType, () => ( sniff(bytes))) };
62
+ }
63
+ function checked(bytes) {
64
+ if (bytes.length > MAX_IMAGE_BYTES) throw tooLarge();
65
+ return bytes;
66
+ }
67
+ function tooLarge() {
68
+ return new (0, _chunkNRDZIP5Gcjs.AppleLLMError)(`Image is larger than ${MAX_IMAGE_BYTES / 1024 / 1024} MB.`);
69
+ }
70
+ function sniff(bytes) {
71
+ const at = (i) => _nullishCoalesce(bytes[i], () => ( -1));
72
+ if (at(0) === 137 && at(1) === 80 && at(2) === 78 && at(3) === 71) return "image/png";
73
+ if (at(0) === 255 && at(1) === 216) return "image/jpeg";
74
+ if (at(0) === 71 && at(1) === 73 && at(2) === 70) return "image/gif";
75
+ if (at(8) === 87 && at(9) === 69 && at(10) === 66 && at(11) === 80) return "image/webp";
76
+ return "application/octet-stream";
77
+ }
78
+
79
+
80
+
81
+ exports.TempImages = TempImages;
@@ -0,0 +1,81 @@
1
+ import {
2
+ AppleLLMError
3
+ } from "./chunk-GM325EMJ.js";
4
+
5
+ // src/attachments.ts
6
+ import { mkdtemp, rm, writeFile } from "fs/promises";
7
+ import os from "os";
8
+ import path from "path";
9
+ var MAX_IMAGE_BYTES = 20 * 1024 * 1024;
10
+ var EXTENSIONS = {
11
+ "image/png": ".png",
12
+ "image/jpeg": ".jpg",
13
+ "image/jpg": ".jpg",
14
+ "image/gif": ".gif",
15
+ "image/webp": ".webp",
16
+ "image/heic": ".heic",
17
+ "image/heif": ".heif",
18
+ "image/tiff": ".tiff",
19
+ "image/bmp": ".bmp"
20
+ };
21
+ var TempImages = class {
22
+ dir;
23
+ count = 0;
24
+ /** Write one image and return its path. */
25
+ async add(source, mediaType, signal) {
26
+ const { bytes, type } = await loadImage(source, mediaType, signal);
27
+ this.dir ??= await mkdtemp(path.join(os.tmpdir(), "apple-llm-img-"));
28
+ this.count += 1;
29
+ const file = path.join(this.dir, `image${this.count}${EXTENSIONS[type] ?? ".img"}`);
30
+ await writeFile(file, bytes);
31
+ return file;
32
+ }
33
+ async dispose() {
34
+ if (this.dir !== void 0) await rm(this.dir, { recursive: true, force: true }).catch(() => void 0);
35
+ this.dir = void 0;
36
+ }
37
+ };
38
+ async function loadImage(source, mediaType, signal) {
39
+ if (source instanceof Uint8Array) return { bytes: checked(source), type: mediaType ?? sniff(source) };
40
+ if (source instanceof ArrayBuffer) {
41
+ const bytes2 = new Uint8Array(source);
42
+ return { bytes: checked(bytes2), type: mediaType ?? sniff(bytes2) };
43
+ }
44
+ const text = source instanceof URL ? source.href : source;
45
+ const dataUrl = /^data:([^;,]+)?(;base64)?,(.*)$/s.exec(text);
46
+ if (dataUrl !== null) {
47
+ const bytes2 = dataUrl[2] !== void 0 ? Buffer.from(dataUrl[3], "base64") : Buffer.from(decodeURIComponent(dataUrl[3]), "utf8");
48
+ return { bytes: checked(bytes2), type: dataUrl[1] ?? mediaType ?? sniff(bytes2) };
49
+ }
50
+ if (/^https?:\/\//i.test(text)) {
51
+ const response = await fetch(text, { signal });
52
+ if (!response.ok) throw new AppleLLMError(`Could not download image ${text}: HTTP ${response.status}`);
53
+ const declared = Number(response.headers.get("content-length") ?? "0");
54
+ if (declared > MAX_IMAGE_BYTES) throw tooLarge();
55
+ const bytes2 = new Uint8Array(await response.arrayBuffer());
56
+ const type = response.headers.get("content-type")?.split(";")[0]?.trim();
57
+ return { bytes: checked(bytes2), type: mediaType ?? type ?? sniff(bytes2) };
58
+ }
59
+ const bytes = Buffer.from(text, "base64");
60
+ if (bytes.length === 0) throw new AppleLLMError("Image data is empty or not base64.");
61
+ return { bytes: checked(bytes), type: mediaType ?? sniff(bytes) };
62
+ }
63
+ function checked(bytes) {
64
+ if (bytes.length > MAX_IMAGE_BYTES) throw tooLarge();
65
+ return bytes;
66
+ }
67
+ function tooLarge() {
68
+ return new AppleLLMError(`Image is larger than ${MAX_IMAGE_BYTES / 1024 / 1024} MB.`);
69
+ }
70
+ function sniff(bytes) {
71
+ const at = (i) => bytes[i] ?? -1;
72
+ if (at(0) === 137 && at(1) === 80 && at(2) === 78 && at(3) === 71) return "image/png";
73
+ if (at(0) === 255 && at(1) === 216) return "image/jpeg";
74
+ if (at(0) === 71 && at(1) === 73 && at(2) === 70) return "image/gif";
75
+ if (at(8) === 87 && at(9) === 69 && at(10) === 66 && at(11) === 80) return "image/webp";
76
+ return "application/octet-stream";
77
+ }
78
+
79
+ export {
80
+ TempImages
81
+ };