@amaster.ai/pi-attachments 0.1.0-beta.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.
- package/LICENSE +201 -0
- package/README.md +37 -0
- package/dist/classify.d.ts +8 -0
- package/dist/classify.d.ts.map +1 -0
- package/dist/classify.js +108 -0
- package/dist/classify.js.map +1 -0
- package/dist/diagnostics.d.ts +8 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/diagnostics.js +30 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/http.d.ts +10 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +44 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/local-store.d.ts +20 -0
- package/dist/local-store.d.ts.map +1 -0
- package/dist/local-store.js +118 -0
- package/dist/local-store.js.map +1 -0
- package/dist/multipart.d.ts +9 -0
- package/dist/multipart.d.ts.map +1 -0
- package/dist/multipart.js +48 -0
- package/dist/multipart.js.map +1 -0
- package/dist/normalize.d.ts +7 -0
- package/dist/normalize.d.ts.map +1 -0
- package/dist/normalize.js +83 -0
- package/dist/normalize.js.map +1 -0
- package/dist/parser.d.ts +12 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +166 -0
- package/dist/parser.js.map +1 -0
- package/dist/prompt.d.ts +10 -0
- package/dist/prompt.d.ts.map +1 -0
- package/dist/prompt.js +35 -0
- package/dist/prompt.js.map +1 -0
- package/dist/remote-fetch.d.ts +10 -0
- package/dist/remote-fetch.d.ts.map +1 -0
- package/dist/remote-fetch.js +34 -0
- package/dist/remote-fetch.js.map +1 -0
- package/dist/routes.d.ts +14 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/routes.js +89 -0
- package/dist/routes.js.map +1 -0
- package/dist/service.d.ts +10 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +134 -0
- package/dist/service.js.map +1 -0
- package/dist/types.d.ts +85 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/upload-proxy.d.ts +19 -0
- package/dist/upload-proxy.d.ts.map +1 -0
- package/dist/upload-proxy.js +105 -0
- package/dist/upload-proxy.js.map +1 -0
- package/package.json +39 -0
package/dist/routes.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { AttachmentHttpError, readRequestBody, writeJson } from "./http.js";
|
|
2
|
+
import { parseMultipartBoundary, parseMultipartFiles } from "./multipart.js";
|
|
3
|
+
import { proxyAttachmentUpload } from "./upload-proxy.js";
|
|
4
|
+
export async function handleAttachmentRoutes(input) {
|
|
5
|
+
if (input.request.method === "POST" && input.url.pathname === "/v1/attachments/upload") {
|
|
6
|
+
if (input.service.config.storageMode === "platform") {
|
|
7
|
+
await proxyAttachmentUpload({
|
|
8
|
+
request: input.request,
|
|
9
|
+
response: input.response,
|
|
10
|
+
uploadEndpoint: input.service.config.uploadEndpoint,
|
|
11
|
+
allowInsecureLocalTls: input.service.config.allowInsecureLocalUploadTls,
|
|
12
|
+
auth: input.uploadAuth,
|
|
13
|
+
maxBodyBytes: input.maxUploadBodyBytes,
|
|
14
|
+
});
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
const files = await readMultipartFiles(input.request, input.maxUploadBodyBytes);
|
|
19
|
+
const attachments = [];
|
|
20
|
+
for (const file of files) {
|
|
21
|
+
const record = await input.service.store.putBuffer({
|
|
22
|
+
data: file.data,
|
|
23
|
+
name: file.fileName,
|
|
24
|
+
...(file.contentType ? { mimeType: file.contentType } : {}),
|
|
25
|
+
sessionId: input.context.sessionId,
|
|
26
|
+
});
|
|
27
|
+
attachments.push(toStoredAttachment(record));
|
|
28
|
+
}
|
|
29
|
+
writeJson(input.response, 200, { attachments, attachment: attachments[0] });
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
writeRouteError(input.response, error);
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
if (input.request.method === "POST" && input.url.pathname === "/v1/attachments/register-local") {
|
|
37
|
+
if (!input.service.config.desktopEnabled) {
|
|
38
|
+
writeJson(input.response, 404, { error: "local attachment registration is only available in desktop mode" });
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
const body = JSON.parse((await readRequestBody(input.request, input.maxUploadBodyBytes)).toString("utf8"));
|
|
43
|
+
const files = Array.isArray(body.files) ? body.files : [];
|
|
44
|
+
const attachments = [];
|
|
45
|
+
for (const file of files) {
|
|
46
|
+
if (!file.path) {
|
|
47
|
+
throw new Error("file path is required");
|
|
48
|
+
}
|
|
49
|
+
const record = await input.service.store.putFile({
|
|
50
|
+
sourcePath: file.path,
|
|
51
|
+
...(file.name ? { name: file.name } : {}),
|
|
52
|
+
...(file.mimeType ? { mimeType: file.mimeType } : {}),
|
|
53
|
+
sessionId: input.context.sessionId,
|
|
54
|
+
});
|
|
55
|
+
attachments.push(toStoredAttachment(record));
|
|
56
|
+
}
|
|
57
|
+
writeJson(input.response, 200, { attachments });
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
writeRouteError(input.response, error);
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
async function readMultipartFiles(request, maxBytes) {
|
|
67
|
+
const boundary = parseMultipartBoundary(request.headers["content-type"]);
|
|
68
|
+
if (!boundary) {
|
|
69
|
+
throw new AttachmentHttpError(400, "multipart_boundary_required", "multipart boundary is required");
|
|
70
|
+
}
|
|
71
|
+
return parseMultipartFiles(await readRequestBody(request, maxBytes), boundary);
|
|
72
|
+
}
|
|
73
|
+
function toStoredAttachment(record) {
|
|
74
|
+
return {
|
|
75
|
+
id: record.attachmentId,
|
|
76
|
+
name: record.name,
|
|
77
|
+
...(record.mimeType ? { mimeType: record.mimeType } : {}),
|
|
78
|
+
size: record.size,
|
|
79
|
+
source: { kind: "storedFile", attachmentId: record.attachmentId },
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function writeRouteError(response, error) {
|
|
83
|
+
if (error instanceof AttachmentHttpError) {
|
|
84
|
+
writeJson(response, error.statusCode, { error: error.message, code: error.code });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
writeJson(response, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=routes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.js","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC5E,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EAAE,qBAAqB,EAA6B,MAAM,mBAAmB,CAAC;AAErF,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAAC,KAQ5C;IACC,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,KAAK,MAAM,IAAI,KAAK,CAAC,GAAG,CAAC,QAAQ,KAAK,wBAAwB,EAAE,CAAC;QACvF,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,WAAW,KAAK,UAAU,EAAE,CAAC;YACpD,MAAM,qBAAqB,CAAC;gBAC1B,OAAO,EAAE,KAAK,CAAC,OAAO;gBACtB,QAAQ,EAAE,KAAK,CAAC,QAAQ;gBACxB,cAAc,EAAE,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,cAAc;gBACnD,qBAAqB,EAAE,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,2BAA2B;gBACvE,IAAI,EAAE,KAAK,CAAC,UAAU;gBACtB,YAAY,EAAE,KAAK,CAAC,kBAAkB;aACvC,CAAC,CAAC;YACH,OAAO,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,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;AAC9F,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AttachmentContext } from "./types.js";
|
|
2
|
+
import type { AttachmentServiceConfig, PreparedAttachmentBundle } from "./types.js";
|
|
3
|
+
import { LocalAttachmentStore } from "./local-store.js";
|
|
4
|
+
export type AttachmentService = {
|
|
5
|
+
readonly config: AttachmentServiceConfig;
|
|
6
|
+
readonly store: LocalAttachmentStore;
|
|
7
|
+
prepareForPrompt(attachments: unknown, context: AttachmentContext): Promise<PreparedAttachmentBundle>;
|
|
8
|
+
};
|
|
9
|
+
export declare function createAttachmentService(config: AttachmentServiceConfig): AttachmentService;
|
|
10
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,KAAK,EAGV,uBAAuB,EAEvB,wBAAwB,EAEzB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAMxD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,CAAC,MAAM,EAAE,uBAAuB,CAAC;IACzC,QAAQ,CAAC,KAAK,EAAE,oBAAoB,CAAC;IACrC,gBAAgB,CAAC,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAAC;CACvG,CAAC;AAEF,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,uBAAuB,GAAG,iBAAiB,CAM1F"}
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { isDocumentAttachment, isImageMimeType, resolveAttachmentMimeType } from "./classify.js";
|
|
2
|
+
import { LocalAttachmentStore } from "./local-store.js";
|
|
3
|
+
import { normalizeAttachments } from "./normalize.js";
|
|
4
|
+
import { BasicAttachmentParser, LiteParseAttachmentParser } from "./parser.js";
|
|
5
|
+
import { attachmentHeader, truncateText } from "./prompt.js";
|
|
6
|
+
import { fetchRemoteAttachment } from "./remote-fetch.js";
|
|
7
|
+
export function createAttachmentService(config) {
|
|
8
|
+
const store = new LocalAttachmentStore(config.localStoreDir);
|
|
9
|
+
const parser = config.parser === "liteparse"
|
|
10
|
+
? new LiteParseAttachmentParser()
|
|
11
|
+
: new BasicAttachmentParser();
|
|
12
|
+
return new DefaultAttachmentService(config, store, parser);
|
|
13
|
+
}
|
|
14
|
+
class DefaultAttachmentService {
|
|
15
|
+
config;
|
|
16
|
+
store;
|
|
17
|
+
parser;
|
|
18
|
+
constructor(config, store, parser) {
|
|
19
|
+
this.config = config;
|
|
20
|
+
this.store = store;
|
|
21
|
+
this.parser = parser;
|
|
22
|
+
}
|
|
23
|
+
async prepareForPrompt(attachmentsInput, context) {
|
|
24
|
+
const attachments = normalizeAttachments(attachmentsInput, this.config.maxAttachmentCount);
|
|
25
|
+
const images = [];
|
|
26
|
+
const promptBlocks = [];
|
|
27
|
+
const failures = [];
|
|
28
|
+
const telemetry = [];
|
|
29
|
+
for (const [index, attachment] of attachments.entries()) {
|
|
30
|
+
try {
|
|
31
|
+
const result = await this.prepareAttachment(attachment, index, context);
|
|
32
|
+
if (result.image) {
|
|
33
|
+
images.push(result.image);
|
|
34
|
+
}
|
|
35
|
+
if (result.promptBlock) {
|
|
36
|
+
promptBlocks.push(result.promptBlock);
|
|
37
|
+
}
|
|
38
|
+
telemetry.push(result.telemetry);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
42
|
+
failures.push({ attachmentId: attachment.id, name: attachment.name, reason });
|
|
43
|
+
promptBlocks.push(`${attachmentHeader({ index, ...attachment })}\nContent: attachment could not be processed (${reason}).`);
|
|
44
|
+
telemetry.push({ id: attachment.id, name: attachment.name, status: "failed", error: reason });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return { attachments, images, promptBlocks, failures, telemetry };
|
|
48
|
+
}
|
|
49
|
+
async prepareAttachment(attachment, index, context) {
|
|
50
|
+
const header = attachmentHeader({ index, ...attachment });
|
|
51
|
+
if (attachment.source.kind === "inlineText") {
|
|
52
|
+
const text = truncateText(attachment.source.text, this.config.maxTextChars);
|
|
53
|
+
return {
|
|
54
|
+
promptBlock: `${header}\nContent:\n${text}`,
|
|
55
|
+
telemetry: attachmentTelemetry(attachment, { status: "inline_text", textBytes: text.length }),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const local = await this.resolveToLocalFile(attachment, context);
|
|
59
|
+
const mimeType = resolveAttachmentMimeType(local.name, local.mimeType ?? attachment.mimeType);
|
|
60
|
+
if (isImageMimeType(mimeType)) {
|
|
61
|
+
const { readFile } = await import("node:fs/promises");
|
|
62
|
+
const data = await readFile(local.path);
|
|
63
|
+
if (data.byteLength > this.config.maxBytes) {
|
|
64
|
+
throw new Error(`attachment exceeds ${this.config.maxBytes} bytes`);
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
image: {
|
|
68
|
+
type: "image",
|
|
69
|
+
data: data.toString("base64"),
|
|
70
|
+
mimeType,
|
|
71
|
+
},
|
|
72
|
+
promptBlock: `${header}\nContent: image upload is attached directly as model-visible image content. Do not call file tools for this uploaded image.`,
|
|
73
|
+
telemetry: attachmentTelemetry(attachment, { status: "image", bytes: data.byteLength }),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
if (this.config.parseEnabled && isDocumentAttachment(local.name, mimeType)) {
|
|
77
|
+
const parsed = await this.parser.parse({
|
|
78
|
+
path: local.path,
|
|
79
|
+
name: local.name,
|
|
80
|
+
...(mimeType ? { mimeType } : {}),
|
|
81
|
+
format: "text",
|
|
82
|
+
ocr: this.config.ocr,
|
|
83
|
+
maxPages: this.config.maxPages,
|
|
84
|
+
});
|
|
85
|
+
const text = truncateText(parsed.text, this.config.maxTextChars);
|
|
86
|
+
return {
|
|
87
|
+
promptBlock: `${header}\nContent:\n${text}`,
|
|
88
|
+
telemetry: attachmentTelemetry(attachment, {
|
|
89
|
+
status: "parsed",
|
|
90
|
+
textBytes: text.length,
|
|
91
|
+
...(parsed.pageCount !== undefined ? { pageCount: parsed.pageCount } : {}),
|
|
92
|
+
}),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
promptBlock: `${header}\nContent: non-text attachment is available as metadata only.`,
|
|
97
|
+
telemetry: attachmentTelemetry(attachment, { status: "metadata_only" }),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
async resolveToLocalFile(attachment, context) {
|
|
101
|
+
if (attachment.source.kind === "storedFile") {
|
|
102
|
+
return this.store.readRecord(attachment.source.attachmentId);
|
|
103
|
+
}
|
|
104
|
+
if (attachment.source.kind === "remoteObject") {
|
|
105
|
+
const fetched = await fetchRemoteAttachment({
|
|
106
|
+
url: attachment.source.url,
|
|
107
|
+
maxBytes: this.config.maxBytes,
|
|
108
|
+
timeoutMs: this.config.fetchTimeoutMs,
|
|
109
|
+
...(attachment.mimeType ? { fallbackMimeType: attachment.mimeType } : {}),
|
|
110
|
+
});
|
|
111
|
+
return this.store.putBuffer({
|
|
112
|
+
data: fetched.data,
|
|
113
|
+
name: attachment.name,
|
|
114
|
+
sessionId: context.sessionId,
|
|
115
|
+
...optionalMimeType(fetched.mimeType ?? attachment.mimeType),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
throw new Error("inline text attachment does not resolve to a local file");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function optionalMimeType(mimeType) {
|
|
122
|
+
return mimeType ? { mimeType } : {};
|
|
123
|
+
}
|
|
124
|
+
function attachmentTelemetry(attachment, extra) {
|
|
125
|
+
return {
|
|
126
|
+
id: attachment.id,
|
|
127
|
+
name: attachment.name,
|
|
128
|
+
...(attachment.mimeType ? { mimeType: attachment.mimeType } : {}),
|
|
129
|
+
...(attachment.size !== undefined ? { size: attachment.size } : {}),
|
|
130
|
+
sourceKind: attachment.source.kind,
|
|
131
|
+
...extra,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,yBAAyB,EAAE,MAAM,eAAe,CAAC;AACjG,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,yBAAyB,EAAyB,MAAM,aAAa,CAAC;AACtG,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAQ1D,MAAM,UAAU,uBAAuB,CAAC,MAA+B;IACrE,MAAM,KAAK,GAAG,IAAI,oBAAoB,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;IAC7D,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,KAAK,WAAW;QAC1C,CAAC,CAAC,IAAI,yBAAyB,EAAE;QACjC,CAAC,CAAC,IAAI,qBAAqB,EAAE,CAAC;IAChC,OAAO,IAAI,wBAAwB,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AAC7D,CAAC;AAED,MAAM,wBAAwB;IAEjB;IACA;IACQ;IAHnB,YACW,MAA+B,EAC/B,KAA2B,EACnB,MAAwB;QAFhC,WAAM,GAAN,MAAM,CAAyB;QAC/B,UAAK,GAAL,KAAK,CAAsB;QACnB,WAAM,GAAN,MAAM,CAAkB;IACxC,CAAC;IAEJ,KAAK,CAAC,gBAAgB,CAAC,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C,CAAC;QACL,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;IAC7E,CAAC;CACF;AAED,SAAS,gBAAgB,CAAC,QAA4B;IACpD,OAAO,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;AACtC,CAAC;AAED,SAAS,mBAAmB,CAC1B,UAAgC,EAChC,KAA2B;IAE3B,OAAO;QACL,EAAE,EAAE,UAAU,CAAC,EAAE;QACjB,IAAI,EAAE,UAAU,CAAC,IAAI;QACrB,GAAG,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,UAAU,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACjE,GAAG,CAAC,UAAU,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACnE,UAAU,EAAE,UAAU,CAAC,MAAM,CAAC,IAAI;QAClC,GAAG,KAAK;KACT,CAAC;AACJ,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export type AttachmentJsonObject = {
|
|
2
|
+
[key: string]: AttachmentJsonValue | undefined;
|
|
3
|
+
};
|
|
4
|
+
export type AttachmentJsonValue = string | number | boolean | null | AttachmentJsonObject | AttachmentJsonValue[];
|
|
5
|
+
export type AttachmentImageContent = {
|
|
6
|
+
type: "image";
|
|
7
|
+
mimeType: string;
|
|
8
|
+
data: string;
|
|
9
|
+
};
|
|
10
|
+
export type AttachmentSource = {
|
|
11
|
+
kind: "inlineText";
|
|
12
|
+
text: string;
|
|
13
|
+
truncated?: boolean;
|
|
14
|
+
} | {
|
|
15
|
+
kind: "remoteObject";
|
|
16
|
+
url: string;
|
|
17
|
+
key?: string;
|
|
18
|
+
} | {
|
|
19
|
+
kind: "storedFile";
|
|
20
|
+
attachmentId: string;
|
|
21
|
+
};
|
|
22
|
+
export type ChatAttachmentInput = {
|
|
23
|
+
id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
mimeType?: string;
|
|
26
|
+
size?: number;
|
|
27
|
+
source: AttachmentSource;
|
|
28
|
+
};
|
|
29
|
+
export type NormalizedAttachment = ChatAttachmentInput;
|
|
30
|
+
export type PreparedAttachmentBundle = {
|
|
31
|
+
attachments: NormalizedAttachment[];
|
|
32
|
+
images: AttachmentImageContent[];
|
|
33
|
+
promptBlocks: string[];
|
|
34
|
+
failures: AttachmentFailure[];
|
|
35
|
+
telemetry: AttachmentJsonObject[];
|
|
36
|
+
};
|
|
37
|
+
export type AttachmentFailure = {
|
|
38
|
+
attachmentId: string;
|
|
39
|
+
name: string;
|
|
40
|
+
reason: string;
|
|
41
|
+
};
|
|
42
|
+
export type AttachmentContext = {
|
|
43
|
+
tenantId?: string;
|
|
44
|
+
userId?: string;
|
|
45
|
+
workspaceId?: string;
|
|
46
|
+
sessionId: string;
|
|
47
|
+
traceId?: string;
|
|
48
|
+
};
|
|
49
|
+
export type AttachmentServiceConfig = {
|
|
50
|
+
storageMode: "platform" | "local";
|
|
51
|
+
uploadEndpoint: string;
|
|
52
|
+
allowInsecureLocalUploadTls: boolean;
|
|
53
|
+
localStoreDir: string;
|
|
54
|
+
maxAttachmentCount: number;
|
|
55
|
+
maxBytes: number;
|
|
56
|
+
maxTextChars: number;
|
|
57
|
+
fetchTimeoutMs: number;
|
|
58
|
+
parseEnabled: boolean;
|
|
59
|
+
parser: "liteparse" | "basic";
|
|
60
|
+
ocr: "off" | "auto";
|
|
61
|
+
maxPages: number;
|
|
62
|
+
desktopEnabled: boolean;
|
|
63
|
+
};
|
|
64
|
+
export type StoredAttachmentRecord = {
|
|
65
|
+
attachmentId: string;
|
|
66
|
+
name: string;
|
|
67
|
+
mimeType?: string;
|
|
68
|
+
size: number;
|
|
69
|
+
path: string;
|
|
70
|
+
createdAt: string;
|
|
71
|
+
};
|
|
72
|
+
export type ParseAttachmentInput = {
|
|
73
|
+
path: string;
|
|
74
|
+
name: string;
|
|
75
|
+
mimeType?: string;
|
|
76
|
+
format?: "text" | "json";
|
|
77
|
+
ocr: "off" | "auto";
|
|
78
|
+
maxPages: number;
|
|
79
|
+
};
|
|
80
|
+
export type ParsedAttachment = {
|
|
81
|
+
text: string;
|
|
82
|
+
pageCount?: number;
|
|
83
|
+
warnings?: string[];
|
|
84
|
+
};
|
|
85
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,oBAAoB,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,mBAAmB,GAAG,SAAS,CAAA;CAAE,CAAC;AACtF,MAAM,MAAM,mBAAmB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,oBAAoB,GAAG,mBAAmB,EAAE,CAAC;AAElH,MAAM,MAAM,sBAAsB,GAAG;IACnC,IAAI,EAAE,OAAO,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,gBAAgB,GACxB;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,OAAO,CAAA;CAAE,GACzD;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GACnD;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAAC;AAEjD,MAAM,MAAM,mBAAmB,GAAG;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI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AAY,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,GAAG,EAAE,KAAK,GAAG,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
export type AttachmentUploadAuth = {
|
|
3
|
+
enabled: boolean;
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
internalBaseUrl?: string;
|
|
6
|
+
accessToken?: string;
|
|
7
|
+
user?: unknown;
|
|
8
|
+
userHeaders?: (user: unknown) => Record<string, string>;
|
|
9
|
+
};
|
|
10
|
+
export declare function proxyAttachmentUpload(input: {
|
|
11
|
+
request: IncomingMessage;
|
|
12
|
+
response: ServerResponse;
|
|
13
|
+
uploadEndpoint: string;
|
|
14
|
+
allowInsecureLocalTls: boolean;
|
|
15
|
+
auth: AttachmentUploadAuth;
|
|
16
|
+
maxBodyBytes: number;
|
|
17
|
+
}): Promise<void>;
|
|
18
|
+
export declare function isLocalUploadHost(hostname: string): boolean;
|
|
19
|
+
//# sourceMappingURL=upload-proxy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upload-proxy.d.ts","sourceRoot":"","sources":["../src/upload-proxy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAIjE,MAAM,MAAM,oBAAoB,GAAG;IACjC,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACzD,CAAC;AAEF,wBAAsB,qBAAqB,CAAC,KAAK,EAAE;IACjD,OAAO,EAAE,eAAe,CAAC;IACzB,QAAQ,EAAE,cAAc,CAAC;IACzB,cAAc,EAAE,MAAM,CAAC;IACvB,qBAAqB,EAAE,OAAO,CAAC;IAC/B,IAAI,EAAE,oBAAoB,CAAC;IAC3B,YAAY,EAAE,MAAM,CAAC;CACtB,GAAG,OAAO,CAAC,IAAI,CAAC,CAoChB;AAqDD,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAO3D"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { request as httpRequest } from "node:http";
|
|
2
|
+
import { Agent as HttpsAgent, request as httpsRequest } from "node:https";
|
|
3
|
+
import { getHeader, readRequestBody, writeJson } from "./http.js";
|
|
4
|
+
export async function proxyAttachmentUpload(input) {
|
|
5
|
+
if (input.auth.enabled && !input.auth.accessToken) {
|
|
6
|
+
writeJson(input.response, 401, { error: "authentication is required for attachment upload" });
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const targetUrl = resolveUploadUrl(input.uploadEndpoint, input.auth);
|
|
10
|
+
const body = await readRequestBody(input.request, input.maxBodyBytes);
|
|
11
|
+
const headers = {
|
|
12
|
+
"content-length": String(body.length),
|
|
13
|
+
};
|
|
14
|
+
const contentType = getHeader(input.request, "content-type");
|
|
15
|
+
if (contentType) {
|
|
16
|
+
headers["content-type"] = contentType;
|
|
17
|
+
}
|
|
18
|
+
if (input.auth.accessToken) {
|
|
19
|
+
headers.authorization = `Bearer ${input.auth.accessToken}`;
|
|
20
|
+
}
|
|
21
|
+
if (input.auth.user && input.auth.userHeaders) {
|
|
22
|
+
Object.assign(headers, input.auth.userHeaders(input.auth.user));
|
|
23
|
+
}
|
|
24
|
+
let upstream;
|
|
25
|
+
try {
|
|
26
|
+
upstream = await proxyHttpRequest(targetUrl, headers, body, {
|
|
27
|
+
allowInsecureLocalTls: input.allowInsecureLocalTls,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
writeJson(input.response, 502, {
|
|
32
|
+
error: `attachment upload proxy failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
33
|
+
upstream: redactUrlForClient(targetUrl),
|
|
34
|
+
});
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
input.response.writeHead(upstream.status, {
|
|
38
|
+
"content-type": upstream.contentType ?? "application/json; charset=utf-8",
|
|
39
|
+
});
|
|
40
|
+
input.response.end(upstream.body);
|
|
41
|
+
}
|
|
42
|
+
function resolveUploadUrl(uploadEndpoint, auth) {
|
|
43
|
+
if (/^https?:\/\//i.test(uploadEndpoint)) {
|
|
44
|
+
return uploadEndpoint;
|
|
45
|
+
}
|
|
46
|
+
const baseUrl = auth.internalBaseUrl ?? auth.baseUrl;
|
|
47
|
+
if (!baseUrl) {
|
|
48
|
+
throw new Error("upload endpoint must be absolute when upload base url is not configured");
|
|
49
|
+
}
|
|
50
|
+
return new URL(uploadEndpoint, baseUrl).toString();
|
|
51
|
+
}
|
|
52
|
+
function proxyHttpRequest(targetUrl, headers, body, options) {
|
|
53
|
+
const url = new URL(targetUrl);
|
|
54
|
+
const requestImpl = url.protocol === "https:" ? httpsRequest : httpRequest;
|
|
55
|
+
const allowInsecureLocalTls = url.protocol === "https:" &&
|
|
56
|
+
options.allowInsecureLocalTls &&
|
|
57
|
+
isLocalUploadHost(url.hostname);
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
const upstream = requestImpl({
|
|
60
|
+
protocol: url.protocol,
|
|
61
|
+
hostname: url.hostname,
|
|
62
|
+
port: url.port,
|
|
63
|
+
path: `${url.pathname}${url.search}`,
|
|
64
|
+
method: "POST",
|
|
65
|
+
headers,
|
|
66
|
+
...(allowInsecureLocalTls ? { agent: new HttpsAgent({ rejectUnauthorized: false }) } : {}),
|
|
67
|
+
}, (upstreamResponse) => {
|
|
68
|
+
const chunks = [];
|
|
69
|
+
upstreamResponse.on("data", (chunk) => {
|
|
70
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
71
|
+
});
|
|
72
|
+
upstreamResponse.on("error", reject);
|
|
73
|
+
upstreamResponse.on("end", () => {
|
|
74
|
+
const contentTypeHeader = upstreamResponse.headers["content-type"];
|
|
75
|
+
resolve({
|
|
76
|
+
status: upstreamResponse.statusCode ?? 502,
|
|
77
|
+
contentType: Array.isArray(contentTypeHeader) ? contentTypeHeader[0] : contentTypeHeader,
|
|
78
|
+
body: Buffer.concat(chunks),
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
upstream.on("error", reject);
|
|
83
|
+
upstream.end(body);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
export function isLocalUploadHost(hostname) {
|
|
87
|
+
const host = hostname.toLowerCase();
|
|
88
|
+
return host === "localhost" ||
|
|
89
|
+
host === "127.0.0.1" ||
|
|
90
|
+
host === "::1" ||
|
|
91
|
+
host === "[::1]" ||
|
|
92
|
+
host.endsWith(".local");
|
|
93
|
+
}
|
|
94
|
+
function redactUrlForClient(value) {
|
|
95
|
+
try {
|
|
96
|
+
const url = new URL(value);
|
|
97
|
+
url.username = "";
|
|
98
|
+
url.password = "";
|
|
99
|
+
return url.toString();
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=upload-proxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upload-proxy.js","sourceRoot":"","sources":["../src/upload-proxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,WAAW,CAAC;AAEnD,OAAO,EAAE,KAAK,IAAI,UAAU,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1E,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAWlE,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,KAO3C;IACC,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;QAClD,SAAS,CAAC,KAAK,CAAC,QAAQ,EAAE,GAAG,EAAE,EAAE,KAAK,EAAE,kDAAkD,EAAE,CAAC,CAAC;QAC9F,OAAO;IACT,CAAC;IACD,MAAM,SAAS,GAAG,gBAAgB,CAAC,KAAK,CAAC,cAAc,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;IACrE,MAAM,IAAI,GAAG,MAAM,eAAe,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC;IACtE,MAAM,OAAO,GAA2B;QACtC,gBAAgB,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;KACtC,CAAC;IACF,MAAM,WAAW,GAAG,SAAS,CAAC,KAAK,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;IAC7D,IAAI,WAAW,EAAE,CAAC;QAChB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CAAC;QACH,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;QAC3B,GAAG,CAAC,QAAQ,GAAG,EAAE,CAAC;QAClB,GAAG,CAAC,QAAQ,GAAG,EAAE,CAAC;QAClB,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC;IACxB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@amaster.ai/pi-attachments",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": {
|
|
15
|
+
"default": "./package.json"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/TGYD-helige/pi.git",
|
|
28
|
+
"directory": "packages/attachments"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@llamaindex/liteparse": "1.5.3",
|
|
32
|
+
"jszip": "3.10.1"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc -b",
|
|
36
|
+
"typecheck": "tsc -b --pretty false",
|
|
37
|
+
"test": "vitest run"
|
|
38
|
+
}
|
|
39
|
+
}
|