Loading...
Loading...
Integrates a Flows/Dune app with the Fusion built-in PAIA agent panel using @cognite/app-sdk. Use this skill whenever a developer wants to: open the agent panel from their app, send the agent a contextual message, let the agent read app state (resources), or let the agent call actions in the app. Triggers: "fusion agent", "PAIA", "agent panel", "sendAgentMessage", "sendAgentLayoutMode", "agent server", "registerAgentServer", "connectToHostApp", "agent integration", "agent sidebar", "app-sdk agent". Always use this skill instead of manually writing agent integration code — it sets up the correct lifecycle, graceful fallback, and recommended file structure.
npx skill4agent add cognitedata/builder-skills integrate-fusion-agent@cognite/app-sdkpackage.json@cognite/app-sdksrc/App.tsx@cognite/app-sdkpackage.jsonpnpm add @cognite/app-sdk # or npm/yarn depending on the app0.3.1HostAppAPIvite dev// src/hooks/useHostApp.ts
import { useState, useEffect } from 'react';
import { connectToHostApp, type HostAppAPI } from '@cognite/app-sdk';
export function useHostApp(): HostAppAPI | null {
const [api, setApi] = useState<HostAppAPI | null>(null);
useEffect(() => {
connectToHostApp({ applicationName: 'my-app' })
.then(({ api: resolvedApi }) => {
// IMPORTANT: use the updater form here. Comlink proxies are callable
// objects, so setApi(proxy) causes React to invoke the proxy as a
// state-updater function — storing a Promise instead of the proxy.
// setApi(() => proxy) returns the proxy as the new state value.
setApi(() => resolvedApi);
})
.catch(() => {
// Running outside Fusion — agent features disabled, no-op
});
}, []);
return api;
}useHostApp()apiapinullapi.sendAgentLayoutModeimport { type AgentLayoutPayload } from '@cognite/app-sdk';
// Open as sidebar (most common)
await api.sendAgentLayoutMode({ mode: 'sidebar' });
// Other modes
await api.sendAgentLayoutMode({ mode: 'fullscreen' });
await api.sendAgentLayoutMode({ mode: 'closed' });api{api && (
<button onClick={() => api.sendAgentLayoutMode({ mode: 'sidebar' })}>
Open Assistant
</button>
)}sendAgentMessagesendAgentLayoutMode// Open sidebar then inject context
await api.sendAgentLayoutMode({ mode: 'sidebar' });
await api.sendAgentMessage({
message: `Analyse the schedule for "${itemName}" and suggest how to reduce total duration.`,
newSession: true, // clears previous conversation — appropriate for contextual entry points
});newSession: truesrc/features/agent/
agentActions.ts — pure factory: (deps) => Action[]
agentResources.ts — pure factory: (deps) => Resource[]
useAgentServer.ts — useEffect lifecycle hook; calls the factories and registersdescription// src/features/agent/agentResources.ts
import { createAgentResource } from '@cognite/app-sdk';
import type { StorageService } from '../storage/StorageService';
export function buildAgentResources(storage: StorageService) {
return [
createAgentResource({
uri: 'my-app://current-state',
name: 'Current application state',
description:
'The current list of items visible in the app, their statuses, and any active filters. Read this before answering questions about what the user is looking at.',
async read() {
const data = storage.getAll();
return [{ type: 'json', data }];
},
}),
];
}read(){ type: 'json', data: unknown }{ type: 'text', text: string }snake_case.describe()// src/features/agent/agentActions.ts
import { createAgentAction } from '@cognite/app-sdk';
import { z } from 'zod';
import type { DataService } from '../data/DataService';
export function buildAgentActions(dataService: DataService) {
return [
createAgentAction({
name: 'get_item_details',
description: 'Retrieve full details for a specific item by ID. Returns all fields including history.',
parameters: z.object({
item_id: z.string().describe('The ID of the item to retrieve'),
}),
async handler({ item_id }) {
const item = await dataService.getItem(item_id);
return { content: [{ type: 'json', data: item }] };
},
}),
];
}descriptioncreateAgentAction({
name: 'update_item_status',
description:
'Update the status of an item. Call this ONLY when the user has explicitly approved the change. The UI updates immediately.',
parameters: z.object({
item_id: z.string().describe('The item to update'),
status: z.enum(['active', 'closed', 'pending']).describe('The new status'),
}),
async handler({ item_id, status }) {
storage.updateStatus(item_id, status);
return { content: [{ type: 'json', data: { success: true } }] };
},
})// src/features/agent/useAgentServer.ts
import { useEffect } from 'react';
import { createAgentServer, registerAgentServer, type HostAppAPI } from '@cognite/app-sdk';
import { buildAgentActions } from './agentActions';
import { buildAgentResources } from './agentResources';
import { useStorageService } from '../storage/StorageServiceContext';
import { useDataService } from '../data/DataServiceContext';
export function useAgentServer(api: HostAppAPI | null): void {
const storage = useStorageService();
const dataService = useDataService();
useEffect(() => {
if (!api) return;
const server = createAgentServer({
uri: 'my-app', // namespaced by Fusion with instance ID — no need to be globally unique
actions: buildAgentActions(dataService),
resources: buildAgentResources(storage),
});
void registerAgentServer(api, server).catch((err: unknown) => {
console.warn('[agent] registerAgentServer failed:', err);
});
return () => {
void api.unregisterAgentServer('my-app').catch((err: unknown) => {
console.warn('[agent] unregisterAgentServer failed:', err);
});
};
}, [api, storage, dataService]);
}useAgentServer(api)apiuseHostApp()apiuseAgentServer// src/App.tsx
function App() {
const api = useHostApp();
useAgentServer(api); // registers resources + actions when api is ready
return (
<AppLayout>
<MainContent />
{api && (
<ToolbarButton onClick={() => api.sendAgentLayoutMode({ mode: 'sidebar' })}>
Open Assistant
</ToolbarButton>
)}
</AppLayout>
);
}| Environment | | Effect |
|---|---|---|
| Inside Fusion | Resolves with | All features work |
Standalone | Rejects | Agent features silently disabled |
useHostAppbuildAgentActionsbuildAgentResources// agentActions.test.ts
const mockDataService = { getItem: vi.fn().mockResolvedValue({ id: '1', name: 'Test' }) };
const [getItemAction] = buildAgentActions(mockDataService);
const result = await getItemAction.handler({ item_id: '1' });
expect(result.content[0].data).toEqual({ id: '1', name: 'Test' });setApi(resolvedApi)useStatefn(prevState)setApi(proxy)apiapi.sendAgentLayoutMode(...)typeof api.sendAgentLayoutModesetApi(() => resolvedApi)typeof proxy.method === 'function'true'function'typeoftypeoftry/catch.catch()@cognite/app-sdk@0.3.1+useHostAppsetApi(() => resolvedApi)setApi(resolvedApi)useHostAppapiapiuseAgentServerregisterAgentServerunregisterAgentServer.catch()descriptionnamesnake_casedescription