@swifty.js/swifty 0.0.15 → 0.0.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.
package/README.md CHANGED
@@ -178,9 +178,12 @@ Inside the TUI, these commands are available:
178
178
 
179
179
  ### Keyboard Shortcuts
180
180
 
181
- | Key | Action |
182
- | --------- | -------------------------------------------------------------------- |
183
- | Ctrl+C | Interrupt streaming (first press), exit app (second press within 2s) |
184
- | Ctrl+O | Toggle full vs. truncated tool output |
185
- | Ctrl+T | Toggle Teams dialog overlay |
186
- | Shift+Tab | Cycle permission modes |
181
+ | Key | Action |
182
+ | --------- | ----------------------------------------------------------------------------------- |
183
+ | Ctrl+C | Clear input or interrupt streaming (first press), exit app (second press within 2s) |
184
+ | Ctrl+O | Toggle full vs. truncated tool output |
185
+ | Ctrl+T | Toggle Teams dialog overlay |
186
+ | Ctrl+V | Paste a clipboard image (Alt+V on Windows) |
187
+ | Shift+Tab | Cycle permission modes |
188
+
189
+ Pasting an image saves it as a PNG under `.swifty/file-history/<session-id>/` and inserts a workDir-relative `@` reference into the prompt; on submit it expands into an inline image block like any other `@image` mention. Linux requires `wl-clipboard` (Wayland) or `xclip` (X11).
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env node
2
2
  import { createRequire as __swiftyCreateRequire } from "node:module";
3
3
  const require = __swiftyCreateRequire(import.meta.url);
4
- import{h as a}from"./chunk-CYMOBNZF.js";import"./chunk-6ZXBZB2Y.js";import"./chunk-FZPTNGTU.js";import"./chunk-J5LWUHF6.js";import"./chunk-X3UA5OZL.js";export{a as Agent};
4
+ import{h as a}from"./chunk-WF7FXRNB.js";import"./chunk-QU55DC4L.js";import"./chunk-FZPTNGTU.js";import"./chunk-ZM2V2A7B.js";import"./chunk-X3UA5OZL.js";export{a as Agent};
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import { createRequire as __swiftyCreateRequire } from "node:module";
3
+ const require = __swiftyCreateRequire(import.meta.url);
4
+ import{a as $}from"./chunk-3USGFIWF.js";import{g as P}from"./chunk-H7VXSMJR.js";import{k as C}from"./chunk-QU55DC4L.js";import{e as H,g as Y,h as R}from"./chunk-JMPU45A5.js";import{b as N,c as K,d as M,e as W,f as F,g as G}from"./chunk-FZPTNGTU.js";import"./chunk-P276SKAC.js";import"./chunk-7MHXMDYC.js";import{C as w,I as O,J as v,K as j,L as B,M as I,N as _,O as U,g as S}from"./chunk-ZM2V2A7B.js";import"./chunk-E55KH72M.js";import"./chunk-X3UA5OZL.js";function D(o){return o.some(e=>e.defer_loading)}var x=O({module:"llm"});var J=3e3,z=w.object({max_input_tokens:w.coerce.number()});async function ct(o){if(o.protocol!=="anthropic")return 0;let e=R(o),n=`${o.base_url.replace(/\/+$/,"")}/v1/models/${encodeURIComponent(o.model)}`,s=new AbortController,r=setTimeout(()=>{s.abort()},J);try{let l=await fetch(n,{method:"GET",headers:{"anthropic-version":"2023-06-01",...e?{"x-api-key":e}:{}},signal:s.signal});if(!l.ok)return 0;let h=await l.json(),{success:g,error:f,data:d}=await S(z,h);if(!g)return x.warn({message:f.message},"model context window schema validation failed"),0;let k=d.max_input_tokens;return Math.max(k,0)}catch(l){return x.error({err:l},"failed to fetch model context window"),0}finally{clearTimeout(r)}}function V(){return!0}function E(o){return typeof o=="string"?[{type:"text",text:o}]:o}function Q(o){let e=[];for(let t of o)if(t.role==="assistant"){let n=[];if(t.thinkingBlocks)for(let r of t.thinkingBlocks)n.push({type:"thinking",thinking:r.thinking,signature:r.signature});let s=typeof t.content=="string"?t.content:j(t.content);if(s&&n.push({type:"text",text:s}),t.toolUses)for(let r of t.toolUses)n.push({type:"tool_use",id:r.toolUseId,name:r.toolName,input:r.arguments});n.length===0&&n.push({type:"text",text:""}),e.push({role:"assistant",content:n})}else if(t.toolResults&&t.toolResults.length>0){let n=[];for(let s of t.toolResults)n.push({type:"tool_result",tool_use_id:s.toolUseId,is_error:s.isError,content:s.content});e.push({role:"user",content:n})}else{if(e.length===0){e.push({role:"user",content:E(t.content)});continue}let n=!1,s=e[e.length-1],r=s.content;s.role==="user"&&(typeof r=="string"||Array.isArray(r)&&r.length>0&&(r[0].type==="text"||r[0].type==="image"))&&(n=!0),n?(typeof r=="string"&&(r=s.content=r.trim().length>0?[{type:"text",text:r}]:[]),r.push(...E(t.content))):e.push({role:"user",content:E(t.content)})}return e}var q=class{client;model;thinking;systemPrompt;maxOutputTokens;contextWindow;constructor(e,t){let n=R(e);if(!n)throw new M("Anthropic API key not found, set ANTHROPIC_API_KEY in .swifty/config.y(a)ml, or via ANTHROPIC_API_KEY env variable.");this.client=new P({apiKey:n,baseURL:e.base_url}),this.model=e.model,this.thinking=e.thinking??!0,this.systemPrompt=t,this.maxOutputTokens=Y(e),this.contextWindow=H(e)}setSystemPrompt(e){this.systemPrompt=e}setMaxOutputTokens(e){this.maxOutputTokens=e}async*stream(e,t,n){let s=Q(N(e.getMessages())),r=D(t),l=t.map(c=>{let p=c.input_schema,u={name:c.name,description:c.description,input_schema:{type:"object",properties:p.properties,required:p.required??[]}};return c.defer_loading===!0&&(u.defer_loading=!0),u});l.length>0&&(l[l.length-1].cache_control={type:"ephemeral"}),X(s);let h={model:this.model,max_tokens:this.maxOutputTokens,stream:!0,system:[{type:"text",text:this.systemPrompt,cache_control:{type:"ephemeral"}}],messages:s,...l.length>0?{tools:l}:{}};this.thinking?V()&&(h.thinking={type:"enabled",budget_tokens:C(this.contextWindow,this.maxOutputTokens)}):h.thinking={type:"enabled",budget_tokens:C(this.contextWindow,this.maxOutputTokens)};let g=0,f=0,d=0,k=0,L="end_turn",T="",b="",A=!1;try{let c=this.client.messages.stream(h,{...n?{signal:n}:{},...r?{headers:{"anthropic-beta":$}}:{}}),p="",u="",m="";for await(let i of c)switch(i.type){case"content_block_start":{let a=i.content_block;a.type==="thinking"?(A=!0,T="",b=""):a.type==="tool_use"&&(u=a.id,p=a.name,m="",yield{type:"tool_call_start",toolName:p,toolId:u});break}case"content_block_delta":{let a=i.delta;a.type==="thinking_delta"?(T+=a.thinking,yield{type:"thinking_delta",text:a.thinking}):a.type==="signature_delta"?b=a.signature:a.type==="text_delta"?yield{type:"text_delta",text:a.text}:a.type==="input_json_delta"&&(m+=a.partial_json,yield{type:"tool_call_delta",text:a.partial_json});break}case"content_block_stop":{if(A&&(yield{type:"thinking_complete",thinking:T,signature:b},A=!1),p){let a={};if(m)try{let y=JSON.parse(m);a=B(y)?I(y):{[v]:m}}catch(y){x.error({err:y},"llm operation failed"),a={[v]:m}}yield{type:"tool_call_complete",toolId:u,toolName:p,arguments:a},p="",u="",m=""}break}case"message_delta":{i.delta.stop_reason&&(L=i.delta.stop_reason),i.usage.output_tokens&&(f=i.usage.output_tokens,i.usage.input_tokens&&(g=i.usage.input_tokens),i.usage.cache_read_input_tokens&&(d=i.usage.cache_read_input_tokens),i.usage.cache_creation_input_tokens&&(k=i.usage.cache_creation_input_tokens));break}case"message_start":{g=i.message.usage.input_tokens,f=i.message.usage.output_tokens,d=i.message.usage.cache_read_input_tokens??0,k=i.message.usage.cache_creation_input_tokens??0;break}}yield{type:"stream_end",stopReason:L,usage:{inputTokens:g,outputTokens:f,cacheReadInputTokens:d,cacheCreationInputTokens:k}}}catch(c){throw x.error({err:c},"llm operation failed"),Z(c)}}};function X(o){for(let e=o.length-1;e>=0;e--){if(o[e].role!=="user")continue;let t=o[e].content;if(typeof t=="string"&&t.length===0||Array.isArray(t)&&t.length===0)return;typeof t=="string"&&(t=o[e].content=[{type:"text",text:t}]);let n=t[t.length-1];for(let s=t.length-1;s>=0;s--)if(t[s].type!=="image"){n=t[s];break}Reflect.set(n,"cache_control",{type:"ephemeral"})}}function Z(o){if(o instanceof P.APIError){if(o.status===413||/prompts?\s+too\s+long/i.test(o.message))return new G(`Prompt too long: ${o.message}`);if(o.status===401)return new M(`Invalid API key: ${o.message}`);if(o.status===429){let e=I(o.headers)["retry-after"],t="Rate Limited";if(e){let n=Number.parseInt(_(e));Number.isNaN(n)&&(t+=", please wait."),t+=`, retry after ${_(n)}s.`}else t+=", please wait.";return new W(t,e?_(e):void 0)}return new K(`Anthropic API error (${_(o.status)}): ${o.message}`)}return new F(`Network error: ${U(o)}`)}export{q as AnthropicClient,Q as buildAnthropicMessages,ct as fetchModelContextWindow,X as markLastUserTailForCache,D as needsToolSearchBeta};
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env node
2
2
  import { createRequire as __swiftyCreateRequire } from "node:module";
3
3
  const require = __swiftyCreateRequire(import.meta.url);
4
- import{a,b,c,d,e}from"./chunk-32AZCTP4.js";import"./chunk-5CPQ6RWA.js";import"./chunk-7MHXMDYC.js";import"./chunk-J5LWUHF6.js";import"./chunk-E55KH72M.js";import"./chunk-X3UA5OZL.js";export{b as PathSandbox,e as PermissionChecker,d as RuleEngine,c as evaluateRules,a as extractContent};
4
+ import{a,b,c,d,e}from"./chunk-GM65WMIB.js";import"./chunk-P276SKAC.js";import"./chunk-7MHXMDYC.js";import"./chunk-ZM2V2A7B.js";import"./chunk-E55KH72M.js";import"./chunk-X3UA5OZL.js";export{b as PathSandbox,e as PermissionChecker,d as RuleEngine,c as evaluateRules,a as extractContent};
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env node
2
2
  import { createRequire as __swiftyCreateRequire } from "node:module";
3
3
  const require = __swiftyCreateRequire(import.meta.url);
4
- import{a as i,d as s}from"./chunk-5CPQ6RWA.js";var a=10,p=2.5,E="advanced-tool-use-2025-11-20",f=new Set(["api.anthropic.com"]),d="SWIFTY_MCP_LOADING";function u(o){if(!o)return!0;try{return f.has(new URL(o).hostname.toLowerCase())}catch{return!1}}function l(o){return Math.floor(o/p)}function m(o,t,e,n=a){let r=(process.env[d]??"").trim().toLowerCase();if(r==="eager"||r==="native"||r==="dispatch")return r;if(e<=0)return"eager";let c=t*n/100;return l(e)<c?"eager":u(o)?"native":"dispatch"}function g(o){let t=0;for(let e of o.listTools())if(e.name.startsWith(i))try{t+=JSON.stringify(e.schema()).length}catch{t+=e.name.length+(e.description?.length??0)}return t}function T(o,t){o.mcpLoadingMode=t;let e=t==="eager";for(let n of o.listTools())s(n)&&typeof n.setDeferLoading=="function"&&n.setDeferLoading(!e);o.exposeToolSearch=!e,o.exposeMcpCall=t==="dispatch"}function M(o,t,e){let n=m(t,e,g(o));return T(o,n),n}export{E as a,M as b};
4
+ import{a as i,d as s}from"./chunk-P276SKAC.js";var a=10,p=2.5,E="advanced-tool-use-2025-11-20",f=new Set(["api.anthropic.com"]),d="SWIFTY_MCP_LOADING";function u(o){if(!o)return!0;try{return f.has(new URL(o).hostname.toLowerCase())}catch{return!1}}function l(o){return Math.floor(o/p)}function m(o,t,e,n=a){let r=(process.env[d]??"").trim().toLowerCase();if(r==="eager"||r==="native"||r==="dispatch")return r;if(e<=0)return"eager";let c=t*n/100;return l(e)<c?"eager":u(o)?"native":"dispatch"}function g(o){let t=0;for(let e of o.listTools())if(e.name.startsWith(i))try{t+=JSON.stringify(e.schema()).length}catch{t+=e.name.length+(e.description?.length??0)}return t}function T(o,t){o.mcpLoadingMode=t;let e=t==="eager";for(let n of o.listTools())s(n)&&typeof n.setDeferLoading=="function"&&n.setDeferLoading(!e);o.exposeToolSearch=!e,o.exposeMcpCall=t==="dispatch"}function M(o,t,e){let n=m(t,e,g(o));return T(o,n),n}export{E as a,M as b};