@ai-ecoverse/slicc-agent 7.25.1 → 7.27.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.
Files changed (38) hide show
  1. package/README.md +9 -6
  2. package/dist/approvals/index.js +5 -4
  3. package/dist/approvals/index.js.map +2 -2
  4. package/dist/approvals/people.js +5 -0
  5. package/dist/approvals/people.js.map +7 -0
  6. package/dist/approvals/record.js +5 -2
  7. package/dist/approvals/record.js.map +2 -2
  8. package/dist/approvals/sudo-client.js +11 -4
  9. package/dist/approvals/sudo-client.js.map +2 -2
  10. package/dist/approvals/sudo.js +13 -5
  11. package/dist/approvals/sudo.js.map +2 -2
  12. package/dist/attachments.js +92 -43
  13. package/dist/attachments.js.map +2 -2
  14. package/dist/index.js.map +2 -2
  15. package/dist/main.js +27 -7
  16. package/dist/main.js.map +2 -2
  17. package/dist/page.js.map +2 -2
  18. package/dist/spectrum/agent-port.js +36 -7
  19. package/dist/spectrum/agent-port.js.map +2 -2
  20. package/dist/spectrum/outgoing.js +85 -24
  21. package/dist/spectrum/outgoing.js.map +2 -2
  22. package/dist/staging.js +42 -0
  23. package/dist/staging.js.map +7 -0
  24. package/dist/types/approvals/people.d.ts +2 -0
  25. package/dist/types/approvals/record.d.ts +5 -3
  26. package/dist/types/approvals/sudo-client.d.ts +1 -1
  27. package/dist/types/approvals/sudo.d.ts +9 -1
  28. package/dist/types/attachments.d.ts +13 -3
  29. package/dist/types/index.d.ts +1 -1
  30. package/dist/types/main.d.ts +1 -0
  31. package/dist/types/page.d.ts +1 -1
  32. package/dist/types/spectrum/agent-port.d.ts +2 -0
  33. package/dist/types/spectrum/outgoing.d.ts +12 -3
  34. package/dist/types/staging.d.ts +13 -0
  35. package/dist/types/uploads.d.ts +2 -0
  36. package/dist/uploads.js +3 -1
  37. package/dist/uploads.js.map +2 -2
  38. package/package.json +4 -4
@@ -1,28 +1,78 @@
1
1
  import {
2
2
  FILE_LIMIT,
3
- limitOf,
3
+ fromBase64,
4
+ IMAGE_LIMIT,
5
+ INLINE_LIMIT,
6
+ INLINE_TOTAL,
4
7
  REF,
5
- TOGETHER,
8
+ stagedDir,
6
9
  TOTAL_LIMIT,
7
10
  textBytes,
11
+ together,
8
12
  tooLarge
9
13
  } from "../attachments.js";
10
14
  import { base64, shrink } from "../images/fit.js";
15
+ import { opfsDirectory } from "../opfs-pool.js";
11
16
  const readUrl = async (url) => {
12
17
  const response = await fetch(url);
13
18
  if (!response.ok) throw new Error(`status ${response.status}`);
14
- return new Uint8Array(await response.arrayBuffer());
19
+ return response.blob();
15
20
  };
21
+ function limitsOf(staging) {
22
+ const each = staging ? FILE_LIMIT : INLINE_LIMIT;
23
+ return { image: each, text: each, file: each, total: staging ? TOTAL_LIMIT : INLINE_TOTAL };
24
+ }
25
+ function stagingDir(serverId, root = () => navigator.storage.getDirectory()) {
26
+ return Promise.resolve().then(root).then(
27
+ (handle) => opfsDirectory(handle, stagedDir(serverId)),
28
+ () => void 0
29
+ );
30
+ }
31
+ function stager(dir) {
32
+ const names = [];
33
+ return {
34
+ async stage(body) {
35
+ const id = crypto.randomUUID();
36
+ names.push(id);
37
+ const writable = await (await dir.getFileHandle(id, { create: true })).createWritable();
38
+ await body.stream().pipeTo(writable);
39
+ return id;
40
+ },
41
+ async clear() {
42
+ await Promise.all(
43
+ names.splice(0).map((name) => dir.removeEntry(name).catch(() => void 0))
44
+ );
45
+ }
46
+ };
47
+ }
16
48
  const KINDS = /* @__PURE__ */ new Set(["image", "text", "file"]);
17
- async function bytesOf(attachment, read, fit) {
18
- const bytes = await read(attachment.url).catch((error) => {
49
+ async function carried(base, body, carry) {
50
+ carry.all += body.size;
51
+ if (carry.stage && carry.all > TOTAL_LIMIT) throw new Error(together(TOTAL_LIMIT));
52
+ if (body.size <= INLINE_LIMIT && carry.inline + body.size <= INLINE_TOTAL) {
53
+ carry.inline += body.size;
54
+ return { ...base, data: base64(new Uint8Array(await body.arrayBuffer())) };
55
+ }
56
+ if (!carry.stage)
57
+ throw new Error(
58
+ body.size > INLINE_LIMIT ? tooLarge(base.name, INLINE_LIMIT) : together(INLINE_TOTAL)
59
+ );
60
+ if (body.size > FILE_LIMIT) throw new Error(tooLarge(base.name, FILE_LIMIT));
61
+ return { ...base, upload: await carry.stage(body) };
62
+ }
63
+ async function fitted(attachment, read, fit) {
64
+ const blob = await read(attachment.url).catch((error) => {
19
65
  throw new Error(`Couldn't read ${attachment.name}: ${error.message}.`);
20
66
  });
21
- const kind = attachment.kind;
22
- const image = { bytes, mimeType: attachment.mimeType };
23
- const fitted = kind === "image" && bytes.length > limitOf(kind) ? await fit(image).catch(() => image) : image;
24
- if (fitted.bytes.length > limitOf(kind)) throw new Error(tooLarge(attachment.name, kind));
25
- return fitted;
67
+ const { mimeType } = attachment;
68
+ if (attachment.kind !== "image" || blob.size <= IMAGE_LIMIT) return { blob, mimeType };
69
+ if (blob.size > FILE_LIMIT) throw new Error(tooLarge(attachment.name, FILE_LIMIT));
70
+ const image = { bytes: new Uint8Array(await blob.arrayBuffer()), mimeType };
71
+ const smaller = await fit(image).catch(() => image);
72
+ return {
73
+ blob: new Blob([smaller.bytes], { type: smaller.mimeType }),
74
+ mimeType: smaller.mimeType
75
+ };
26
76
  }
27
77
  async function kept(attachment, stored) {
28
78
  const match = REF.exec(attachment.id);
@@ -33,7 +83,17 @@ async function kept(attachment, stored) {
33
83
  throw new Error(`Couldn't find ${attachment.name} again; attach it again.`);
34
84
  return found;
35
85
  }
36
- async function resolved(attachment, read, fit, stored) {
86
+ async function written(base, text, carry) {
87
+ const size = textBytes(text);
88
+ if (size <= INLINE_LIMIT && carry.inline + size <= INLINE_TOTAL) {
89
+ carry.all += size;
90
+ carry.inline += size;
91
+ return { ...base, text };
92
+ }
93
+ if (base.path !== void 0) return base;
94
+ return carried({ ...base, kind: "file" }, new Blob([text], { type: base.mimeType }), carry);
95
+ }
96
+ async function resolved(attachment, read, fit, stored, carry) {
37
97
  const kind = attachment.kind;
38
98
  const base = {
39
99
  name: attachment.name,
@@ -42,32 +102,33 @@ async function resolved(attachment, read, fit, stored) {
42
102
  ...attachment.path === void 0 ? {} : { path: attachment.path }
43
103
  };
44
104
  const found = await kept(attachment, stored);
45
- if (found?.data) return { ...base, mimeType: found.mimeType, data: found.data };
46
- if (kind === "text" && attachment.text !== void 0) {
47
- if (textBytes(attachment.text) > FILE_LIMIT) throw new Error(tooLarge(attachment.name, kind));
48
- return { ...base, text: attachment.text };
105
+ if (found?.data) {
106
+ const bytes = fromBase64(found.data);
107
+ return carried({ ...base, mimeType: found.mimeType }, new Blob([bytes]), carry);
49
108
  }
109
+ if (kind === "text" && attachment.text !== void 0)
110
+ return written(base, attachment.text, carry);
50
111
  if (attachment.url) {
51
- const { bytes, mimeType } = await bytesOf(attachment, read, fit);
52
- return { ...base, mimeType, data: base64(bytes) };
112
+ const { blob, mimeType } = await fitted(attachment, read, fit);
113
+ return carried({ ...base, mimeType }, blob, carry);
53
114
  }
54
115
  if (attachment.path !== void 0) return base;
55
116
  throw new Error(`Couldn't read ${attachment.name}; attach it again.`);
56
117
  }
57
- async function sendAttachments(attachments = [], read = readUrl, fit = shrink, stored = async () => ({ data: null, mimeType: null })) {
118
+ async function sendAttachments(attachments = [], read = readUrl, fit = shrink, stored = async () => ({ data: null, mimeType: null }), stage) {
58
119
  const out = [];
59
- let total = 0;
120
+ const carry = { stage, inline: 0, all: 0 };
60
121
  for (const attachment of attachments) {
61
122
  if (attachment.error || !KINDS.has(attachment.kind)) continue;
62
- const item = await resolved(attachment, read, fit, stored);
63
- total += textBytes(item.text ?? "") + (item.data?.length ?? 0) * 3 / 4;
64
- if (total > TOTAL_LIMIT) throw new Error(TOGETHER);
65
- out.push(item);
123
+ out.push(await resolved(attachment, read, fit, stored, carry));
66
124
  }
67
125
  return out;
68
126
  }
69
127
  export {
128
+ limitsOf,
70
129
  readUrl,
71
- sendAttachments
130
+ sendAttachments,
131
+ stager,
132
+ stagingDir
72
133
  };
73
134
  //# sourceMappingURL=outgoing.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/spectrum/outgoing.ts"],
4
- "sourcesContent": ["import type { Attachment } from '@ai-ecoverse/slicc-spectrum/ui';\nimport {\n type AttachmentKind,\n FILE_LIMIT,\n limitOf,\n REF,\n type SendAttachment,\n TOGETHER,\n TOTAL_LIMIT,\n textBytes,\n tooLarge,\n} from '../attachments.ts';\nimport { base64, type Shrink, shrink } from '../images/fit.ts';\nimport type { ImageRef, StoredImage } from '../services.ts';\n\nexport type Read = (url: string) => Promise<Uint8Array>;\n\nexport type Stored = (ref: ImageRef) => Promise<StoredImage>;\n\nexport const readUrl: Read = async (url) => {\n const response = await fetch(url);\n if (!response.ok) throw new Error(`status ${response.status}`);\n return new Uint8Array(await response.arrayBuffer());\n};\n\nconst KINDS = new Set<string>(['image', 'text', 'file']);\n\nasync function bytesOf(\n attachment: Attachment,\n read: Read,\n fit: Shrink\n): Promise<{ bytes: Uint8Array; mimeType: string }> {\n const bytes = await read(attachment.url as string).catch((error: Error) => {\n throw new Error(`Couldn't read ${attachment.name}: ${error.message}.`);\n });\n const kind = attachment.kind as AttachmentKind;\n const image = { bytes, mimeType: attachment.mimeType };\n const fitted =\n kind === 'image' && bytes.length > limitOf(kind) ? await fit(image).catch(() => image) : image;\n if (fitted.bytes.length > limitOf(kind)) throw new Error(tooLarge(attachment.name, kind));\n return fitted;\n}\n\nasync function kept(attachment: Attachment, stored: Stored): Promise<StoredImage | undefined> {\n const match = REF.exec(attachment.id);\n if (!match) return undefined;\n const [, entry, message, part] = match.map(Number) as number[];\n const found = await stored({ entry, message, part } as ImageRef);\n if (found.data === null)\n throw new Error(`Couldn't find ${attachment.name} again; attach it again.`);\n return found;\n}\n\nasync function resolved(\n attachment: Attachment,\n read: Read,\n fit: Shrink,\n stored: Stored\n): Promise<SendAttachment> {\n const kind = attachment.kind as AttachmentKind;\n const base = {\n name: attachment.name,\n kind,\n mimeType: attachment.mimeType,\n ...(attachment.path === undefined ? {} : { path: attachment.path }),\n };\n const found = await kept(attachment, stored);\n if (found?.data) return { ...base, mimeType: found.mimeType, data: found.data };\n if (kind === 'text' && attachment.text !== undefined) {\n if (textBytes(attachment.text) > FILE_LIMIT) throw new Error(tooLarge(attachment.name, kind));\n return { ...base, text: attachment.text };\n }\n if (attachment.url) {\n const { bytes, mimeType } = await bytesOf(attachment, read, fit);\n return { ...base, mimeType, data: base64(bytes) };\n }\n if (attachment.path !== undefined) return base;\n throw new Error(`Couldn't read ${attachment.name}; attach it again.`);\n}\n\nexport async function sendAttachments(\n attachments: readonly Attachment[] = [],\n read: Read = readUrl,\n fit: Shrink = shrink,\n stored: Stored = async () => ({ data: null, mimeType: null })\n): Promise<SendAttachment[]> {\n const out: SendAttachment[] = [];\n let total = 0;\n for (const attachment of attachments) {\n if (attachment.error || !KINDS.has(attachment.kind)) continue;\n const item = await resolved(attachment, read, fit, stored);\n total += textBytes(item.text ?? '') + ((item.data?.length ?? 0) * 3) / 4;\n if (total > TOTAL_LIMIT) throw new Error(TOGETHER);\n out.push(item);\n }\n return out;\n}\n"],
5
- "mappings": "AACA;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,QAAqB,cAAc;AAOrC,MAAM,UAAgB,OAAO,QAAQ;AAC1C,QAAM,WAAW,MAAM,MAAM,GAAG;AAChC,MAAI,CAAC,SAAS,GAAI,OAAM,IAAI,MAAM,UAAU,SAAS,MAAM,EAAE;AAC7D,SAAO,IAAI,WAAW,MAAM,SAAS,YAAY,CAAC;AACpD;AAEA,MAAM,QAAQ,oBAAI,IAAY,CAAC,SAAS,QAAQ,MAAM,CAAC;AAEvD,eAAe,QACb,YACA,MACA,KACkD;AAClD,QAAM,QAAQ,MAAM,KAAK,WAAW,GAAa,EAAE,MAAM,CAAC,UAAiB;AACzE,UAAM,IAAI,MAAM,iBAAiB,WAAW,IAAI,KAAK,MA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ClB,UAAM,EAAE,OAAO,SAAS,IAAI,MAAM,QAAQ,YAAY,MAAM,GAAG;AAC/D,WAAO,EAAE,GAAG,MAAM,UAAU,MAAM,OAAO,KAAK,EAAE;AAAA,EAClD;AACA,MAAI,WAAW,SAAS,OAAW,QAAO;AAC1C,QAAM,IAAI,MAAM,iBAAiB,WAAW,IAAI,oBAAoB;AACtE;AAEA,eAAsB,gBACpB,cAAqC,CAAC,GACtC,OAAa,SACb,MAAc,QACd,SAAiB,aAAa,EAAE,MAAM,MAAM,UAAU,KAAK,IAChC;AAC3B,QAAM,MAAwB,CAAC;AAC/B,MAAI,QAAQ;AACZ,aAAW,cAAc,aAAa;AACpC,QAAI,WAAW,SAAS,CAAC,MAAM,IAAI,WAAW,IAAI,EAAG;AACrD,UAAM,OAAO,MAAM,SAAS,YAAY,MAAM,KAAK,MAAM;AACzD,aAAS,UAAU,KAAK,QAAQ,EAAE,KAAM,KAAK,MAAM,UAAU,KAAK,IAAK;AACvE,QAAI,QAAQ,YAAa,OAAM,IAAI,MAAM,QAAQ;AACjD,QAAI,KAAK,IAAI;AAAA,EACf;AACA,SAAO;AACT;",
4
+ "sourcesContent": ["import type { Attachment } from '@ai-ecoverse/slicc-spectrum/ui';\nimport {\n type AttachmentKind,\n FILE_LIMIT,\n fromBase64,\n IMAGE_LIMIT,\n INLINE_LIMIT,\n INLINE_TOTAL,\n REF,\n type SendAttachment,\n stagedDir,\n TOTAL_LIMIT,\n textBytes,\n together,\n tooLarge,\n} from '../attachments.ts';\nimport { base64, type Shrink, shrink } from '../images/fit.ts';\nimport { opfsDirectory } from '../opfs-pool.ts';\nimport type { ImageRef, StoredImage } from '../services.ts';\n\nexport type Read = (url: string) => Promise<Blob>;\n\nexport type Stored = (ref: ImageRef) => Promise<StoredImage>;\n\nexport type Stage = (body: Blob) => Promise<string>;\n\nexport type AttachmentLimits = Partial<Record<AttachmentKind | 'secret' | 'total', number>>;\n\nexport interface Stager {\n stage: Stage;\n clear(): Promise<void>;\n}\n\nexport const readUrl: Read = async (url) => {\n const response = await fetch(url);\n if (!response.ok) throw new Error(`status ${response.status}`);\n return response.blob();\n};\n\nexport function limitsOf(staging: boolean): AttachmentLimits {\n const each = staging ? FILE_LIMIT : INLINE_LIMIT;\n return { image: each, text: each, file: each, total: staging ? TOTAL_LIMIT : INLINE_TOTAL };\n}\n\nexport function stagingDir(\n serverId: string,\n root: () => Promise<FileSystemDirectoryHandle> = () => navigator.storage.getDirectory()\n): Promise<FileSystemDirectoryHandle | undefined> {\n return Promise.resolve()\n .then(root)\n .then(\n (handle) => opfsDirectory(handle, stagedDir(serverId)),\n () => undefined\n );\n}\n\nexport function stager(dir: FileSystemDirectoryHandle): Stager {\n const names: string[] = [];\n return {\n async stage(body) {\n const id = crypto.randomUUID();\n names.push(id);\n const writable = await (await dir.getFileHandle(id, { create: true })).createWritable();\n await body.stream().pipeTo(writable);\n return id;\n },\n async clear() {\n await Promise.all(\n names.splice(0).map((name) => dir.removeEntry(name).catch(() => undefined))\n );\n },\n };\n}\n\nconst KINDS = new Set<string>(['image', 'text', 'file']);\n\ntype Base = Omit<SendAttachment, 'data' | 'text' | 'upload'>;\n\ninterface Carry {\n stage: Stage | undefined;\n inline: number;\n all: number;\n}\n\nasync function carried(base: Base, body: Blob, carry: Carry): Promise<SendAttachment> {\n carry.all += body.size;\n if (carry.stage && carry.all > TOTAL_LIMIT) throw new Error(together(TOTAL_LIMIT));\n if (body.size <= INLINE_LIMIT && carry.inline + body.size <= INLINE_TOTAL) {\n carry.inline += body.size;\n return { ...base, data: base64(new Uint8Array(await body.arrayBuffer())) };\n }\n if (!carry.stage)\n throw new Error(\n body.size > INLINE_LIMIT ? tooLarge(base.name, INLINE_LIMIT) : together(INLINE_TOTAL)\n );\n if (body.size > FILE_LIMIT) throw new Error(tooLarge(base.name, FILE_LIMIT));\n return { ...base, upload: await carry.stage(body) };\n}\n\nasync function fitted(\n attachment: Attachment,\n read: Read,\n fit: Shrink\n): Promise<{ blob: Blob; mimeType: string }> {\n const blob = await read(attachment.url as string).catch((error: Error) => {\n throw new Error(`Couldn't read ${attachment.name}: ${error.message}.`);\n });\n const { mimeType } = attachment;\n if (attachment.kind !== 'image' || blob.size <= IMAGE_LIMIT) return { blob, mimeType };\n if (blob.size > FILE_LIMIT) throw new Error(tooLarge(attachment.name, FILE_LIMIT));\n const image = { bytes: new Uint8Array(await blob.arrayBuffer()), mimeType };\n const smaller = await fit(image).catch(() => image);\n return {\n blob: new Blob([smaller.bytes as Uint8Array<ArrayBuffer>], { type: smaller.mimeType }),\n mimeType: smaller.mimeType,\n };\n}\n\nasync function kept(attachment: Attachment, stored: Stored): Promise<StoredImage | undefined> {\n const match = REF.exec(attachment.id);\n if (!match) return undefined;\n const [, entry, message, part] = match.map(Number) as number[];\n const found = await stored({ entry, message, part } as ImageRef);\n if (found.data === null)\n throw new Error(`Couldn't find ${attachment.name} again; attach it again.`);\n return found;\n}\n\nasync function written(base: Base, text: string, carry: Carry): Promise<SendAttachment> {\n const size = textBytes(text);\n if (size <= INLINE_LIMIT && carry.inline + size <= INLINE_TOTAL) {\n carry.all += size;\n carry.inline += size;\n return { ...base, text };\n }\n if (base.path !== undefined) return base;\n return carried({ ...base, kind: 'file' }, new Blob([text], { type: base.mimeType }), carry);\n}\n\nasync function resolved(\n attachment: Attachment,\n read: Read,\n fit: Shrink,\n stored: Stored,\n carry: Carry\n): Promise<SendAttachment> {\n const kind = attachment.kind as AttachmentKind;\n const base = {\n name: attachment.name,\n kind,\n mimeType: attachment.mimeType,\n ...(attachment.path === undefined ? {} : { path: attachment.path }),\n };\n const found = await kept(attachment, stored);\n if (found?.data) {\n const bytes = fromBase64(found.data) as Uint8Array<ArrayBuffer>;\n return carried({ ...base, mimeType: found.mimeType }, new Blob([bytes]), carry);\n }\n if (kind === 'text' && attachment.text !== undefined)\n return written(base, attachment.text, carry);\n if (attachment.url) {\n const { blob, mimeType } = await fitted(attachment, read, fit);\n return carried({ ...base, mimeType }, blob, carry);\n }\n if (attachment.path !== undefined) return base;\n throw new Error(`Couldn't read ${attachment.name}; attach it again.`);\n}\n\nexport async function sendAttachments(\n attachments: readonly Attachment[] = [],\n read: Read = readUrl,\n fit: Shrink = shrink,\n stored: Stored = async () => ({ data: null, mimeType: null }),\n stage?: Stage\n): Promise<SendAttachment[]> {\n const out: SendAttachment[] = [];\n const carry: Carry = { stage, inline: 0, all: 0 };\n for (const attachment of attachments) {\n if (attachment.error || !KINDS.has(attachment.kind)) continue;\n out.push(await resolved(attachment, read, fit, stored, carry));\n }\n return out;\n}\n"],
5
+ "mappings": "AACA;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,QAAqB,cAAc;AAC5C,SAAS,qBAAqB;AAgBvB,MAAM,UAAgB,OAAO,QAAQ;AAC1C,QAAM,WAAW,MAAM,MAAM,GAAG;AAChC,MAAI,CAAC,SAAS,GAAI,OAAM,IAAI,MAAM,UAAU,SAAS,MAAM,EAAE;AAC7D,SAAO,SAAS,KAAK;AACvB;AAEO,SAAS,SAAS,SAAoC;AAC3D,QAAM,OAAO,UAAU,aAAa;AACpC,SAAO,EAAE,OAAO,MAAM,MAAM,MAAM,MAAM,MAAM,OAAO,UAAU,cAAc,aAAa;AAC5F;AAEO,SAAS,WACd,UACA,OAAiD,MAAM,UAAU,QAAQ,aAAa,GACtC;AAChD,SAAO,QAAQ,QAAQ,EACpB,KAAK,IAAI,EACT;AAAA,IACC,CAAC,WAAW,cAAc,QAAQ,UAAU,QAAQ,CAAC;AAAA,IACrD,MAAM;AAAA,EACR;AACJ;AAEO,SAAS,OAAO,KAAwC;AAC7D,QAAM,QAAkB,CAAC;AACzB,SAAO;AAAA,IACL,MAAM,MAAM,MAAM;AAChB,YAAM,KAAK,OAAO,WAAW;AAC7B,YAAM,KAAK,EAAE;AACb,YAAM,WAAW,OAAO,MAAM,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ACA,UAAU,WAAW;AAAA,IACrB,GAAI,WAAW,SAAS,SAAY,CAAC,IAAI,EAAE,MAAM,WAAW,KAAK;AAAA,EACnE;AACA,QAAM,QAAQ,MAAM,KAAK,YAAY,MAAM;AAC3C,MAAI,OAAO,MAAM;AACf,UAAM,QAAQ,WAAW,MAAM,IAAI;AACnC,WAAO,QAAQ,EAAE,GAAG,MAAM,UAAU,MAAM,SAAS,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,GAAG,KAAK;AAAA,EAChF;AACA,MAAI,SAAS,UAAU,WAAW,SAAS;AACzC,WAAO,QAAQ,MAAM,WAAW,MAAM,KAAK;AAC7C,MAAI,WAAW,KAAK;AAClB,UAAM,EAAE,MAAM,SAAS,IAAI,MAAM,OAAO,YAAY,MAAM,GAAG;AAC7D,WAAO,QAAQ,EAAE,GAAG,MAAM,SAAS,GAAG,MAAM,KAAK;AAAA,EACnD;AACA,MAAI,WAAW,SAAS,OAAW,QAAO;AAC1C,QAAM,IAAI,MAAM,iBAAiB,WAAW,IAAI,oBAAoB;AACtE;AAEA,eAAsB,gBACpB,cAAqC,CAAC,GACtC,OAAa,SACb,MAAc,QACd,SAAiB,aAAa,EAAE,MAAM,MAAM,UAAU,KAAK,IAC3D,OAC2B;AAC3B,QAAM,MAAwB,CAAC;AAC/B,QAAM,QAAe,EAAE,OAAO,QAAQ,GAAG,KAAK,EAAE;AAChD,aAAW,cAAc,aAAa;AACpC,QAAI,WAAW,SAAS,CAAC,MAAM,IAAI,WAAW,IAAI,EAAG;AACrD,QAAI,KAAK,MAAM,SAAS,YAAY,MAAM,KAAK,QAAQ,KAAK,CAAC;AAAA,EAC/D;AACA,SAAO;AACT;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,42 @@
1
+ import { STAGED_ID, stagedDir } from "./attachments.js";
2
+ import { opfsDirectory } from "./opfs-pool.js";
3
+ const STAGED_MS = 60 * 60 * 1e3;
4
+ function workerStaging(serverId, options = {}) {
5
+ const root = options.root ?? (() => navigator.storage.getDirectory());
6
+ const maxAge = options.maxAgeMs ?? STAGED_MS;
7
+ const now = options.now ?? Date.now;
8
+ const dir = () => Promise.resolve().then(root).then(
9
+ (handle) => opfsDirectory(handle, stagedDir(serverId)),
10
+ () => void 0
11
+ );
12
+ const drop = async (id) => {
13
+ if (STAGED_ID.test(id)) await (await dir())?.removeEntry(id).catch(() => void 0);
14
+ };
15
+ const sweep = async () => {
16
+ const found = await dir();
17
+ if (!found) return;
18
+ const stale = [];
19
+ for await (const handle of found.values()) {
20
+ if (handle.kind !== "file") continue;
21
+ const file = await handle.getFile().catch(() => void 0);
22
+ if (file && file.lastModified <= now() - maxAge) stale.push(handle.name);
23
+ }
24
+ await Promise.all(stale.map((name) => found.removeEntry(name).catch(() => void 0)));
25
+ };
26
+ const timer = setInterval(() => void sweep(), maxAge);
27
+ return {
28
+ async take(id) {
29
+ if (!STAGED_ID.test(id)) return void 0;
30
+ const handle = await (await dir())?.getFileHandle(id).catch(() => void 0);
31
+ return handle?.getFile().catch(() => void 0);
32
+ },
33
+ drop,
34
+ sweep,
35
+ close: () => clearInterval(timer)
36
+ };
37
+ }
38
+ export {
39
+ STAGED_MS,
40
+ workerStaging
41
+ };
42
+ //# sourceMappingURL=staging.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/staging.ts"],
4
+ "sourcesContent": ["import { STAGED_ID, stagedDir } from './attachments.ts';\nimport { opfsDirectory } from './opfs-pool.ts';\n\nexport const STAGED_MS = 60 * 60 * 1000;\n\nexport interface Staging {\n take(id: string): Promise<Blob | undefined>;\n drop(id: string): Promise<void>;\n sweep(): Promise<void>;\n close(): void;\n}\n\nexport interface StagingOptions {\n root?: () => Promise<FileSystemDirectoryHandle>;\n maxAgeMs?: number;\n now?: () => number;\n}\n\nexport function workerStaging(serverId: string, options: StagingOptions = {}): Staging {\n const root = options.root ?? (() => navigator.storage.getDirectory());\n const maxAge = options.maxAgeMs ?? STAGED_MS;\n const now = options.now ?? Date.now;\n const dir = () =>\n Promise.resolve()\n .then(root)\n .then(\n (handle) => opfsDirectory(handle, stagedDir(serverId)),\n () => undefined\n );\n const drop = async (id: string) => {\n if (STAGED_ID.test(id)) await (await dir())?.removeEntry(id).catch(() => undefined);\n };\n const sweep = async () => {\n const found = await dir();\n if (!found) return;\n const stale: string[] = [];\n for await (const handle of found.values()) {\n if (handle.kind !== 'file') continue;\n const file = await (handle as FileSystemFileHandle).getFile().catch(() => undefined);\n if (file && file.lastModified <= now() - maxAge) stale.push(handle.name);\n }\n await Promise.all(stale.map((name) => found.removeEntry(name).catch(() => undefined)));\n };\n const timer = setInterval(() => void sweep(), maxAge);\n return {\n async take(id) {\n if (!STAGED_ID.test(id)) return undefined;\n const handle = await (await dir())?.getFileHandle(id).catch(() => undefined);\n return handle?.getFile().catch(() => undefined);\n },\n drop,\n sweep,\n close: () => clearInterval(timer),\n };\n}\n"],
5
+ "mappings": "AAAA,SAAS,WAAW,iBAAiB;AACrC,SAAS,qBAAqB;AAEvB,MAAM,YAAY,KAAK,KAAK;AAe5B,SAAS,cAAc,UAAkB,UAA0B,CAAC,GAAY;AACrF,QAAM,OAAO,QAAQ,SAAS,MAAM,UAAU,QAAQ,aAAa;AACnE,QAAM,SAAS,QAAQ,YAAY;AACnC,QAAM,MAAM,QAAQ,OAAO,KAAK;AAChC,QAAM,MAAM,MACV,QAAQ,QAAQ,EACb,KAAK,IAAI,EACT;AAAA,IACC,CAAC,WAAW,cAAc,QAAQ,UAAU,QAAQ,CAAC;AAAA,IACrD,MAAM;AAAA,EACR;AACJ,QAAM,OAAO,OAAO,OAAe;AACjC,QAAI,UAAU,KAAK,EAAE,EAAG,QAAO,MAAM,IAAI,IAAI,YAAY,EAAE,EAAE,MAAM,MAAM,MAAS;AAAA,EACpF;AACA,QAAM,QAAQ,YAAY;AACxB,UAAM,QAAQ,MAAM,IAAI;AACxB,QAAI,CAAC,MAAO;AACZ,UAAM,QAAkB,CAAC;AACzB,qBAAiB,UAAU,MAAM,OAAO,GAAG;AACzC,UAAI,OAAO,SAAS,OAAQ;AAC5B,YAAM,OAAO,MAAO,OAAgC,QAAQ,EAAE,MAAM,MAAM,MAAS;AACnF,UAAI,QAAQ,KAAK,gBAAgB,IAAI,IAAI,OAAQ,OAAM,KAAK,OAAO,IAAI;AAAA,IACzE;AACA,UAAM,QAAQ,IAAI,MAAM,IAAI,CAAC,SAAS,MAAM,YAAY,IAAI,EAAE,MAAM,MAAM,MAAS,CAAC,CAAC;AAAA,EACvF;AACA,QAAM,QAAQ,YAAY,MAAM,KAAK,MAAM,GAAG,MAAM;AACpD,SAAO;AAAA,IACL,MAAM,KAAK,IAAI;AACb,UAAI,CAAC,UAAU,KAAK,EAAE,EAAG,QAAO;AAChC,YAAM,SAAS,OAAO,MAAM,IAAI,IAAI,cAAc,EAAE,EAAE,MAAM,MAAM,MAAS;AAC3E,aAAO,QAAQ,QAAQ,EAAE,MAAM,MAAM,MAAS;AAAA,IAChD;AAAA,IACA;AAAA,IACA;AAAA,IACA,OAAO,MAAM,cAAc,KAAK;AAAA,EAClC;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,2 @@
1
+ import type { DecidedBy } from './record.ts';
2
+ export declare const personal: (by: DecidedBy) => boolean;
@@ -1,5 +1,6 @@
1
1
  import { fits, JUDGE_ALLOW } from './aligned.ts';
2
- export { fits, JUDGE_ALLOW };
2
+ import { personal } from './people.ts';
3
+ export { fits, JUDGE_ALLOW, personal };
3
4
  export type ApprovalKind = 'guest-message' | 'guest-tool' | 'sudo' | 'capture';
4
5
  export type ApprovalState = 'pending' | 'approved' | 'rejected' | 'unanswered' | 'withdrawn';
5
6
  export type ApprovalTier = 'user' | 'agent' | 'cone' | `scoop:${string}`;
@@ -51,6 +52,7 @@ export type SudoDetail = {
51
52
  stdin?: SudoStdin;
52
53
  rootOnly?: Record<string, boolean>;
53
54
  writable?: boolean;
55
+ validate?: true;
54
56
  };
55
57
  export type CaptureWhat = 'screen' | 'camera' | 'microphone';
56
58
  export type CaptureMode = 'still' | 'video' | 'audio';
@@ -135,11 +137,11 @@ export declare function knownTier(tier: unknown): tier is ApprovalTier;
135
137
  export declare const heldKind: (kind: ApprovalKind) => boolean;
136
138
  export declare function routable(kind: ApprovalKind, tier: ApprovalTier): boolean;
137
139
  export declare function answerOf(value: unknown): ApprovalAnswer | null;
138
- export declare const personal: (by: DecidedBy) => boolean;
139
140
  export declare function answerer(by: DecidedBy, item: Pick<Approval, 'tier' | 'reviewer'>): boolean;
140
141
  export type SudoApprover = 'user' | 'agent';
141
142
  export declare const sudoApprover: (value: unknown) => SudoApprover;
142
- export declare const tierOf: (kind: ApprovalKind, tier: string, sudo: SudoApprover) => string;
143
+ export declare const personOnly: (item: Pick<ApprovalDraft, 'kind' | 'detail'>) => boolean;
144
+ export declare const tierOf: (draft: Pick<ApprovalDraft, 'kind' | 'detail' | 'tier'>, sudo: SudoApprover) => string;
143
145
  export type Gate = 'person' | 'judge';
144
146
  export type Routed = 'approve' | 'deny' | 'ask';
145
147
  export declare const gateOf: (tier: string) => Gate;
@@ -10,7 +10,7 @@ export declare const UNAVAILABLE = "The agent can't take this sudo request.";
10
10
  export declare const unavailable: (cause?: unknown) => Error;
11
11
  export declare function answerOf(item: Approval | undefined): SudoAnswer | null;
12
12
  export declare const JUDGE_DENIED = "sudo: denied by the approval judge";
13
- export declare function replyOf(item: Approval | undefined): SudoReply | null;
13
+ export declare function replyOf(item: Approval | undefined, stamps?: boolean): SudoReply | null;
14
14
  export declare const WITHDRAW_MS = 5000;
15
15
  export interface CaptureAsk {
16
16
  kind: string;
@@ -9,7 +9,11 @@ export type SudoDenial = {
9
9
  answer: 'deny';
10
10
  message: string;
11
11
  };
12
- export type SudoReply = SudoAnswer | SudoDenial;
12
+ export type SudoTimestamp = {
13
+ answer: 'once' | 'always';
14
+ timestamp: true;
15
+ };
16
+ export type SudoReply = SudoAnswer | SudoDenial | SudoTimestamp;
13
17
  export interface SudoRequest {
14
18
  id: string;
15
19
  kind: 'sudo';
@@ -28,6 +32,8 @@ export interface SudoRequest {
28
32
  always?: boolean;
29
33
  stdin?: SudoStdin;
30
34
  rootOnly?: Record<string, boolean>;
35
+ timestamp?: boolean;
36
+ validate?: boolean;
31
37
  }
32
38
  export type SudoAsked = {
33
39
  id: string;
@@ -47,6 +53,8 @@ export declare function sudoRequester(request: SudoRequest, agents: Readonly<Age
47
53
  requester: ApprovalRequester;
48
54
  agentId: string;
49
55
  };
56
+ export declare const validating: (request: Pick<SudoRequest, 'validate'>) => boolean;
57
+ export declare const timestampFor: (user: string) => string;
50
58
  export declare function sudoDetail(request: SudoRequest): SudoDetail;
51
59
  export interface SudoAttach {
52
60
  approvals: Approvals;
@@ -6,6 +6,7 @@ export interface SendAttachment {
6
6
  data?: string;
7
7
  text?: string;
8
8
  path?: string;
9
+ upload?: string;
9
10
  }
10
11
  export interface ShownAttachment {
11
12
  id: string;
@@ -28,16 +29,25 @@ export type Part = {
28
29
  export interface Saver {
29
30
  sees: boolean;
30
31
  save?(name: string, bytes: Uint8Array): Promise<string>;
32
+ staging?: {
33
+ take(id: string): Promise<Blob | undefined>;
34
+ drop(id: string): Promise<void>;
35
+ };
31
36
  }
32
37
  export declare const IMAGE_LIMIT: number;
38
+ export declare const INLINE_LIMIT: number;
39
+ export declare const INLINE_TOTAL: number;
33
40
  export declare const FILE_LIMIT: number;
34
41
  export declare const TOTAL_LIMIT: number;
42
+ export declare const STAGED_DIR = "/.slicc/attachments";
43
+ export declare const STAGED_ID: RegExp;
44
+ export declare function stagedDir(serverId: string): string;
35
45
  export declare const SEEN: Set<string>;
36
46
  export declare const BLIND = "This model can't see images, so the image was saved as a file instead.";
37
- export declare const TOGETHER = "These attachments add up to more than 10 MiB; send them in separate messages.";
47
+ export declare const UNSHOWN = "The image was too large to show the model, so it was saved as a file instead.";
38
48
  export declare function mib(bytes: number): string;
39
- export declare function limitOf(kind: AttachmentKind): number;
40
- export declare function tooLarge(name: string, kind: AttachmentKind): string;
49
+ export declare function together(limit: number): string;
50
+ export declare function tooLarge(name: string, limit: number): string;
41
51
  export declare function textBytes(text: string): number;
42
52
  export declare function decodedSize(data: string): number;
43
53
  export declare function fromBase64(data: string): Uint8Array;
@@ -2,7 +2,7 @@ export { ADOBE, ADOBE_PROXY, type AdobeConfig, type AdobeModelInfo, adobeClassif
2
2
  export { type Agent, type AgentOptions, answerText, openAgent } from './agent.ts';
3
3
  export { APPROVAL_MS, type Approval, type ApprovalAnswer, type ApprovalDecided, type ApprovalDraft, type ApprovalJudge, type ApprovalKind, type ApprovalRequester, type ApprovalState, type Approvals, ApprovalsDoc, type ApprovalTier, type ApprovalVerdict, ASK_MIN_MS, approvalJudge, attachApprovals, type CaptureApproval, type CaptureDetail, DEFAULT_POLICY, type DecidedBy, type Decider, JUDGE_ALLOW, JUDGE_KIND, JUDGE_MODEL, JUDGE_QUESTIONS, type Judgement, judgeModelOf, judgeSpender, judgeState, type OwnerIntent, ownerIntent, POLICY_FILE, REASONS, route, seedPolicy, VERDICT_REASONS, verdictOf, verdictRecorder, } from './approvals/index.ts';
4
4
  export { type ApprovalPolicy, type ApprovalPolicyChange, type ApprovalPolicySet, DEFAULT_APPROVAL_POLICY, type SudoPolicy, } from './approvals/policy.ts';
5
- export type { AgentSudo, SudoAnswer, SudoDenial, SudoReply, SudoRequest, } from './approvals/sudo.ts';
5
+ export type { AgentSudo, SudoAnswer, SudoDenial, SudoReply, SudoRequest, SudoTimestamp, } from './approvals/sudo.ts';
6
6
  export type { CaptureAsk } from './approvals/sudo-client.ts';
7
7
  export { FILE_LIMIT, IMAGE_LIMIT, type SendAttachment, type ShownAttachment, splitUser, TOTAL_LIMIT, userParts, } from './attachments.ts';
8
8
  export { type AgentConnection, connectAgent } from './client.ts';
@@ -35,6 +35,7 @@ export interface AgentWorkerOptions {
35
35
  users?: boolean;
36
36
  mcpRedirect?: string;
37
37
  approvalMs?: number;
38
+ stagedMs?: number;
38
39
  }
39
40
  export declare function usersOf(admin: MessagePort | undefined, wanted: boolean | undefined, attach: KernelAttach): UserClients | undefined;
40
41
  export declare const STORE_DIR = "/.slicc";
@@ -1,4 +1,4 @@
1
- export type { SudoAnswer, SudoDenial, SudoReply, SudoRequest } from './approvals/sudo.ts';
1
+ export type { SudoAnswer, SudoDenial, SudoReply, SudoRequest, SudoTimestamp, } from './approvals/sudo.ts';
2
2
  export { type ApproveOptions, JUDGE_DENIED, UNAVAILABLE } from './approvals/sudo-client.ts';
3
3
  export { type AgentConnection, connectAgent } from './client.ts';
4
4
  export { connectGuest, GUEST_ONLY, type GuestConnection, SEAT_REFUSED, } from './guests/client.ts';
@@ -4,6 +4,7 @@ import { type VaultHold } from '../secrets/view.ts';
4
4
  import type { Command } from '../services.ts';
5
5
  import { Emitter } from './emitter.ts';
6
6
  import { type ShownImport } from './import-port.ts';
7
+ import { type AttachmentLimits } from './outgoing.ts';
7
8
  export declare const CONE = "cone";
8
9
  export declare const OLDER_PAGE = 100;
9
10
  export declare const OLDER_PAGES = 40;
@@ -22,6 +23,7 @@ export declare class AgentAdapter extends Emitter<AgentAdapterEvents> implements
22
23
  active(): string;
23
24
  select(id: string): void;
24
25
  messages(agentId?: string): readonly Message[];
26
+ attachmentLimits(): AttachmentLimits;
25
27
  send(agentId: string, input: string | Outgoing): Promise<void>;
26
28
  rewind(agentId: string, messageId: string): Promise<Outgoing | null>;
27
29
  stop(agentId?: string): void;
@@ -1,8 +1,17 @@
1
1
  import type { Attachment } from '@ai-ecoverse/slicc-spectrum/ui';
2
- import { type SendAttachment } from '../attachments.ts';
2
+ import { type AttachmentKind, type SendAttachment } from '../attachments.ts';
3
3
  import { type Shrink } from '../images/fit.ts';
4
4
  import type { ImageRef, StoredImage } from '../services.ts';
5
- export type Read = (url: string) => Promise<Uint8Array>;
5
+ export type Read = (url: string) => Promise<Blob>;
6
6
  export type Stored = (ref: ImageRef) => Promise<StoredImage>;
7
+ export type Stage = (body: Blob) => Promise<string>;
8
+ export type AttachmentLimits = Partial<Record<AttachmentKind | 'secret' | 'total', number>>;
9
+ export interface Stager {
10
+ stage: Stage;
11
+ clear(): Promise<void>;
12
+ }
7
13
  export declare const readUrl: Read;
8
- export declare function sendAttachments(attachments?: readonly Attachment[], read?: Read, fit?: Shrink, stored?: Stored): Promise<SendAttachment[]>;
14
+ export declare function limitsOf(staging: boolean): AttachmentLimits;
15
+ export declare function stagingDir(serverId: string, root?: () => Promise<FileSystemDirectoryHandle>): Promise<FileSystemDirectoryHandle | undefined>;
16
+ export declare function stager(dir: FileSystemDirectoryHandle): Stager;
17
+ export declare function sendAttachments(attachments?: readonly Attachment[], read?: Read, fit?: Shrink, stored?: Stored, stage?: Stage): Promise<SendAttachment[]>;
@@ -0,0 +1,13 @@
1
+ export declare const STAGED_MS: number;
2
+ export interface Staging {
3
+ take(id: string): Promise<Blob | undefined>;
4
+ drop(id: string): Promise<void>;
5
+ sweep(): Promise<void>;
6
+ close(): void;
7
+ }
8
+ export interface StagingOptions {
9
+ root?: () => Promise<FileSystemDirectoryHandle>;
10
+ maxAgeMs?: number;
11
+ now?: () => number;
12
+ }
13
+ export declare function workerStaging(serverId: string, options?: StagingOptions): Staging;
@@ -2,12 +2,14 @@ import type { Context } from '@earendil-works/chord';
2
2
  import type { Conversation, ModelRef } from '@earendil-works/pi-durable';
3
3
  import type { ExecutionEnv } from '@earendil-works/pi-durable/env';
4
4
  import type { Saver } from './attachments.ts';
5
+ import type { Staging } from './staging.ts';
5
6
  export interface Uploads {
6
7
  env(target: {
7
8
  conversationId: number;
8
9
  cwd?: string;
9
10
  }, context: Context): ExecutionEnv | undefined | Promise<ExecutionEnv | undefined>;
10
11
  sees(model: ModelRef | undefined): boolean;
12
+ staging?: Staging;
11
13
  }
12
14
  export declare const UPLOADS = "uploads";
13
15
  export declare function upload(env: ExecutionEnv, name: string, bytes: Uint8Array, context: Context): Promise<string>;
package/dist/uploads.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const UPLOADS = "uploads";
2
2
  function cleaned(name) {
3
- const clean = name.replace(/[/\\\0]/g, "_").replace(/^\.+/, "") || "attachment";
3
+ const clean = name.replace(/[/\\<>"\p{Cc}]/gu, "_").replace(/^\.+/, "") || "attachment";
4
4
  const dot = clean.lastIndexOf(".");
5
5
  return dot > 0 ? { stem: clean.slice(0, dot), ext: clean.slice(dot) } : { stem: clean, ext: "" };
6
6
  }
@@ -21,9 +21,11 @@ async function upload(env, name, bytes, context) {
21
21
  }
22
22
  async function saverFor(conversation, uploads, context) {
23
23
  if (!uploads) return { sees: true };
24
+ await uploads.staging?.sweep();
24
25
  const agent = await conversation.agent(context);
25
26
  return {
26
27
  sees: uploads.sees(agent.model),
28
+ ...uploads.staging ? { staging: uploads.staging } : {},
27
29
  async save(name, bytes) {
28
30
  const target = {
29
31
  conversationId: conversation.id,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/uploads.ts"],
4
- "sourcesContent": ["import type { Context } from '@earendil-works/chord';\nimport type { Conversation, ModelRef } from '@earendil-works/pi-durable';\nimport type { ExecutionEnv } from '@earendil-works/pi-durable/env';\nimport type { Saver } from './attachments.ts';\n\nexport interface Uploads {\n env(\n target: { conversationId: number; cwd?: string },\n context: Context\n ): ExecutionEnv | undefined | Promise<ExecutionEnv | undefined>;\n sees(model: ModelRef | undefined): boolean;\n}\n\nexport const UPLOADS = 'uploads';\n\nfunction cleaned(name: string): { stem: string; ext: string } {\n const clean = name.replace(/[/\\\\\\0]/g, '_').replace(/^\\.+/, '') || 'attachment';\n const dot = clean.lastIndexOf('.');\n return dot > 0 ? { stem: clean.slice(0, dot), ext: clean.slice(dot) } : { stem: clean, ext: '' };\n}\n\nfunction must(result: { ok: boolean; error?: { message: string } }, name: string): void {\n if (!result.ok) throw new Error(`Couldn't save ${name}: ${result.error?.message}`);\n}\n\nexport async function upload(\n env: ExecutionEnv,\n name: string,\n bytes: Uint8Array,\n context: Context\n): Promise<string> {\n const dir = `${env.cwd.replace(/\\/+$/, '')}/${UPLOADS}`;\n must(await env.createDir(dir, { recursive: true }, context), name);\n const { stem, ext } = cleaned(name);\n for (let n = 0; n < 100; n++) {\n const path = `${dir}/${n ? `${stem}-${n}${ext}` : `${stem}${ext}`}`;\n if ((await env.fileInfo(path, context)).ok) continue;\n must(await env.writeFile(path, bytes, context), name);\n return path;\n }\n throw new Error(`Couldn't find a free name for ${name} in ${dir}.`);\n}\n\nexport async function saverFor(\n conversation: Conversation,\n uploads: Uploads | undefined,\n context: Context\n): Promise<Saver> {\n if (!uploads) return { sees: true };\n const agent = await conversation.agent(context);\n return {\n sees: uploads.sees(agent.model),\n async save(name, bytes) {\n const target = {\n conversationId: conversation.id,\n ...(agent.cwd ? { cwd: agent.cwd } : {}),\n };\n const env = await uploads.env(target, context);\n if (!env) throw new Error(`This agent has no folder to save ${name} in.`);\n return upload(env, name, bytes, context);\n },\n };\n}\n"],
5
- "mappings": "AAaO,MAAM,UAAU;AAEvB,SAAS,QAAQ,MAA6C;AAC5D,QAAM,QAAQ,KAAK,QAAQ,YAAY,GAAG,EAAE,QAAQ,QAAQ,EAAE,KAAK;AACnE,QAAM,MAAM,MAAM,YAAY,GAAG;AACjC,SAAO,MAAM,IAAI,EAAE,MAAM,MAAM,MAAM,GAAG,GAAG,GAAG,KAAK,MAAM,MAAM,GAAG,EAAE,IAAI,EAAE,MAAM,OAAO,KAAK,GAAG;AACjG;AAEA,SAAS,KAAK,QAAsD,MAAoB;AACtF,MAAI,CAAC,OAAO,GAAI,OAAM,IAAI,MAAM,iBAAiB,IAAI,KAAK,OAAO,OAAO,OAAO,EAAE;AACnF;AAEA,eAAsB,OACpB,KACA,MACA,OACA,SACiB;AACjB,QAAM,MAAM,GAAG,IAAI,IAAI,QAAQ,QAAQ,EAAE,CAAC,IAAI,OAAO;AACrD,OAAK,MAAM,IAAI,UAAU,KAAK,EAAE,WAAW,KAAK,GAAG,OAAO,GAAG,IAAI;AACjE,QAAM,EAAE,MAAM,IAAI,IAAI,QAAQ,IAAI;AAClC,WAAS,IAAI,GAAG,IAAI,KAAK,KAAK;AAC5B,UAAM,OAAO,GAAG,GAAG,IAAI,IAAI,GAAG,IAAI,IAAI,CAAC,GAAG,GAAG,KAAK,GAAG,IAAI,GAAG,GAAG,EAAE;AACjE,SAAK,MAAM,IAAI,SAAS,MAAM,OAAO,GAAG,GAAI;AAC5C,SAAK,MAAM,IAAI,UAAU,MAAM,OAAO,OAAO,GAAG,IAAI;AACpD,WAAO;AAAA,EACT;AACA,QAAM,IAAI,MAAM,iCAAiC,IAAI,OAAO,GAAG,GAAG;AACpE;AAEA,eAAsB,SACpB,cACA,SACA,SACgB;AAChB,MAAI,CAAC,QAAS,QAAO,EAAE,MAAM,KAAK;AAClC,QAAM,QAAQ,MAAM,aAAa,MAAM,OAAO;AAC9C,SAAO;AAAA,IACL,MAAM,QAAQ,KAAK,MAAM,KAAK;AAAA,IAC9B,MAAM,KAAK,MAAM,OAAO;AACtB,YAAM,SAAS;AAAA,QACb,gBAAgB,aAAa;AAAA,QAC7B,GAAI,MAAM,MAAM,EAAE,KAAK,MAAM,IAAI,IAAI,CAAC;AAAA,MACxC;AACA,YAAM,MAAM,MAAM,QAAQ,IAAI,QAAQ,OAAO;AAC7C,UAAI,CAAC,IAAK,OAAM,IAAI,MAAM,oCAAoC,IAAI,MAAM;AACxE,aAAO,OAAO,KAAK,MAAM,OAAO,OAAO;AAAA,IACzC;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import type { Context } from '@earendil-works/chord';\nimport type { Conversation, ModelRef } from '@earendil-works/pi-durable';\nimport type { ExecutionEnv } from '@earendil-works/pi-durable/env';\nimport type { Saver } from './attachments.ts';\nimport type { Staging } from './staging.ts';\n\nexport interface Uploads {\n env(\n target: { conversationId: number; cwd?: string },\n context: Context\n ): ExecutionEnv | undefined | Promise<ExecutionEnv | undefined>;\n sees(model: ModelRef | undefined): boolean;\n staging?: Staging;\n}\n\nexport const UPLOADS = 'uploads';\n\nfunction cleaned(name: string): { stem: string; ext: string } {\n const clean = name.replace(/[/\\\\<>\"\\p{Cc}]/gu, '_').replace(/^\\.+/, '') || 'attachment';\n const dot = clean.lastIndexOf('.');\n return dot > 0 ? { stem: clean.slice(0, dot), ext: clean.slice(dot) } : { stem: clean, ext: '' };\n}\n\nfunction must(result: { ok: boolean; error?: { message: string } }, name: string): void {\n if (!result.ok) throw new Error(`Couldn't save ${name}: ${result.error?.message}`);\n}\n\nexport async function upload(\n env: ExecutionEnv,\n name: string,\n bytes: Uint8Array,\n context: Context\n): Promise<string> {\n const dir = `${env.cwd.replace(/\\/+$/, '')}/${UPLOADS}`;\n must(await env.createDir(dir, { recursive: true }, context), name);\n const { stem, ext } = cleaned(name);\n for (let n = 0; n < 100; n++) {\n const path = `${dir}/${n ? `${stem}-${n}${ext}` : `${stem}${ext}`}`;\n if ((await env.fileInfo(path, context)).ok) continue;\n must(await env.writeFile(path, bytes, context), name);\n return path;\n }\n throw new Error(`Couldn't find a free name for ${name} in ${dir}.`);\n}\n\nexport async function saverFor(\n conversation: Conversation,\n uploads: Uploads | undefined,\n context: Context\n): Promise<Saver> {\n if (!uploads) return { sees: true };\n await uploads.staging?.sweep();\n const agent = await conversation.agent(context);\n return {\n sees: uploads.sees(agent.model),\n ...(uploads.staging ? { staging: uploads.staging } : {}),\n async save(name, bytes) {\n const target = {\n conversationId: conversation.id,\n ...(agent.cwd ? { cwd: agent.cwd } : {}),\n };\n const env = await uploads.env(target, context);\n if (!env) throw new Error(`This agent has no folder to save ${name} in.`);\n return upload(env, name, bytes, context);\n },\n };\n}\n"],
5
+ "mappings": "AAeO,MAAM,UAAU;AAEvB,SAAS,QAAQ,MAA6C;AAC5D,QAAM,QAAQ,KAAK,QAAQ,oBAAoB,GAAG,EAAE,QAAQ,QAAQ,EAAE,KAAK;AAC3E,QAAM,MAAM,MAAM,YAAY,GAAG;AACjC,SAAO,MAAM,IAAI,EAAE,MAAM,MAAM,MAAM,GAAG,GAAG,GAAG,KAAK,MAAM,MAAM,GAAG,EAAE,IAAI,EAAE,MAAM,OAAO,KAAK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AAO;AAC7C,UAAI,CAAC,IAAK,OAAM,IAAI,MAAM,oCAAoC,IAAI,MAAM;AACxE,aAAO,OAAO,KAAK,MAAM,OAAO,OAAO;AAAA,IACzC;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-ecoverse/slicc-agent",
3
- "version": "7.25.1",
3
+ "version": "7.27.0",
4
4
  "description": "SLICC's agent core on pi durable: an agent worker, session storage, remoting and pi extensions",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -73,11 +73,11 @@
73
73
  },
74
74
  "devDependencies": {
75
75
  "@ai-ecoverse/slicc-jsh": "0.64.0",
76
- "@ai-ecoverse/slicc-kernel": "1.176.1",
76
+ "@ai-ecoverse/slicc-kernel": "1.176.2",
77
77
  "@ai-ecoverse/slicc-scrub": "1.1.0",
78
78
  "@ai-ecoverse/slicc-shared-web": "2.13.10",
79
- "@ai-ecoverse/slicc-spectrum": "2.7.0",
80
- "@ai-ecoverse/wasi-fnox": "1.45.0-2",
79
+ "@ai-ecoverse/slicc-spectrum": "2.9.0",
80
+ "@ai-ecoverse/wasi-fnox": "1.46.0-1",
81
81
  "@ai-ecoverse/wasm-bash": "5.3.0-23",
82
82
  "@ai-ecoverse/wasm-coreutils": "9.12.0-15",
83
83
  "@ai-ecoverse/wasm-git": "2.55.0-22",