Skip to content

Commit

Permalink
docs: context API (WIP) (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Jun 23, 2024
1 parent f0da6e7 commit 392b728
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 80 deletions.
1 change: 1 addition & 0 deletions apps/www/pages/reference/_meta.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const meta = {
runtime: "Runtime API",
context: "Context API",
"-- integrations": {
title: "Integrations",
type: "separator",
Expand Down
76 changes: 76 additions & 0 deletions apps/www/pages/reference/context.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
title: Context API
---

assistant-ui components and APIs use React Context to store information and interact with the runtime.

The context is split into four hierarchies:

- **Assistant Context**: Wraps your entire app. An app can contain multiple threads.
- **Thread Context**: Provides access to the thread state. A thread can contain multiple messages.
- **Message Context**: Provides access to the message state. A message can contain multiple content parts.
- **Content Part Context**: Provides access to the content part state.


import { AssistantContextValue } from "./contextParameters";

## Assistant Context

The **Assistant Context** provides access for ModelConfigState and ToolUIsState.

### `useAssistantContext`

This hook providers access to the context stores.

```tsx
import { useAssistantContext } from "@assistant-ui/react";

const { useModelConfig, useToolUIs } = useAssistantContext();
```

#### Return Type

<AssistantContextValue />


## Thread Context

The **Thread Context** provides access to ThreadState, ThreadActionsState, ComposerState and ThreadViewportState.

### `useThreadContext`

This hook provides access to the context stores.

```tsx
import { useThreadContext } from "@assistant-ui/react";

const { useThread, useThreadActions, useComposer, useViewport } = useThreadContext();
```

## Message Context

The **Message Context** provides access to MessageState, EditComposerState and MessageUtilsState.

### `useMessageContext`

This hook provides access to the context stores.

```tsx
import { useMessageContext } from "@assistant-ui/react";

const { useMessage, useMessageUtils, useComposer } = useMessageContext();
```

## Content Part Context

The **Content Part Context** provides access to ContentPartState.

### `useContentPartContext`

This hook provides access to the context stores.

```tsx
import { useContentPartContext } from "@assistant-ui/react";

const { useContentPart } = useContentPartContext();
```
125 changes: 125 additions & 0 deletions apps/www/pages/reference/contextParameters.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { ParametersTable } from "@/components/docs";

export const AssistantContextValue = () => {
return (
<ParametersTable
type="AssistantContextValue"
parameters={[
{
name: "useModelConfig",
type: "ReadonlyStore<ModelConfigState>",
required: true,
description:
"Configuration of the model (system prompt, tools, etc.)",
children: [
{
type: "ModelConfigState",
parameters: [
{
name: "getModelConfig",
type: "() => ModelConfig",
description: "Gets the current model config.",
required: true,
children: [
{
type: "ModelConfig",
parameters: [
{
name: "system",
type: "string",
description: "The system prompt.",
},
{
name: "tools",
type: "Record<string, Tool<any, any>>",
description: "The tools available to the model.",
children: [
{
type: "Tool<TArgs, TResult>",
parameters: [
{
name: "description",
type: "string",
description: "The tool description.",
},
{
name: "parameters",
type: "z.ZodType<TArgs>",
description: "The tool parameters.",
},
{
name: "execute",
type: "(args: TArgs) => Promise<TResult>",
description: "The tool execution function.",
},
],
},
],
},
],
},
],
},
{
name: "registerModelConfigProvider",
type: "(provider: () => ModelConfig) => Unsubscribe",
description:
"Registers a model config provider to update the model config.",
required: true,
},
],
},
],
},
{
name: "useToolUIs",
type: "ReadonlyStore<ToolUIsState>",
required: true,
description: "Tool UIs to render on tool calls.",
children: [
{
type: "ToolUIsState",
parameters: [
{
name: "getToolUI",
type: "(toolName: string) => ToolCallContentPartProps",
description:
"Gets the current tool UI for a given tool name.",
required: true,
children: [
{
type: "ToolCallContentPartProps<TArgs, TResult>",
parameters: [
{
name: "part",
type: "ToolCallContentPart<TArgs, TResult>",
description: "The tool call content part.",
},
{
name: "status",
type: "'in_progress' | 'done' | 'error'",
description: "The tool call status.",
},
{
name: "addResult",
type: "(result: TResult) => void",
description: "Adds a result to the tool call.",
},
],
},
],
},
{
name: "setToolUI",
type: "(toolName: string, render: ToolCallContentPartComponent) => Unsubscribe",
description: "Sets the tool UI.",
required: true,
},
],
},
],
},
]}
/>
);
};
82 changes: 2 additions & 80 deletions apps/www/pages/reference/runtime.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
---
title: Runtime API
description: A React Hook Form integration for @assistant-ui.
---

import { ParametersTable } from "@/components/docs";
import { AssistantRuntimeProviderProps } from "./runtimeParameters";

### `AssistantRuntimeProvider`

Expand All @@ -26,81 +25,4 @@ const MyApp = () => {

#### Properties

<ParametersTable
type="AssistantRuntimeProviderProps"
parameters={[
{
name: "runtime",
type: "AssistantRuntime",
required: true,
description: "The runtime to provide to the rest of your app.",
children: [
{
type: "AssistantRuntime",
parameters: [
{
name: "messages",
type: "readonly ThreadMessage[]",
required: true,
description: "The messages in the thread.",
},
{
name: "isRunning",
type: "boolean",
required: true,
description: "Whether the thread is running.",
},
{
name: "getBranches",
type: "(messageId: string) => readonly string[]",
required: true,
description: "A function to get the branches for a message.",
},
{
name: "switchToBranch",
type: "(branchId: string) => void",
required: true,
description: "A function to switch to a branch.",
},
{
name: "append",
type: "(message: AppendMessage) => void",
required: true,
description: "A function to append a message to the thread.",
},
{
name: "startRun",
type: "(parentId: string | null) => void",
required: true,
description: "A function to start a run.",
},
{
name: "cancelRun",
type: "() => void",
required: true,
description: "A function to cancel a run.",
},
{
name: "addToolResult",
type: "(toolCallId: string, result: any) => void",
required: true,
description: "A function to add a tool result.",
},
{
name: "subscribe",
type: "(callback: () => void) => Unsubscribe",
required: true,
description: "A function to subscribe to updates.",
},
{
name: "registerModelConfigProvider",
type: "(provider: ModelConfigProvider) => Unsubscribe",
required: true,
description: "A function to register a model config provider.",
},
],
},
],
},
]}
/>
<AssistantRuntimeProviderProps />
85 changes: 85 additions & 0 deletions apps/www/pages/reference/runtimeParameters.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { ParametersTable } from "@/components/docs";

export const AssistantRuntimeProviderProps = () => {
return (
<ParametersTable
type="AssistantRuntimeProviderProps"
parameters={[
{
name: "runtime",
type: "AssistantRuntime",
required: true,
description: "The runtime to provide to the rest of your app.",
children: [
{
type: "AssistantRuntime",
parameters: [
{
name: "messages",
type: "readonly ThreadMessage[]",
required: true,
description: "The messages in the thread.",
},
{
name: "isRunning",
type: "boolean",
required: true,
description: "Whether the thread is running.",
},
{
name: "getBranches",
type: "(messageId: string) => readonly string[]",
required: true,
description: "A function to get the branches for a message.",
},
{
name: "switchToBranch",
type: "(branchId: string) => void",
required: true,
description: "A function to switch to a branch.",
},
{
name: "append",
type: "(message: AppendMessage) => void",
required: true,
description: "A function to append a message to the thread.",
},
{
name: "startRun",
type: "(parentId: string | null) => void",
required: true,
description: "A function to start a run.",
},
{
name: "cancelRun",
type: "() => void",
required: true,
description: "A function to cancel a run.",
},
{
name: "addToolResult",
type: "(toolCallId: string, result: any) => void",
required: true,
description: "A function to add a tool result.",
},
{
name: "subscribe",
type: "(callback: () => void) => Unsubscribe",
required: true,
description: "A function to subscribe to updates.",
},
{
name: "registerModelConfigProvider",
type: "(provider: ModelConfigProvider) => Unsubscribe",
required: true,
description:
"A function to register a model config provider.",
},
],
},
],
},
]}
/>
);
};

0 comments on commit 392b728

Please sign in to comment.