ai-ax 0.1.1 → 0.2.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/README.md +1 -1
- package/dist/action.d.ts +12 -1
- package/dist/client.d.ts +3 -2
- package/dist/client.js +1 -1
- package/dist/const.d.ts +18 -0
- package/dist/const.js +1 -0
- package/dist/index.d.ts +3 -9
- package/dist/index.js +2 -2
- package/dist/server.d.ts +5 -3
- package/dist/server.js +1 -1
- package/dist/{action-DItZAgdx.d.ts → types.d.ts} +1 -12
- package/dist/types.js +1 -0
- package/package.json +1 -5
- package/dist/const-DMb65zjR.js +0 -1
- package/dist/const-Hb5KgSHO.d.ts +0 -5
package/README.md
CHANGED
|
@@ -40,7 +40,7 @@ flowchart TB
|
|
|
40
40
|
worker には 4 つの middleware を並べるだけでよい。
|
|
41
41
|
|
|
42
42
|
```ts
|
|
43
|
-
const ax = createAiax({ name: 'demo', own: ownSelf(), database: (e) => e.
|
|
43
|
+
const ax = createAiax({ name: 'demo', own: ownSelf(), database: (e) => e.my_d1_aiax, system: PROMPT })
|
|
44
44
|
const app = new Hono().use('*', ax.auth()).use('/parties/*', ax.party()).all('/api/mcp', ax.mcp()).post('/api/llm', ax.llm())
|
|
45
45
|
```
|
|
46
46
|
|
package/dist/action.d.ts
CHANGED
|
@@ -1,2 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ActMeta, ActionDef, Side } from "./types.js";
|
|
2
|
+
import * as Y from "yjs";
|
|
3
|
+
//#region src/action.d.ts
|
|
4
|
+
type Action<I, R> = (input: I) => R | Promise<R>;
|
|
5
|
+
type Call = (name: string, input: any) => Promise<any>;
|
|
6
|
+
declare const actions: Map<string, ActionDef>;
|
|
7
|
+
declare const side: Side;
|
|
8
|
+
declare const bind: (doc: Y.Doc) => Y.Doc;
|
|
9
|
+
declare const data: <T = any>(name?: string) => Y.Map<T>;
|
|
10
|
+
declare function action<I extends object, R>(load: Action<I, R> | ((input?: I) => any), at: Side, meta: ActMeta): (input: I) => Promise<R>;
|
|
11
|
+
declare const init: (doc: Y.Doc, wrap?: (run: () => Promise<any>) => Promise<any>) => Call;
|
|
12
|
+
//#endregion
|
|
2
13
|
export { Call, action, actions, bind, data, init, side };
|
package/dist/client.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ActMeta, ActionDef, Side } from "./types.js";
|
|
2
|
+
import { Call, action, actions, bind, data, init, side } from "./action.js";
|
|
3
|
+
import { ALIAS, BINDING } from "./const.js";
|
|
3
4
|
import * as Y from "yjs";
|
|
4
5
|
//#region src/client.d.ts
|
|
5
6
|
interface Route {
|
package/dist/client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{ALIAS as e,BINDING as t}from"./const.js";import{action as n,actions as r,bind as i,data as a,init as o,side as s}from"./action.js";import c from"y-partyserver/provider";import*as l from"yjs";let u=null,d=new Set;const f=e=>void d.add(e),p=(e={})=>{u?.leave();let{room:t=``,path:n=``,host:r=location.host,party:i=`v1`,doc:a}=e,s=t?new c(r,t,a,{party:i,params:{path:n}}):null,f=s?.doc??a??new l.Doc;return u={doc:f,call:o(f),synced:e=>{if(!s||s.synced)return void e();s.once(`synced`,()=>e())},leave:()=>{d.forEach(e=>e()),d=new Set,s?.destroy()}}};export{e as ALIAS,t as BINDING,n as action,r as actions,i as bind,p as connect,a as data,o as init,f as onLeave,s as side};
|
package/dist/const.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/const.d.ts
|
|
2
|
+
declare const ALIAS = "my-room";
|
|
3
|
+
declare const SUB_HEADER = "x-user-sub";
|
|
4
|
+
declare const PATH_HEADER = "x-user-path";
|
|
5
|
+
declare const SALT = "authjs.mcp-token";
|
|
6
|
+
declare const MAX_AGE: number;
|
|
7
|
+
declare const BINDING = "v1";
|
|
8
|
+
declare const MCP_PATH = "/api/mcp";
|
|
9
|
+
declare const LLM_PATH = "/api/llm";
|
|
10
|
+
declare const MCP_VERSION = "1.0.0";
|
|
11
|
+
declare const CLAUDE_URL = "https://api.anthropic.com/v1/messages";
|
|
12
|
+
declare const CLAUDE_MODEL = "claude-sonnet-5";
|
|
13
|
+
declare const CLAUDE_VERSION = "2023-06-01";
|
|
14
|
+
declare const CLAUDE_BETA = "mcp-client-2025-11-20";
|
|
15
|
+
declare const MAX_TOKENS = 32000;
|
|
16
|
+
declare const MAX_TURNS = 10;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { ALIAS, BINDING, CLAUDE_BETA, CLAUDE_MODEL, CLAUDE_URL, CLAUDE_VERSION, LLM_PATH, MAX_AGE, MAX_TOKENS, MAX_TURNS, MCP_PATH, MCP_VERSION, PATH_HEADER, SALT, SUB_HEADER };
|
package/dist/const.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=`my-room`,t=`x-user-sub`,n=`x-user-path`,r=`authjs.mcp-token`,i=43200,a=`v1`,o=`/api/mcp`,s=`/api/llm`,c=`1.0.0`,l=`https://api.anthropic.com/v1/messages`,u=`claude-sonnet-5`,d=`2023-06-01`,f=`mcp-client-2025-11-20`,p=32e3,m=10;export{e as ALIAS,a as BINDING,f as CLAUDE_BETA,u as CLAUDE_MODEL,l as CLAUDE_URL,d as CLAUDE_VERSION,s as LLM_PATH,i as MAX_AGE,p as MAX_TOKENS,m as MAX_TURNS,o as MCP_PATH,c as MCP_VERSION,n as PATH_HEADER,r as SALT,t as SUB_HEADER};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { AiaxServer } from "./server.js";
|
|
4
|
-
//#region src/auth.d.ts
|
|
1
|
+
import { Config, Own } from "./types.js";
|
|
2
|
+
//#region src/worker/auth.d.ts
|
|
5
3
|
declare const ownSite: (bucket: (env: any) => any, meta?: string) => Own;
|
|
6
4
|
declare const ownSelf: () => Own;
|
|
7
5
|
//#endregion
|
|
8
|
-
//#region src/mcp.d.ts
|
|
9
|
-
declare const grantOf: (request: Request) => Grant;
|
|
10
|
-
//#endregion
|
|
11
6
|
//#region src/index.d.ts
|
|
12
7
|
declare const createAiax: (config: Config) => {
|
|
13
8
|
config: Config;
|
|
14
|
-
Server: typeof AiaxServer;
|
|
15
9
|
all: () => import("hono").MiddlewareHandler<any, string, {}, Response>;
|
|
16
10
|
auth: () => import("hono").MiddlewareHandler;
|
|
17
11
|
party: () => import("hono").MiddlewareHandler<any, string, {}, any>;
|
|
@@ -19,4 +13,4 @@ declare const createAiax: (config: Config) => {
|
|
|
19
13
|
llm: () => import("hono").MiddlewareHandler<any, string, {}, Response>;
|
|
20
14
|
};
|
|
21
15
|
//#endregion
|
|
22
|
-
export {
|
|
16
|
+
export { createAiax, ownSelf, ownSite };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
2
|
-
`);u=f.pop()??``;for(let e of f){let c=
|
|
1
|
+
import{CLAUDE_BETA as e,CLAUDE_VERSION as t,PATH_HEADER as n,SUB_HEADER as r}from"./const.js";import{actions as i}from"./action.js";import{createMiddleware as a}from"hono/factory";import o from"@auth/core/providers/google";import{decode as s,encode as c}from"@auth/core/jwt";import{DrizzleAdapter as l}from"@auth/drizzle-adapter";import{getAuthUser as u,initAuthConfig as d}from"@hono/auth-js";import{drizzle as f}from"drizzle-orm/d1";import{env as p}from"hono/adapter";import{streamText as m}from"hono/streaming";import{StreamableHTTPTransport as h}from"@hono/mcp";import{McpServer as g}from"@modelcontextprotocol/sdk/server/mcp.js";import{getServerByName as _,routePartykitRequest as v}from"partyserver";const y=e=>d(t=>{let n=new URL(t.req.url).host,r=!!e.cookie&&e.cookie.services.some(t=>n===`${t}.${e.cookie.apex}`),i={sessionToken:{options:{domain:`.${e.cookie?.apex}`}}},a=e.providers??[o({clientId:t.env.GOOGLE_CLIENT_ID,clientSecret:t.env.GOOGLE_CLIENT_SECRET})];return{...e.database?{adapter:l(f(e.database(t.env)))}:{},providers:a,secret:t.env.AUTH_SECRET,session:{strategy:`jwt`},...r?{cookies:i}:{}}}),b=async e=>(await u(e))?.token?.sub??``,x=(e,t,n,r)=>c({token:{sub:t,path:n},secret:p(e).AUTH_SECRET,salt:r?.salt??`authjs.mcp-token`,maxAge:r?.maxAge??43200}),S=async(e,t)=>{let n=(e.req.header(`Authorization`)??``).replace(/^Bearer\s+/i,``);if(!n)return null;let r=await s({token:n,secret:p(e).AUTH_SECRET,salt:t?.salt??`authjs.mcp-token`}).catch(()=>null);return{sub:r?.sub??``,path:r?.path??``}},C=(e,t=`site`)=>async(n,r,i)=>!r||!i?``:(await e(n).head(`${r}/${i}`))?.customMetadata?.[t]??``,w=()=>(e,t)=>t,T=e=>e.startsWith(`data:`)?JSON.parse(e.slice(5)):null,E=(n,r,i)=>fetch(n.api,{method:`POST`,headers:{"x-api-key":n.key,"anthropic-version":t,"anthropic-beta":e,"content-type":`application/json`},body:JSON.stringify({model:n.model,max_tokens:n.maxTokens,stream:!0,cache_control:{type:`ephemeral`},system:n.system,mcp_servers:[{type:`url`,url:n.url,name:n.name,...r?{authorization_token:r}:{}}],tools:[{type:`mcp_toolset`,mcp_server_name:n.name}],messages:i})}),D=async(e,t)=>{let n=[],r=null,i=0,a=0,o=0,s=``,c=e.body.getReader(),l=new TextDecoder,u=``;for(;;){let{done:e,value:d}=await c.read();if(e)break;u+=l.decode(d,{stream:!0});let f=u.split(`
|
|
2
|
+
`);u=f.pop()??``;for(let e of f){let c=T(e);if(c){if(c.type===`message_start`){let e=c.message?.usage??{};i+=(e.input_tokens??0)+(e.cache_creation_input_tokens??0)+(e.cache_read_input_tokens??0),a+=e.cache_read_input_tokens??0}c.type===`message_delta`&&(o=c.usage?.output_tokens??o,s=c.delta?.stop_reason??s),c.type===`content_block_start`&&(r={...c.content_block},n.push(r),r.type===`mcp_tool_use`&&await t(`\n[tool] ${r.name}\n`)),c.type===`content_block_delta`&&c.delta?.type===`text_delta`&&(r.text=(r.text??``)+c.delta.text,await t(c.delta.text)),c.type===`content_block_delta`&&c.delta?.type===`input_json_delta`&&(r.partial=(r.partial??``)+c.delta.partial_json),c.type===`content_block_delta`&&c.delta?.type===`thinking_delta`&&(r.thinking=(r.thinking??``)+c.delta.thinking),c.type===`content_block_delta`&&c.delta?.type===`signature_delta`&&(r.signature=(r.signature??``)+c.delta.signature),c.type===`content_block_stop`&&r?.partial!==void 0&&(r.input=r.partial?JSON.parse(r.partial):{},delete r.partial)}}}return{blocks:n,input:i,cached:a,output:o,stop:s}},O=async(e,t,n,r,i,a=10)=>{let o=r,s=0,c=0,l=0,u=``;for(let r=0;r<a;r++){let r=await D(o,i);if(s+=r.input,c+=r.cached,l+=r.output,u=r.stop||u,u!==`pause_turn`)break;if(n.push({role:`assistant`,content:r.blocks.filter(e=>e.type!==`thinking`||e.thinking||e.signature)}),o=await E(e,await t(),n),!o.ok){await i(await o.text());break}}await i(`\n[usage] input ${s} (cache ${c}) / output ${l} tokens (${u})\n`)},k=e=>a(async t=>{let n=await b(t),{path:r=``,prompt:i}=await t.req.json(),a=!!e.lobby&&!r;if(!a&&!n)return t.text(`Unauthorized`,401);if(!a&&!await e.own(p(t),n,r))return t.text(`Forbidden`,403);let o=t.req.header(`x-original-host`)??t.req.header(`x-forwarded-host`)??new URL(t.req.url).host,s={key:p(t).ANTHROPIC_API_KEY,api:e.claude??`https://api.anthropic.com/v1/messages`,name:e.name,url:`https://${o}${e.endpoint??`/api/mcp`}`,model:e.model??`claude-sonnet-5`,system:e.system??``,maxTokens:e.maxTokens??32e3},c=()=>a?Promise.resolve(``):x(t,n,r,e),l=[{role:`user`,content:i}],u=await E(s,await c(),l);return u.ok?m(t,t=>O(s,c,l,u,e=>t.write(e),e.maxTurns??10)):t.text(await u.text(),500)}),A=async(e,t,i,a,o)=>{let s=await _(e,t),c={[r]:i.sub,[n]:encodeURIComponent(i.path)};return(await s.fetch(`https://internal/act`,{method:`POST`,headers:c,body:JSON.stringify({name:a,input:o})})).json()},j=e=>a(async t=>{let n=await S(t,e),r=n?.sub??``,a=n?.path??``,o=e.lobby??``;if(n||!o){if(!r)return t.text(`Unauthorized`,401);if(o=await e.own(p(t),r,a),!o)return t.text(`Forbidden`,403)}let s=new g({name:e.name,version:e.version??`1.0.0`}),c=new h;return i.forEach((n,i)=>s.registerTool(i,{description:n.description,inputSchema:{...n.input}},async n=>{let s=await A(p(t)[e.binding??`v1`],o,{sub:r,path:a},i,n);return{content:[{type:`text`,text:JSON.stringify(s)}]}})),await s.connect(c),c.handleRequest(t)}),M=async(e,t,i,a)=>{let o=new Headers(e.req.raw.headers);o.set(r,i),o.set(n,encodeURIComponent(a));let s=new URL(e.req.url),c=s.pathname.split(`/`);c[3]=encodeURIComponent(t),s.pathname=c.join(`/`);let l=new Request(s,new Request(e.req.raw,{headers:o}));return await v(l,p(e))??e.text(`Not Found`,404)},N=e=>a(async t=>{let n=decodeURIComponent(t.req.path.split(`/`)[3]??``),r=t.req.query(`path`)??``,i=await b(t);if(e.lobby&&n===e.lobby)return M(t,n,i,r);if(!i)return t.text(`Unauthorized`,401);let a=await e.own(p(t),i,r);return!a||n!==a&&n!==(e.alias??`my-room`)?t.text(`Forbidden`,403):M(t,a,i,r)}),P=e=>{let t={auth:y(e),party:N(e),mcp:j(e),llm:k(e)},n=a(async(n,r)=>{let i=()=>n.req.path.startsWith(`/parties/`)?t.party(n,r):n.req.path===(e.endpoint??`/api/mcp`)?t.mcp(n,r):n.req.path===`/api/llm`&&n.req.method===`POST`?t.llm(n,r):r(),a;if(await t.auth(n,async()=>void(a=await i())),a)return a});return{config:e,all:()=>n,auth:()=>t.auth,party:()=>t.party,mcp:()=>t.mcp,llm:()=>t.llm}};export{P as createAiax,w as ownSelf,C as ownSite};
|
package/dist/server.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Grant } from "./types.js";
|
|
2
|
+
import { Call } from "./action.js";
|
|
2
3
|
import { YServer } from "y-partyserver";
|
|
3
4
|
//#region src/server.d.ts
|
|
4
5
|
declare class AiaxServer extends YServer {
|
|
5
6
|
wrap: (run: () => Promise<any>) => Promise<any>;
|
|
6
7
|
call: Call;
|
|
7
|
-
|
|
8
|
+
grantOf(request: Request): Grant;
|
|
9
|
+
onAct(_grant: Grant, _request: Request): Promise<void>;
|
|
8
10
|
onRequest(request: Request): Promise<Response>;
|
|
9
11
|
}
|
|
10
12
|
//#endregion
|
|
11
|
-
export { AiaxServer };
|
|
13
|
+
export { AiaxServer, type Grant };
|
package/dist/server.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{actions as e,bind as t,init as n}from"./action.js";import{YServer as r}from"y-partyserver";var i=class extends r{wrap=e=>e();call=n(this.document,e=>this.wrap(e));async onAct(e){}async onRequest(n){if(t(this.document),n.method!==`POST`)return new Response(`Not Found`,{status:404});let{name:r,input:i}=await n.json();return e.get(r)?.side===`client`?Response.json({ok:!0,result:await this.call(r,i)}):(await this.onAct(n),Response.json({ok:!0,result:await this.wrap(()=>this.call(r,i))}))}};export{i as AiaxServer};
|
|
1
|
+
import"./const.js";import{actions as e,bind as t,init as n}from"./action.js";import{YServer as r}from"y-partyserver";var i=class extends r{wrap=e=>e();call=n(this.document,e=>this.wrap(e));grantOf(e){return{sub:e.headers.get(`x-user-sub`)??``,path:decodeURIComponent(e.headers.get(`x-user-path`)??``)}}async onAct(e,t){}async onRequest(n){if(t(this.document),n.method!==`POST`)return new Response(`Not Found`,{status:404});let{name:r,input:i}=await n.json();return e.get(r)?.side===`client`?Response.json({ok:!0,result:await this.call(r,i)}):(await this.onAct(this.grantOf(n),n),Response.json({ok:!0,result:await this.wrap(()=>this.call(r,i))}))}};export{i as AiaxServer};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as Y from "yjs";
|
|
2
1
|
import { ZodRawShape } from "zod";
|
|
3
2
|
//#region src/types.d.ts
|
|
4
3
|
type Side = 'server' | 'client' | 'both';
|
|
@@ -40,14 +39,4 @@ interface Config {
|
|
|
40
39
|
providers?: any[];
|
|
41
40
|
}
|
|
42
41
|
//#endregion
|
|
43
|
-
|
|
44
|
-
type Action<I, R> = (input: I) => R | Promise<R>;
|
|
45
|
-
type Call = (name: string, input: any) => Promise<any>;
|
|
46
|
-
declare const actions: Map<string, ActionDef>;
|
|
47
|
-
declare const side: Side;
|
|
48
|
-
declare const bind: (doc: Y.Doc) => Y.Doc;
|
|
49
|
-
declare const data: <T = any>(name?: string) => Y.Map<T>;
|
|
50
|
-
declare function action<I extends object, R>(load: Action<I, R> | ((input?: I) => any), at: Side, meta: ActMeta): (input: I) => Promise<R>;
|
|
51
|
-
declare const init: (doc: Y.Doc, wrap?: (run: () => Promise<any>) => Promise<any>) => Call;
|
|
52
|
-
//#endregion
|
|
53
|
-
export { data as a, ActMeta as c, Cookie as d, Grant as f, bind as i, ActionDef as l, Side as m, action as n, init as o, Own as p, actions as r, side as s, Call as t, Config as u };
|
|
42
|
+
export { ActMeta, ActionDef, Config, Cookie, Grant, Own, Side };
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-ax",
|
|
3
3
|
"author": "tseijp",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "AI Agent Experience (AI/AX) library for Hono + Cloudflare. One act surface for both human UI/UX and LLM MCP tools, with authentication, authorization, realtime sync and Claude API relay.",
|
|
7
7
|
"keywords": [
|
|
@@ -49,10 +49,6 @@
|
|
|
49
49
|
"./src/*": {
|
|
50
50
|
"types": "./src/*.ts",
|
|
51
51
|
"default": "./src/*.ts"
|
|
52
|
-
},
|
|
53
|
-
"./*": {
|
|
54
|
-
"types": "./dist/index.d.ts",
|
|
55
|
-
"default": "./dist/index.js"
|
|
56
52
|
}
|
|
57
53
|
},
|
|
58
54
|
"files": [
|
package/dist/const-DMb65zjR.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const e=`my-room`,t=`x-user-sub`,n=`x-user-path`,r=`authjs.mcp-token`,i=43200,a=`v1`,o=`/api/mcp`,s=`/api/llm`,c=`1.0.0`,l=`https://api.anthropic.com/v1/messages`,u=`claude-sonnet-5`,d=`2023-06-01`,f=`mcp-client-2025-11-20`,p=32e3;export{l as a,i as c,c as d,n as f,u as i,p as l,t as m,a as n,d as o,r as p,f as r,s,e as t,o as u};
|