-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
544 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "ai", | ||
"type": "module", | ||
"version": "1.0.0", | ||
"description": "Vercel AI mockup", | ||
"exports": { | ||
"./rsc": { | ||
"types": "./src/index.d.ts", | ||
"react-server": "./src/server.js", | ||
"import": "./src/client.js" | ||
} | ||
}, | ||
"devDependencies": { | ||
"react-dom": "^18", | ||
"react-server-dom-webpack": "18.3.0-canary-eb33bd747-20240312" | ||
}, | ||
"peerDependencies": { | ||
"react": "^18 || ^19" | ||
}, | ||
"private": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use client'; | ||
import { useActions } from './shared.js'; | ||
|
||
export { useActions }; | ||
|
||
export function createAI() { | ||
throw new Error('You should not call createAI in the client side'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { ReactNode } from 'react'; | ||
|
||
declare function createAI( | ||
actions: Record<string, any>, | ||
): (props: { children: ReactNode }) => ReactNode; | ||
|
||
declare function useActions(): Record<string, any>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use server'; | ||
import { InternalProvider } from './shared.js'; | ||
import { jsx } from 'react/jsx-runtime'; | ||
|
||
async function innerAction({ action }, state, ...args) { | ||
'use server'; | ||
const result = await action(...args); | ||
// eslint-disable-next-line no-undef | ||
console.log('wrapped action', result); | ||
return result; | ||
} | ||
|
||
function wrapAction(action, options) { | ||
return innerAction.bind(null, { action, options }); | ||
} | ||
|
||
export function createAI(actions) { | ||
const wrappedActions = {}; | ||
for (const name in actions) { | ||
wrappedActions[name] = wrapAction(actions[name]); | ||
} | ||
return function AI(props) { | ||
return jsx(InternalProvider, { | ||
actions: wrappedActions, | ||
children: props.children, | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use client'; | ||
import { createContext, useContext } from 'react'; | ||
import { jsx } from 'react/jsx-runtime'; | ||
|
||
const ActionContext = createContext(null); | ||
|
||
export function useActions() { | ||
return useContext(ActionContext); | ||
} | ||
|
||
export function InternalProvider(props) { | ||
return jsx('div', { | ||
'data-testid': 'ai-internal-provider', | ||
children: jsx(ActionContext.Provider, { | ||
value: props.actions, | ||
children: props.children, | ||
}), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
e2e/fixtures/rsc-basic/src/components/ServerAction/Client.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use client'; | ||
|
||
import { useActions } from 'ai/rsc'; | ||
import { useEffect } from 'react'; | ||
|
||
export const ClientActionsConsumer = () => { | ||
const actions = useActions(); | ||
useEffect(() => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
globalThis.actions = actions; | ||
}, [actions]); | ||
return <div>{JSON.stringify(Object.keys(actions))}</div>; | ||
}; |
15 changes: 15 additions & 0 deletions
15
e2e/fixtures/rsc-basic/src/components/ServerAction/Server.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { ReactNode } from 'react'; | ||
import { createAI } from 'ai/rsc'; | ||
|
||
const AI = createAI({ | ||
actions: { | ||
foo: async () => { | ||
'use server'; | ||
return 0; | ||
}, | ||
}, | ||
}); | ||
|
||
export function ServerProvider({ children }: { children: ReactNode }) { | ||
return <AI>{children}</AI>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.