-
Notifications
You must be signed in to change notification settings - Fork 2
Add Action Gateway TypeScript SDK surface #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tgillam-do
wants to merge
10
commits into
main
Choose a base branch
from
tgillam/action-gateway-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8b72840
Add Action Gateway TypeScript SDK surface
tgillam-do c4e2e97
Align Action Gateway SDK with backend APIs
tgillam-do e33b1a2
Document Action Gateway session controls
tgillam-do c6fff12
Generate Action Gateway public API clients
tgillam-do 0688dc4
Generate prefixed Action Gateway API clients
tgillam-do 2fb6efb
Add generated Action Gateway API examples
tgillam-do 2e82e2f
Test generated Action Gateway session delegation
tgillam-do 58f0db6
Delete docs/action-gateway.md
tgillam-do 4f797fd
Address Action Gateway PR review feedback
bbatha 038a217
Merge remote-tracking branch 'origin/main' into tgillam/action-gatewa…
SSharma-10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,43 @@ | ||
| # Action Gateway | ||
|
|
||
| The TypeScript SDK uses a session-first Action Gateway flow. Create a session | ||
| on the DigitalOcean public API, then discover or invoke tools through | ||
| `actions.do-ai.run` with the session and actor headers managed by the SDK. | ||
|
|
||
| ```ts | ||
| import { ActionGatewayClient } from "@digitalocean/dots/action_gateway"; | ||
|
|
||
| const gateway = new ActionGatewayClient({ | ||
| apiKey: process.env.DIGITALOCEAN_TOKEN!, | ||
| }); | ||
| const session = await gateway.session.create({ actorId: "end-user-123" }); | ||
| ``` | ||
|
|
||
| Session creation sends `actor_id`, `name`, and `policy_json` to | ||
| `POST /v2/action-gateway/sessions`. Gateway REST requests use the bare session | ||
| UUID in `X-Session-Id` and the actor in `X-Actor-Id`. | ||
|
|
||
| ## Toolbelts | ||
|
|
||
| Toolbelts are public DigitalOcean API resources, so CRUD operations are | ||
| generated from the public OpenAPI specification under `gateway.toolbelts`. | ||
| `createToolbelt` is the Action Gateway convenience wrapper: | ||
|
|
||
| ```ts | ||
| const toolbelt = await gateway.createToolbelt({ | ||
| name: "search-toolbelt", | ||
| tools: ["exa_web_search", "exa_web_fetch"], | ||
| }); | ||
|
|
||
| const session = await gateway.session.create({ | ||
| actorId: "end-user-123", | ||
| permissions: { | ||
| defaultAction: "ask", | ||
| rules: [{ tool: `toolbelt:${toolbelt.ref}`, action: "allow" }], | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| See `examples/action-gateway/` for Chat Completions, Messages, Responses, | ||
| direct tool and code execution, asynchronous usage, toolbelt creation, and | ||
| toolbelt policy examples. |
This file contains hidden or 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,13 @@ | ||
| import { ActionGatewayClient } from "../../src/action-gateway/index.js"; | ||
|
|
||
| const gateway = new ActionGatewayClient({ | ||
| apiKey: process.env.DIGITALOCEAN_TOKEN!, | ||
| }); | ||
| const session = await gateway.session.create({ actorId: "end-user-123" }); | ||
|
|
||
| const [tools, catalog] = await Promise.all([ | ||
| session.tools(), | ||
| session.toolsOperations.list({ includeAll: true }), | ||
| ]); | ||
|
|
||
| console.log(`Loaded ${tools.length} model tools and ${catalog.length} catalog tools.`); |
This file contains hidden or 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,27 @@ | ||
| import { Client } from "../../src/inference-gen/inference.js"; | ||
| import { ActionGatewayClient } from "../../src/action-gateway/index.js"; | ||
|
|
||
| const apiKey = process.env.DIGITALOCEAN_TOKEN!; | ||
| const inference = new Client({ apiKey }); | ||
| const gateway = new ActionGatewayClient({ apiKey }); | ||
| const session = await gateway.session.create({ actorId: "end-user-123" }); | ||
|
|
||
| const messages: Record<string, unknown>[] = [{ | ||
| role: "user", | ||
| content: "Find the latest DigitalOcean news and summarize it.", | ||
| }]; | ||
|
|
||
| while (true) { | ||
| const response = await inference.chat.completions.create({ | ||
| model: "llama3.3-70b-instruct", | ||
| messages, | ||
| tools: await session.tools(), | ||
| }); | ||
| const message = response.choices[0].message; | ||
| messages.push(message); | ||
| if (!message.tool_calls?.length) { | ||
| console.log(message.content); | ||
| break; | ||
| } | ||
| messages.push(...await session.handleToolCalls(response)); | ||
| } |
This file contains hidden or 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,24 @@ | ||
| import { ActionGatewayClient } from "../../src/action-gateway/index.js"; | ||
|
|
||
| const gateway = new ActionGatewayClient({ | ||
| apiKey: process.env.DIGITALOCEAN_TOKEN!, | ||
| }); | ||
|
|
||
| const toolbelt = await gateway.createToolbelt({ | ||
| name: "search-toolbelt", | ||
| tools: ["exa_web_search", "exa_web_fetch"], | ||
| }); | ||
|
|
||
| console.log(toolbelt.ref); // search-toolbelt@1 | ||
|
|
||
| // The base CRUD surface is generated from the public OpenAPI specification. | ||
| await gateway.toolbelts.get({ queryParameters: { status: "active" } }); | ||
| await gateway.toolbelts.byName("search-toolbelt").get({ | ||
| queryParameters: { version: "1" }, | ||
| }); | ||
| await gateway.toolbelts.byName("search-toolbelt").tools.add.post({ | ||
| tools: ["jira_create_issue"], | ||
| }); | ||
| await gateway.toolbelts.byName("search-toolbelt").tools.remove.post({ | ||
| tools: ["exa_web_fetch"], | ||
| }); |
This file contains hidden or 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 { ActionGatewayClient } from "../../src/action-gateway/index.js"; | ||
|
|
||
| const gateway = new ActionGatewayClient({ | ||
| apiKey: process.env.DIGITALOCEAN_TOKEN!, | ||
| }); | ||
| const session = await gateway.session.create({ actorId: "end-user-123" }); | ||
|
|
||
| const search = await session.toolsOperations.search("search the web for DigitalOcean news"); | ||
| const result = await session.toolsOperations.invokeOne("exa_web_search", { | ||
| query: "DigitalOcean news", | ||
| max_results: 5, | ||
| }); | ||
| const code = await session.code.execute("print(sum(range(10)))"); | ||
|
|
||
| console.dir({ search, result, code }, { depth: null }); |
This file contains hidden or 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,22 @@ | ||
| import { Client } from "../../src/inference-gen/inference.js"; | ||
| import { | ||
| ActionGatewayClient, | ||
| MessagesProvider, | ||
| } from "../../src/action-gateway/index.js"; | ||
|
|
||
| const apiKey = process.env.DIGITALOCEAN_TOKEN!; | ||
| const inference = new Client({ apiKey }); | ||
| const gateway = new ActionGatewayClient({ | ||
| apiKey, | ||
| provider: new MessagesProvider(), | ||
| }); | ||
| const session = await gateway.session.create({ actorId: "end-user-123" }); | ||
|
|
||
| const response = await inference.messages.create({ | ||
| model: "anthropic-claude-sonnet-4", | ||
| max_tokens: 1024, | ||
| messages: [{ role: "user", content: "Find the latest DigitalOcean news." }], | ||
| tools: await session.tools(), | ||
| }); | ||
|
|
||
| console.dir(await session.handleToolCalls(response), { depth: null }); |
This file contains hidden or 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 @@ | ||
| import { Client } from "../../src/inference-gen/inference.js"; | ||
| import { | ||
| ActionGatewayClient, | ||
| ResponsesProvider, | ||
| } from "../../src/action-gateway/index.js"; | ||
|
|
||
| const apiKey = process.env.DIGITALOCEAN_TOKEN!; | ||
| const inference = new Client({ apiKey }); | ||
| const gateway = new ActionGatewayClient({ | ||
| apiKey, | ||
| provider: new ResponsesProvider(), | ||
| }); | ||
| const session = await gateway.session.create({ actorId: "end-user-123" }); | ||
|
|
||
| const response = await inference.responses.create({ | ||
| model: "openai-gpt-4o", | ||
| input: "What DigitalOcean Droplet sizes are available in NYC3?", | ||
| tools: await session.tools(), | ||
| }); | ||
|
|
||
| console.dir(await session.handleToolCalls(response), { depth: null }); |
This file contains hidden or 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 @@ | ||
| import { ActionGatewayClient } from "../../src/action-gateway/index.js"; | ||
|
|
||
| const gateway = new ActionGatewayClient({ | ||
| apiKey: process.env.DIGITALOCEAN_TOKEN!, | ||
| }); | ||
| const toolbelt = await gateway.createToolbelt({ | ||
| name: "search-toolbelt", | ||
| tools: ["exa_web_search", "exa_web_fetch"], | ||
| }); | ||
|
|
||
| const session = await gateway.session.create({ | ||
| actorId: "end-user-123", | ||
| permissions: { | ||
| defaultAction: "ask", | ||
| rules: [{ tool: `toolbelt:${toolbelt.ref}`, action: "allow" }], | ||
| }, | ||
| }); | ||
|
|
||
| console.log(session.url); |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.