import { IncredibleClient } from "@incredible-ai/sdk";
import type { MessageParam, Tool, ToolUseBlock } from "@anthropic-ai/sdk";
const API_KEY = "ik_your_incredible_api_key";
const USER_ID = "integration-demo-user";
const INTEGRATION_ID = "googlesheets";
const CALLBACK_BASE = "https://your-ngrok-url.ngrok-free.app";
const CALLBACK_PATH = "/oauth/google-sheets/callback";
async function main() {
const client = new IncredibleClient({ apiKey: API_KEY });
// Phase 1: Discover integrations
const integrations = await client.integrations.list();
console.log("Available integrations:", integrations);
// Phase 2: OAuth flow for Google Sheets
await ensureOAuthAuthorized(client, buildCallbackUrl());
// Phase 3: Build tools for function calling
const tools = buildGoogleSheetsTools(client);
const messages = createInitialMessages();
// Phase 4: Run agent conversation with tool calling
await runAgentConversation(client, tools, messages);
}
// Tool definitions for function calling
function buildGoogleSheetsTools(client: IncredibleClient) {
return [
{
definition: {
name: "list_incredible_integrations",
description: "Return the first ten Incredible integrations",
input_schema: { type: "object", properties: {} }
},
run: async () => {
const all = await client.integrations.list();
return all.slice(0, 10);
}
},
{
definition: {
name: "create_google_sheet",
description: "Create a new Google Sheet",
input_schema: {
type: "object",
properties: {
title: { type: "string" }
},
required: ["title"]
}
},
run: async (args) => {
return client.integrations.execute(INTEGRATION_ID, {
user_id: USER_ID,
feature_name: "GOOGLESHEETS_CREATE_GOOGLE_SHEET1",
inputs: { title: args.title }
});
}
},
{
definition: {
name: "write_google_sheets_from_json",
description: "Write data to Google Sheets from JSON",
input_schema: {
type: "object",
properties: {
spreadsheet_title: { type: "string" },
sheet_title: { type: "string" },
rows_json: { type: "string" }
},
required: ["spreadsheet_title", "sheet_title", "rows_json"]
}
},
run: async (args) => {
return client.integrations.execute(INTEGRATION_ID, {
user_id: USER_ID,
feature_name: "GOOGLESHEETS_SHEET_FROM_JSON",
inputs: {
title: args.spreadsheet_title,
sheet_name: args.sheet_title,
sheet_json: JSON.parse(args.rows_json)
}
});
}
}
];
}
// OAuth flow implementation
async function ensureOAuthAuthorized(client: IncredibleClient, callbackUrl: string) {
const connectResponse = await client.integrations.connect(INTEGRATION_ID, {
callback_url: callbackUrl,
user_id: USER_ID
});
if (connectResponse?.redirect_url) {
console.log(`Visit this URL to authorize: ${connectResponse.redirect_url}`);
// Wait for user authorization...
}
}
// Agent conversation with tool calling
async function runAgentConversation(client: IncredibleClient, tools: RegisteredTool[], messages: MessageParam[]) {
for (;;) {
const response = await client.messages.create({
model: "small-1",
max_tokens: 1024,
messages,
tools: tools.map(tool => tool.definition)
});
messages.push({ role: response.role, content: response.content });
if (response.stop_reason !== "tool_use") {
return;
}
const toolUses = extractToolUses(response);
for (const toolUse of toolUses) {
await invokeTool(tools, toolUse, messages);
}
}
}