@alwith/module-chat 0.1.16 → 0.1.17

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.
@@ -29,6 +29,8 @@ export type TextInputContext = {
29
29
  value: string;
30
30
  setInput: (v: string) => void;
31
31
  clear: () => void;
32
+ getRevision?: () => number;
33
+ clearSubmitted?: () => void;
32
34
  };
33
35
  export type PromptInputControllerProps = {
34
36
  textInput: TextInputContext;
@@ -37,12 +39,22 @@ export type PromptInputControllerProps = {
37
39
  };
38
40
  export declare const PromptInputSubmitContext: import("react").Context<((text: string) => void) | null>;
39
41
  export declare function usePromptInputSubmit(): ((text: string) => void) | null;
42
+ export type PromptSubmitResult = {
43
+ status: "accepted";
44
+ } | {
45
+ status: "blocked";
46
+ reason: string;
47
+ };
48
+ export declare const PromptInputSubmissionContext: import("react").Context<(() => Promise<PromptSubmitResult>) | null>;
49
+ export declare function usePromptInputSubmission(): (() => Promise<PromptSubmitResult>) | null;
40
50
  export declare const ProviderAttachmentsContext: import("react").Context<AttachmentsContext | null>;
41
51
  export declare function usePromptInputController(): PromptInputControllerProps;
42
52
  export declare const useOptionalPromptInputController: () => PromptInputControllerProps | null;
43
53
  export declare function useProviderAttachments(): AttachmentsContext;
44
54
  export type PromptInputProviderProps = PropsWithChildren<{
45
55
  initialInput?: string;
56
+ revision?: number;
57
+ onSubmittedClear?: () => void;
46
58
  attachmentPolicy?: AttachmentPolicy;
47
59
  onRemoveAttachment?: () => void;
48
60
  value?: string;
@@ -54,6 +66,6 @@ export type PromptInputProviderProps = PropsWithChildren<{
54
66
  id: string;
55
67
  })[]) => void;
56
68
  }>;
57
- export declare function PromptInputProvider({ initialInput: initialTextInput, onRemoveAttachment, attachmentPolicy, value, onValueChange, attachments: attachmentsControlled, onAttachmentsChange, children }: PromptInputProviderProps): import("react").JSX.Element;
69
+ export declare function PromptInputProvider({ initialInput: initialTextInput, revision, onSubmittedClear, onRemoveAttachment, attachmentPolicy, value, onValueChange, attachments: attachmentsControlled, onAttachmentsChange, children }: PromptInputProviderProps): import("react").JSX.Element;
58
70
  export declare const LocalAttachmentsContext: import("react").Context<AttachmentsContext | null>;
59
71
  export declare function usePromptInputAttachments(): AttachmentsContext;
@@ -1 +1 @@
1
- import{jsx as M}from"react/jsx-runtime";import{createContext as a,useCallback as r,useContext as i,useEffect as J,useMemo as S,useRef as g,useState as _}from"react";var v=a(null),K=a(null);function Z(){return i(K)}var b=a(null);function V(){let n=i(v);if(!n)throw Error("Wrap your component inside <PromptInputProvider> to use usePromptInputController().");return n}var tt=()=>i(v);function et(){let n=i(b);if(!n)throw Error("Wrap your component inside <PromptInputProvider> to use useProviderAttachments().");return n}var N=()=>i(b);function nt({initialInput:n="",onRemoveAttachment:l,attachmentPolicy:o,value:x,onValueChange:U,attachments:I,onAttachmentsChange:R,children:q}){let w=r((t)=>{if(t.type)return t.type;let e=(t.name||"").toLowerCase();if(e.endsWith(".png"))return"image/png";if(e.endsWith(".jpg")||e.endsWith(".jpeg"))return"image/jpeg";if(e.endsWith(".webp"))return"image/webp";if(e.endsWith(".gif"))return"image/gif";if(e.endsWith(".bmp"))return"image/bmp";if(e.endsWith(".svg"))return"image/svg+xml";return""},[]),A=x!==void 0,[z,D]=_(n),L=x!==void 0?x:z,d=r((t)=>{if(A)U?.(t);else D(t)},[A,U]),j=r(()=>d(""),[d]),f=I!==void 0,[B,G]=_([]),m=I!==void 0?I:B,P=g(m);P.current=m;let u=r((t)=>{let e=typeof t==="function"?t(P.current):t;if(f)R?.(e);else G(e)},[f,R]),W=g(null),y=g(()=>{}),C=r((t)=>{let e=Array.from(t);if(e.length===0)return;u((c)=>{let p=o===void 0?e:o.uniqueFiles(e,c);return c.concat(p.map((s)=>{let E=o?.sourcePath(s);return{id:crypto.randomUUID(),type:"file",url:URL.createObjectURL(s),mediaType:w(s),filename:s.name,size:s.size,lastModified:s.lastModified,...E===void 0?{}:{sourcePath:E}}}))})},[o,w,u]),O=r((t)=>{u((e)=>{let c=e.find((p)=>p.id===t);if(c?.url)URL.revokeObjectURL(c.url);return e.filter((p)=>p.id!==t)}),l?.()},[l,u]),T=r(()=>{u((t)=>{for(let e of t)if(e.url)URL.revokeObjectURL(e.url);return[]})},[u]);J(()=>{if(f)return;return()=>{for(let t of P.current)if(t.url)URL.revokeObjectURL(t.url)}},[f]);let F=r(()=>{y.current?.()},[]),h=S(()=>({files:m,add:C,remove:O,clear:T,openFileDialog:F,fileInputRef:W}),[m,C,O,T,F]),k=r((t,e)=>{W.current=t.current,y.current=e},[]),H=S(()=>({textInput:{value:L,setInput:d,clear:j},attachments:h,__registerFileInput:k}),[L,d,j,h,k]);return M(v.Provider,{value:H,children:M(b.Provider,{value:h,children:q})})}var Q=a(null);function rt(){let n=N(),l=i(Q),o=n??l;if(!o)throw Error("usePromptInputAttachments must be used within a PromptInput or PromptInputProvider");return o}export{Q as LocalAttachmentsContext,nt as PromptInputProvider,K as PromptInputSubmitContext,b as ProviderAttachmentsContext,tt as useOptionalPromptInputController,rt as usePromptInputAttachments,V as usePromptInputController,Z as usePromptInputSubmit,et as useProviderAttachments};
1
+ import{jsx as z}from"react/jsx-runtime";import{createContext as m,useCallback as n,useContext as u,useEffect as X,useMemo as D,useRef as v,useState as B}from"react";var w=m(null),Y=m(null);function rt(){return u(Y)}var Z=m(null);function ot(){return u(Z)}var A=m(null);function ut(){let r=u(w);if(!r)throw Error("Wrap your component inside <PromptInputProvider> to use usePromptInputController().");return r}var st=()=>u(w);function it(){let r=u(A);if(!r)throw Error("Wrap your component inside <PromptInputProvider> to use useProviderAttachments().");return r}var V=()=>u(A);function ct({initialInput:r="",revision:p,onSubmittedClear:s,onRemoveAttachment:L,attachmentPolicy:a,value:g,onValueChange:j,attachments:h,onAttachmentsChange:W,children:G}){let y=n((t)=>{if(t.type)return t.type;let e=(t.name||"").toLowerCase();if(e.endsWith(".png"))return"image/png";if(e.endsWith(".jpg")||e.endsWith(".jpeg"))return"image/jpeg";if(e.endsWith(".webp"))return"image/webp";if(e.endsWith(".gif"))return"image/gif";if(e.endsWith(".bmp"))return"image/bmp";if(e.endsWith(".svg"))return"image/svg+xml";return""},[]),T=g!==void 0,[H,J]=B(r),l=g!==void 0?g:H,o=v({value:l,external:p,revision:0});if(o.current.value!==l||o.current.external!==p)o.current={value:l,external:p,revision:o.current.revision+1};let C=n(()=>o.current.revision,[]),x=n((t)=>{if(o.current.value=t,o.current.revision++,T)j?.(t);else J(t)},[T,j]),b=n(()=>x(""),[x]),I=h!==void 0,[K,N]=B([]),P=h!==void 0?h:K,R=v(P);R.current=P;let i=n((t)=>{let e=typeof t==="function"?t(R.current):t;if(I)W?.(e);else N(e)},[I,W]),O=v(null),k=v(()=>{}),F=n((t)=>{let e=Array.from(t);if(e.length===0)return;i((f)=>{let d=a===void 0?e:a.uniqueFiles(e,f);return f.concat(d.map((c)=>{let q=a?.sourcePath(c);return{id:crypto.randomUUID(),type:"file",url:URL.createObjectURL(c),mediaType:y(c),filename:c.name,size:c.size,lastModified:c.lastModified,...q===void 0?{}:{sourcePath:q}}}))})},[a,y,i]),E=n((t)=>{i((e)=>{let f=e.find((d)=>d.id===t);if(f?.url)URL.revokeObjectURL(f.url);return e.filter((d)=>d.id!==t)}),L?.()},[L,i]),M=n(()=>{i((t)=>{for(let e of t)if(e.url)URL.revokeObjectURL(e.url);return[]})},[i]);X(()=>{if(I)return;return()=>{for(let t of R.current)if(t.url)URL.revokeObjectURL(t.url)}},[I]);let S=n(()=>{k.current?.()},[]),U=D(()=>({files:P,add:F,remove:E,clear:M,openFileDialog:S,fileInputRef:O}),[P,F,E,M,S]),_=n((t,e)=>{O.current=t.current,k.current=e},[]),Q=D(()=>({textInput:{value:l,setInput:x,clear:b,getRevision:C,clearSubmitted:s===void 0?b:s},attachments:U,__registerFileInput:_}),[l,x,b,C,s,U,_]);return z(w.Provider,{value:Q,children:z(A.Provider,{value:U,children:G})})}var tt=m(null);function pt(){let r=V(),p=u(tt),s=r??p;if(!s)throw Error("usePromptInputAttachments must be used within a PromptInput or PromptInputProvider");return s}export{tt as LocalAttachmentsContext,ct as PromptInputProvider,Z as PromptInputSubmissionContext,Y as PromptInputSubmitContext,A as ProviderAttachmentsContext,st as useOptionalPromptInputController,pt as usePromptInputAttachments,ut as usePromptInputController,ot as usePromptInputSubmission,rt as usePromptInputSubmit,it as useProviderAttachments};
@@ -13,6 +13,8 @@ export type PromptInputProps = Omit<HTMLAttributes<HTMLFormElement>, "onSubmit"
13
13
  attachmentPolicy?: AttachmentPolicy;
14
14
  onCommand?: (text: string) => boolean;
15
15
  onSubmitError: (error: unknown) => void;
16
+ canSubmit?: () => true | string;
17
+ onSubmitAsync?: (message: PromptInputMessage) => Promise<void>;
16
18
  accept?: string;
17
19
  multiple?: boolean;
18
20
  globalDrop?: boolean;
@@ -25,7 +27,7 @@ export type PromptInputProps = Omit<HTMLAttributes<HTMLFormElement>, "onSubmit"
25
27
  }) => void;
26
28
  onSubmit: (message: PromptInputMessage, event: FormEvent<HTMLFormElement>) => void | Promise<void>;
27
29
  };
28
- export declare const PromptInput: ({ className, bashMode, attachmentPolicy, onCommand, onSubmitError, accept, multiple, globalDrop, syncHiddenInput, maxFiles, maxFileSize, onError, onSubmit, children, ...props }: PromptInputProps) => import("react").JSX.Element;
30
+ export declare const PromptInput: ({ className, bashMode, attachmentPolicy, onCommand, onSubmitError, canSubmit, onSubmitAsync, accept, multiple, globalDrop, syncHiddenInput, maxFiles, maxFileSize, onError, onSubmit, children, ...props }: PromptInputProps) => import("react").JSX.Element;
29
31
  export type PromptInputBodyProps = HTMLAttributes<HTMLDivElement>;
30
32
  export declare function PromptInputBody({ className, ...props }: PromptInputBodyProps): import("react").JSX.Element;
31
33
  export type PromptInputHeaderProps = Omit<ComponentProps<typeof InputGroupAddon>, "align">;
@@ -1 +1 @@
1
- import{jsx as o,jsxs as ce}from"react/jsx-runtime";import{ArrowUpIcon as ue,Loader2Icon as le,SquareIcon as fe,XIcon as de}from"lucide-react";import{Children as X,useCallback as g,useEffect as w,useMemo as me,useRef as N,useState as pe}from"react";import{flushSync as ge}from"react-dom";import{cn as h}from"cn";import{InputGroup as he,InputGroupAddon as H,InputGroupButton as J}from"../ui/input-group.js";import{exceedsImageLimit as ve,MAX_TOTAL_IMAGE_MB as be}from"../lib/limits.js";import{preparePromptFiles as xe}from"../lib/prepare-prompt-files.js";import{LocalAttachmentsContext as ye,PromptInputSubmitContext as Ie,ProviderAttachmentsContext as Le,useOptionalPromptInputController as Te}from"./prompt-input-context.js";import{MAX_TOTAL_IMAGE_MB as Ge}from"../lib/limits.js";var Re=({className:a,bashMode:c=!1,attachmentPolicy:v,onCommand:m,onSubmitError:b,accept:l,multiple:I,globalDrop:z,syncHiddenInput:U,maxFiles:P,maxFileSize:j,onError:p,onSubmit:K,children:Q,...V})=>{let i=Te(),s=!!i,Y=c&&s&&i.textInput.value.startsWith("!"),x=N(null),C=N(null),M=N(!1),[Z,R]=pe([]),f=s?i.attachments.files:Z,W=N(f);W.current=f;let ee=g(()=>{x.current?.click()},[]),k=g((e)=>{if(!l||l.trim()==="")return!0;return l.split(",").map((r)=>r.trim()).filter(Boolean).some((r)=>{if(r.endsWith("/*")){let n=r.slice(0,-1);return e.type.startsWith(n)}return e.type===r})},[l]),D=g((e)=>{if(e.type)return e.type;let t=(e.name||"").toLowerCase();if(t.endsWith(".png"))return"image/png";if(t.endsWith(".jpg")||t.endsWith(".jpeg"))return"image/jpeg";if(t.endsWith(".webp"))return"image/webp";if(t.endsWith(".gif"))return"image/gif";if(t.endsWith(".bmp"))return"image/bmp";if(t.endsWith(".svg"))return"image/svg+xml";return""},[]),te=g((e)=>{let t=Array.from(e),r=t.filter((u)=>k(u));if(t.length&&r.length===0){p?.({code:"accept",message:"No files match the accepted types."});return}let n=(u)=>j?u.size<=j:!0,d=r.filter(n);if(r.length>0&&d.length===0){p?.({code:"max_file_size",message:"All files exceed the maximum size."});return}R((u)=>{let T=v?v.uniqueFiles(d,u):d,A=typeof P==="number"?Math.max(0,P-u.length):void 0,ae=typeof A==="number"?T.slice(0,A):T;if(typeof A==="number"&&T.length>A)p?.({code:"max_files",message:"Too many files. Some were not added."});let q=[];for(let L of ae){let E=v?.sourcePath(L);q.push({id:crypto.randomUUID(),type:"file",url:URL.createObjectURL(L),mediaType:D(L),filename:L.name,size:L.size,lastModified:L.lastModified,...E===void 0?{}:{sourcePath:E}})}return u.concat(q)})},[v,D,k,P,j,p]),re=g((e)=>R((t)=>{let r=t.find((n)=>n.id===e);if(r?.url)URL.revokeObjectURL(r.url);return t.filter((n)=>n.id!==e)}),[]),ne=g(()=>R((e)=>{for(let t of e)if(t.url)URL.revokeObjectURL(t.url);return[]}),[]),O=s?i.attachments.add:te,y=g((e)=>{let t=Array.from(e),r=t.filter((d)=>!(d.type??"").toLowerCase().startsWith("image/"));if(r.length>0){p?.({code:"unsupported_type",message:`Only image attachments are supported. ${r.length} non-image file(s) were ignored.`});return}let n=t.reduce((d,u)=>d+u.size,0);if(n>0){let d=f.filter((u)=>(u.mediaType??"").toLowerCase().startsWith("image/")).reduce((u,T)=>u+(T.size??0),0);if(ve(d,n)){p?.({code:"max_total_size",message:`Total image size must not exceed ${be}MB.`});return}}O(t)},[f,O,p]),S=s?i.attachments.remove:re,_=s?i.attachments.clear:ne,B=s?i.attachments.openFileDialog:ee;w(()=>{if(!s)return;i.__registerFileInput(x,()=>x.current?.click())},[s,i]),w(()=>{if(U&&x.current&&f.length===0)x.current.value=""},[f,U]),w(()=>{let e=C.current;if(!e)return;if(z)return;let t=(n)=>{if(n.dataTransfer?.types?.includes("Files"))n.preventDefault()},r=(n)=>{if(n.dataTransfer?.types?.includes("Files"))n.preventDefault();if(n.dataTransfer?.files&&n.dataTransfer.files.length>0)y(n.dataTransfer.files)};return e.addEventListener("dragover",t),e.addEventListener("drop",r),()=>{e.removeEventListener("dragover",t),e.removeEventListener("drop",r)}},[y,z]),w(()=>{if(!z)return;let e=(r)=>{if(r.dataTransfer?.types?.includes("Files"))r.preventDefault()},t=(r)=>{if(r.dataTransfer?.types?.includes("Files"))r.preventDefault();if(r.dataTransfer?.files&&r.dataTransfer.files.length>0)y(r.dataTransfer.files)};return document.addEventListener("dragover",e),document.addEventListener("drop",t),()=>{document.removeEventListener("dragover",e),document.removeEventListener("drop",t)}},[y,z]),w(()=>()=>{if(!s){for(let e of W.current)if(e.url)URL.revokeObjectURL(e.url)}},[s]);let se=(e)=>{if(e.currentTarget.files)y(e.currentTarget.files);e.currentTarget.value=""},F=me(()=>({files:f.map((e)=>({...e,id:e.id})),add:y,remove:S,clear:_,openFileDialog:B,fileInputRef:x}),[f,y,S,_,B]),ie=async(e)=>{if(e.preventDefault(),M.current)return;M.current=!0;try{let t=e.currentTarget,r=s?i.textInput.value:new FormData(t).get("message")||"";if(m?.(r.trim())){if(t.reset(),_(),s)i.textInput.clear();return}if(!s)t.reset();let n=await xe(f);if(n.rejectedCount>0)p?.({code:"accept",message:"One or more images are corrupted and were removed before sending."});if(await K({text:r,files:n.files},e),_(),s)i.textInput.clear()}catch(t){b(t)}finally{M.current=!1}},oe=g((e)=>{if(!e.trim())return;if(s&&i)ge(()=>{i.textInput.setInput(e)});C.current?.requestSubmit()},[i,s]),G=ce(Ie.Provider,{value:oe,children:[o("input",{accept:l,"aria-label":"Upload files",className:"hidden",multiple:I,onChange:se,ref:x,title:"Upload files",type:"file"}),o("form",{className:h("w-full",a),onSubmit:ie,ref:C,"data-tour":"chat-input",...V,children:o(he,{className:h("codex-composer-surface overflow-y-auto has-[[data-slot=input-group-control]:focus-visible]:ring-0",Y&&"border! border-[rgb(255,0,135)] has-[[data-slot=input-group-control]:focus-visible]:border-[rgb(255,0,135)] dark:border-[rgb(253,93,177)] dark:has-[[data-slot=input-group-control]:focus-visible]:border-[rgb(253,93,177)]"),children:Q})})]});return s?o(Le.Provider,{value:F,children:G}):o(ye.Provider,{value:F,children:G})};function Ue({className:a,...c}){return o("div",{className:h("flex w-full min-w-0 flex-row items-end gap-2",a),...c})}function We({className:a,...c}){return o(H,{align:"block-end",className:h("order-first flex-wrap gap-1 p-2 pb-1.5",a),...c})}function ke({className:a,...c}){return o(H,{align:"block-end",className:h("mt-3.5 justify-between gap-1 p-0",a),...c})}function De({className:a,...c}){return o("div",{className:h("flex items-center gap-1",a),...c})}var Oe=({variant:a="ghost",className:c,size:v,collapsed:m=!1,...b})=>{let l=m||X.count(b.children)<=1?"icon-xs":"xs";return o(J,{className:h("h-7 rounded-full text-xs font-medium hover:bg-transparent dark:hover:bg-transparent",(m||X.count(b.children)<=1)&&"size-7",c),size:l,type:"button",variant:a,...b})},Se=({className:a,variant:c="ghost",size:v="icon-xs",status:m,children:b,...l})=>{let I=o(ue,{className:"size-4"});if(m==="submitted")I=o(le,{className:"size-4 animate-spin"});else if(m==="streaming")I=o(fe,{className:"size-3 fill-current"});else if(m==="error")I=o(de,{className:"size-4"});return o(J,{"aria-label":"Submit",className:h("bg-foreground text-background hover:bg-foreground/70 size-7 rounded-full p-0 transition-opacity disabled:opacity-50",a),size:v,type:"submit",variant:c,...l,children:b??I})};export{Ge as MAX_TOTAL_IMAGE_MB,Re as PromptInput,Ue as PromptInputBody,Oe as PromptInputButton,ke as PromptInputFooter,We as PromptInputHeader,Se as PromptInputSubmit,De as PromptInputTools};
1
+ import{jsx as o,jsxs as me}from"react/jsx-runtime";import{ArrowUpIcon as pe,Loader2Icon as ge,SquareIcon as he,XIcon as be}from"lucide-react";import{Children as V,useCallback as v,useEffect as z,useMemo as ve,useRef as _,useState as xe}from"react";import{flushSync as Ie}from"react-dom";import{cn as x}from"cn";import{InputGroup as ye,InputGroupAddon as Y,InputGroupButton as Z}from"../ui/input-group.js";import{exceedsImageLimit as Le,MAX_TOTAL_IMAGE_MB as we}from"../lib/limits.js";import{preparePromptFiles as Te}from"../lib/prepare-prompt-files.js";import{LocalAttachmentsContext as ze,PromptInputSubmitContext as _e,PromptInputSubmissionContext as ke,ProviderAttachmentsContext as Ae,useOptionalPromptInputController as Pe}from"./prompt-input-context.js";import{MAX_TOTAL_IMAGE_MB as Je}from"../lib/limits.js";var Fe=({className:c,bashMode:l=!1,attachmentPolicy:I,onCommand:m,onSubmitError:y,canSubmit:p,onSubmitAsync:g,accept:k,multiple:ee,globalDrop:A,syncHiddenInput:D,maxFiles:S,maxFileSize:j,onError:h,onSubmit:te,children:re,...ne})=>{let s=Pe(),a=!!s,se=l&&a&&s.textInput.value.startsWith("!"),L=_(null),P=_(null),M=_(!1),[ie,U]=xe([]),d=a?s.attachments.files:ie,F=_(d);F.current=d;let oe=v(()=>{L.current?.click()},[]),O=v((e)=>{if(!k||k.trim()==="")return!0;return k.split(",").map((t)=>t.trim()).filter(Boolean).some((t)=>{if(t.endsWith("/*")){let n=t.slice(0,-1);return e.type.startsWith(n)}return e.type===t})},[k]),B=v((e)=>{if(e.type)return e.type;let r=(e.name||"").toLowerCase();if(r.endsWith(".png"))return"image/png";if(r.endsWith(".jpg")||r.endsWith(".jpeg"))return"image/jpeg";if(r.endsWith(".webp"))return"image/webp";if(r.endsWith(".gif"))return"image/gif";if(r.endsWith(".bmp"))return"image/bmp";if(r.endsWith(".svg"))return"image/svg+xml";return""},[]),ae=v((e)=>{let r=Array.from(e),t=r.filter((i)=>O(i));if(r.length&&t.length===0){h?.({code:"accept",message:"No files match the accepted types."});return}let n=(i)=>j?i.size<=j:!0,f=t.filter(n);if(t.length>0&&f.length===0){h?.({code:"max_file_size",message:"All files exceed the maximum size."});return}U((i)=>{let b=I?I.uniqueFiles(f,i):f,u=typeof S==="number"?Math.max(0,S-i.length):void 0,N=typeof u==="number"?b.slice(0,u):b;if(typeof u==="number"&&b.length>u)h?.({code:"max_files",message:"Too many files. Some were not added."});let K=[];for(let T of N){let Q=I?.sourcePath(T);K.push({id:crypto.randomUUID(),type:"file",url:URL.createObjectURL(T),mediaType:B(T),filename:T.name,size:T.size,lastModified:T.lastModified,...Q===void 0?{}:{sourcePath:Q}})}return i.concat(K)})},[I,B,O,S,j,h]),ue=v((e)=>U((r)=>{let t=r.find((n)=>n.id===e);if(t?.url)URL.revokeObjectURL(t.url);return r.filter((n)=>n.id!==e)}),[]),ce=v(()=>U((e)=>{for(let r of e)if(r.url)URL.revokeObjectURL(r.url);return[]}),[]),q=a?s.attachments.add:ae,w=v((e)=>{let r=Array.from(e),t=r.filter((f)=>!(f.type??"").toLowerCase().startsWith("image/"));if(t.length>0){h?.({code:"unsupported_type",message:`Only image attachments are supported. ${t.length} non-image file(s) were ignored.`});return}let n=r.reduce((f,i)=>f+i.size,0);if(n>0){let f=d.filter((i)=>(i.mediaType??"").toLowerCase().startsWith("image/")).reduce((i,b)=>i+(b.size??0),0);if(Le(f,n)){h?.({code:"max_total_size",message:`Total image size must not exceed ${we}MB.`});return}}q(r)},[d,q,h]),E=a?s.attachments.remove:ue,W=a?s.attachments.clear:ce,G=a?s.attachments.openFileDialog:oe;z(()=>{if(!a)return;s.__registerFileInput(L,()=>L.current?.click())},[a,s]),z(()=>{if(D&&L.current&&d.length===0)L.current.value=""},[d,D]),z(()=>{let e=P.current;if(!e)return;if(A)return;let r=(n)=>{if(n.dataTransfer?.types?.includes("Files"))n.preventDefault()},t=(n)=>{if(n.dataTransfer?.types?.includes("Files"))n.preventDefault();if(n.dataTransfer?.files&&n.dataTransfer.files.length>0)w(n.dataTransfer.files)};return e.addEventListener("dragover",r),e.addEventListener("drop",t),()=>{e.removeEventListener("dragover",r),e.removeEventListener("drop",t)}},[w,A]),z(()=>{if(!A)return;let e=(t)=>{if(t.dataTransfer?.types?.includes("Files"))t.preventDefault()},r=(t)=>{if(t.dataTransfer?.types?.includes("Files"))t.preventDefault();if(t.dataTransfer?.files&&t.dataTransfer.files.length>0)w(t.dataTransfer.files)};return document.addEventListener("dragover",e),document.addEventListener("drop",r),()=>{document.removeEventListener("dragover",e),document.removeEventListener("drop",r)}},[w,A]),z(()=>()=>{if(!a){for(let e of F.current)if(e.url)URL.revokeObjectURL(e.url)}},[a]);let le=(e)=>{if(e.currentTarget.files)w(e.currentTarget.files);e.currentTarget.value=""},X=ve(()=>({files:d.map((e)=>({...e,id:e.id})),add:w,remove:E,clear:W,openFileDialog:G,fileInputRef:L}),[d,w,E,W,G]),R=_({controller:s,files:d,canSubmit:p});R.current={controller:s,files:d,canSubmit:p};let C=_(!0);z(()=>(C.current=!0,()=>{C.current=!1}),[]);let H=async(e)=>{let r=p===void 0?!0:p();if(r!==!0)return{status:"blocked",reason:r};if(M.current)return{status:"blocked",reason:"submission_pending"};if(e===null&&g===void 0)throw Error("Awaitable submission requires onSubmitAsync");let t=P.current;if(t===null)return{status:"blocked",reason:"form_unmounted"};let n=a?s.textInput.value:String(new FormData(t).get("message")??""),f=s?.textInput.getRevision?.(),i=d,b=()=>{if(!C.current)return;let u=R.current;if(u.files!==i)return;if(a){if(f===void 0||u.controller===null||u.controller.textInput.getRevision?.()!==f)return;if(u.controller.textInput.clearSubmitted===void 0)u.controller.textInput.clear();else u.controller.textInput.clearSubmitted()}else if(String(new FormData(t).get("message")??"")===n)t.reset();W()};M.current=!0;try{if(m?.(n.trim()))return b(),{status:"accepted"};let u=await Te(i),N=R.current.canSubmit===void 0?!0:R.current.canSubmit();if(N!==!0)return{status:"blocked",reason:N};if(!C.current)return{status:"blocked",reason:"form_unmounted"};if(u.rejectedCount>0)h?.({code:"accept",message:"One or more images are corrupted and were removed before sending."});if(g!==void 0)await g({text:n,files:u.files});else if(e!==null)await te({text:n,files:u.files},e);return b(),{status:"accepted"}}finally{M.current=!1}},fe=(e)=>{e.preventDefault(),H(e).catch(y)},de=v((e)=>{if(!e.trim())return;if(a&&s)Ie(()=>{s.textInput.setInput(e)});P.current?.requestSubmit()},[s,a]),J=o(ke.Provider,{value:()=>H(null),children:me(_e.Provider,{value:de,children:[o("input",{accept:k,"aria-label":"Upload files",className:"hidden",multiple:ee,onChange:le,ref:L,title:"Upload files",type:"file"}),o("form",{className:x("w-full",c),onSubmit:fe,ref:P,"data-tour":"chat-input",...ne,children:o(ye,{className:x("codex-composer-surface overflow-y-auto has-[[data-slot=input-group-control]:focus-visible]:ring-0",se&&"border! border-[rgb(255,0,135)] has-[[data-slot=input-group-control]:focus-visible]:border-[rgb(255,0,135)] dark:border-[rgb(253,93,177)] dark:has-[[data-slot=input-group-control]:focus-visible]:border-[rgb(253,93,177)]"),children:re})})]})});return a?o(Ae.Provider,{value:X,children:J}):o(ze.Provider,{value:X,children:J})};function Oe({className:c,...l}){return o("div",{className:x("flex w-full min-w-0 flex-row items-end gap-2",c),...l})}function Be({className:c,...l}){return o(Y,{align:"block-end",className:x("order-first flex-wrap gap-1 p-2 pb-1.5",c),...l})}function qe({className:c,...l}){return o(Y,{align:"block-end",className:x("mt-3.5 justify-between gap-1 p-0",c),...l})}function Ee({className:c,...l}){return o("div",{className:x("flex items-center gap-1",c),...l})}var Ge=({variant:c="ghost",className:l,size:I,collapsed:m=!1,...y})=>{let p=m||V.count(y.children)<=1?"icon-xs":"xs";return o(Z,{className:x("h-7 rounded-full text-xs font-medium hover:bg-transparent dark:hover:bg-transparent",(m||V.count(y.children)<=1)&&"size-7",l),size:p,type:"button",variant:c,...y})},Xe=({className:c,variant:l="ghost",size:I="icon-xs",status:m,children:y,...p})=>{let g=o(pe,{className:"size-4"});if(m==="submitted")g=o(ge,{className:"size-4 animate-spin"});else if(m==="streaming")g=o(he,{className:"size-3 fill-current"});else if(m==="error")g=o(be,{className:"size-4"});return o(Z,{"aria-label":"Submit",className:x("bg-foreground text-background hover:bg-foreground/70 size-7 rounded-full p-0 transition-opacity disabled:opacity-50",c),size:I,type:"submit",variant:l,...p,children:y??g})};export{Je as MAX_TOTAL_IMAGE_MB,Fe as PromptInput,Oe as PromptInputBody,Ge as PromptInputButton,qe as PromptInputFooter,Be as PromptInputHeader,Xe as PromptInputSubmit,Ee as PromptInputTools};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alwith/module-chat",
3
- "version": "0.1.16",
3
+ "version": "0.1.17",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {