UNPKG

takumi

Version:

A coding agent CLI to enhance your development workflow.

2 lines (1 loc) 6.6 kB
import{r as t,M as De,A as c}from"./index-C0R0gBPH.js";import{g as Se,f as Q,u as P,e as je,c as Me,s as Re,p as ne,a as Ee,b as ke,i as Te,d as Ae}from"./index-BgIL2Wp3.js";function Ne(s){var a=t.useRef(s);return a.current=s,a}var Ie=function(s){var a=Ne(s);t.useEffect(function(){return function(){a.current()}},[])},X,oe;function Fe(){if(oe)return X;oe=1;function s(a,m){if(typeof a!="function")throw new TypeError(`Expected the first argument to be a \`function\`, got \`${typeof a}\`.`);let C,h=0;return function(...g){clearTimeout(C);const x=Date.now(),n=x-h,f=m-n;f<=0?(h=x,a.apply(this,g)):C=setTimeout(()=>{h=Date.now(),a.apply(this,g)},f)}}return X=s,X}var _e=Fe();const qe=De(_e);function le(s,a){return a!=null?qe(s,a):s}function Le(s){const[a,m]=t.useState(s);return t.useEffect(()=>{Ae(s,a)||m(s)},[s,a]),a}function Pe({api:s="/api/chat",id:a,initialMessages:m,initialInput:C="",sendExtraMessageFields:h,onToolCall:w,experimental_prepareRequestBody:g,maxSteps:x=1,streamProtocol:n="data",onResponse:f,onFinish:Y,onError:U,credentials:J,headers:K,body:O,generateId:y=Se,fetch:Z,keepLastMessageOnError:B=!0,experimental_throttle:W}={}){const[ue]=t.useState(y),D=a??ue,F=typeof s=="string"?[s,D]:D,V=Le(m??[]),ce=t.useMemo(()=>Q(V),[V]),{data:E,mutate:S}=P([F,"messages"],null,{fallbackData:ce}),i=t.useRef(E||[]);t.useEffect(()=>{i.current=E||[]},[E]);const{data:_,mutate:q}=P([F,"streamData"],null),k=t.useRef(_);t.useEffect(()=>{k.current=_},[_]);const{data:j="ready",mutate:M}=P([F,"status"],null),{data:ie=void 0,mutate:$}=P([F,"error"],null),p=t.useRef(null),v=t.useRef({credentials:J,headers:K,body:O});t.useEffect(()=>{v.current={credentials:J,headers:K,body:O}},[J,K,O]);const d=t.useCallback(async(e,l="generate")=>{var o,u;M("submitted"),$(void 0);const r=Q(e.messages),A=r.length,R=je((o=r[r.length-1])==null?void 0:o.toolInvocations);try{const b=new AbortController;p.current=b;const G=le(S,W),ve=le(q,W),Ce=i.current;G(r,!1);const we=h?r:r.map(({role:H,content:I,experimental_attachments:L,data:te,annotations:se,toolInvocations:ae,parts:re})=>({role:H,content:I,...L!==void 0&&{experimental_attachments:L},...te!==void 0&&{data:te},...se!==void 0&&{annotations:se},...ae!==void 0&&{toolInvocations:ae},...re!==void 0&&{parts:re}})),ee=k.current;await Me({api:s,body:(u=g==null?void 0:g({id:D,messages:r,requestData:e.data,requestBody:e.body}))!=null?u:{id:D,messages:we,data:e.data,...v.current.body,...e.body},streamProtocol:n,credentials:v.current.credentials,headers:{...v.current.headers,...e.headers},abortController:()=>p.current,restoreMessagesOnFailure(){B||G(Ce,!1)},onResponse:f,onUpdate({message:H,data:I,replaceLastMessage:L}){M("streaming"),G([...L?r.slice(0,r.length-1):r,H],!1),I!=null&&I.length&&ve([...ee??[],...I],!1)},onToolCall:w,onFinish:Y,generateId:y,fetch:Z,lastMessage:r[r.length-1],requestType:l}),p.current=null,M("ready")}catch(b){if(b.name==="AbortError")return p.current=null,M("ready"),null;U&&b instanceof Error&&U(b),$(b),M("error")}const N=i.current;Re({originalMaxToolInvocationStep:R,originalMessageCount:A,maxSteps:x,messages:N})&&await d({messages:N})},[S,M,s,v,f,Y,U,$,q,k,n,h,g,w,x,i,p,y,Z,B,W,D]),de=t.useCallback(async(e,{data:l,headers:o,body:u,experimental_attachments:r=e.experimental_attachments}={})=>{var A,R;const N=await ne(r),b=i.current.concat({...e,id:(A=e.id)!=null?A:y(),createdAt:(R=e.createdAt)!=null?R:new Date,experimental_attachments:N.length>0?N:void 0,parts:Ee(e)});return d({messages:b,headers:o,body:u,data:l})},[d,y]),me=t.useCallback(async({data:e,headers:l,body:o}={})=>{const u=i.current;if(u.length===0)return null;const r=u[u.length-1];return d({messages:r.role==="assistant"?u.slice(0,-1):u,headers:l,body:o,data:e})},[d]),fe=t.useCallback(()=>{p.current&&(p.current.abort(),p.current=null)},[]),ge=t.useCallback(async()=>{const e=i.current;d({messages:e},"resume")},[d]),he=t.useCallback(e=>{typeof e=="function"&&(e=e(i.current));const l=Q(e);S(l,!1),i.current=l},[S]),pe=t.useCallback(e=>{typeof e=="function"&&(e=e(k.current)),q(e,!1),k.current=e},[q]),[T,z]=t.useState(C),be=t.useCallback(async(e,l={},o)=>{var u;if((u=e==null?void 0:e.preventDefault)==null||u.call(e),!T&&!l.allowEmptySubmit)return;o&&(v.current={...v.current,...o});const r=await ne(l.experimental_attachments),R={messages:i.current.concat({id:y(),createdAt:new Date,role:"user",content:T,experimental_attachments:r.length>0?r:void 0,parts:[{type:"text",text:T}]}),headers:l.headers,body:l.body,data:l.data};d(R),z("")},[T,y,d]),xe=e=>{z(e.target.value)},ye=t.useCallback(({toolCallId:e,result:l})=>{const o=i.current;if(ke({messages:o,toolCallId:e,toolResult:l}),S([...o.slice(0,o.length-1),{...o[o.length-1]}],!1),j==="submitted"||j==="streaming")return;const u=o[o.length-1];Te(u)&&d({messages:o})},[S,j,d]);return{messages:E??[],id:D,setMessages:he,data:_,setData:pe,error:ie,append:de,reload:me,stop:fe,experimental_resume:ge,input:T,setInput:z,handleInputChange:xe,handleSubmit:be,isLoading:j==="submitted"||j==="streaming",status:j,addToolResult:ye}}const Ke=()=>{const{input:s,setInput:a,status:m,handleSubmit:C,messages:h,stop:w,data:g,setData:x}=Pe({api:"/api/chat/completions",body:{model:"takumi",plan:!0},onFinish(n){console.log("Response data:",n)},onError(n){console.error("Error:",n)},onResponse(n){console.log("Response:",n)},onToolCall({toolCall:n}){console.log("Tool call:",n)}});return Ie(()=>{w()}),c.jsxs("div",{className:"flex flex-col w-full max-w-md py-24 mx-auto stretch",children:[g&&c.jsxs("div",{children:[c.jsx("pre",{className:"p-4 text-sm bg-gray-100",children:JSON.stringify(g,null,2)}),c.jsx("button",{onClick:()=>x(void 0),className:"px-4 py-2 mt-2 text-white bg-blue-500 rounded hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50",children:"Clear Data"})]}),h.map(n=>c.jsxs("div",{className:"whitespace-pre-wrap",children:[c.jsx("strong",{children:n.role==="user"?"User: ":"AI: "}),n.parts.map(f=>c.jsxs("div",{children:[c.jsxs("pre",{children:["思考",f.type==="reasoning"&&f.reasoning]}),f.type==="text"&&f.text]})),c.jsx("br",{}),c.jsx("br",{})]},n.id)),(m==="submitted"||m==="streaming")&&c.jsxs("div",{className:"mt-4 text-gray-500",children:[m==="submitted"&&c.jsx("div",{children:"Loading..."}),c.jsx("button",{type:"button",className:"px-4 py-2 mt-4 text-blue-500 border border-blue-500 rounded-md",onClick:w,children:"Stop"})]}),c.jsx("form",{onSubmit:C,children:c.jsx("input",{className:"fixed bottom-0 w-full max-w-md p-2 mb-8 border border-gray-300 rounded shadow-xl",value:s,onChange:n=>a(n.target.value)})})]})};export{Ke as component};