@datalayer/agent-runtimes 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +141 -22
- package/lib/components/chat/components/AgentDetails.d.ts +1 -1
- package/lib/components/chat/components/AgentDetails.js +7 -92
- package/lib/components/chat/components/Chat.d.ts +5 -1
- package/lib/components/chat/components/Chat.js +28 -19
- package/lib/components/chat/components/ContextDistribution.d.ts +47 -0
- package/lib/components/chat/components/ContextDistribution.js +146 -0
- package/lib/components/chat/components/ContextUsage.d.ts +33 -0
- package/lib/components/chat/components/ContextUsage.js +127 -0
- package/lib/components/chat/components/base/ChatBase.d.ts +5 -1
- package/lib/components/chat/components/base/ChatBase.js +40 -15
- package/lib/components/chat/components/index.d.ts +2 -0
- package/lib/components/chat/components/index.js +2 -0
- package/lib/examples/AgentSpaceFormExample.js +41 -6
- package/lib/examples/components/AgentConfiguration.d.ts +22 -0
- package/lib/examples/components/AgentConfiguration.js +37 -10
- package/lib/examples/components/Header.d.ts +0 -2
- package/lib/examples/components/Header.js +2 -16
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/runtime/index.d.ts +35 -0
- package/lib/runtime/index.js +40 -0
- package/lib/runtime/runtimeStore.d.ts +77 -0
- package/lib/runtime/runtimeStore.js +184 -0
- package/lib/runtime/types.d.ts +84 -0
- package/lib/runtime/types.js +15 -0
- package/lib/runtime/useAgentConnection.d.ts +46 -0
- package/lib/runtime/useAgentConnection.js +112 -0
- package/lib/runtime/useAgentRuntime.d.ts +94 -0
- package/lib/runtime/useAgentRuntime.js +125 -0
- package/package.json +1 -1
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025-2026 Datalayer, Inc.
|
|
3
|
+
* Distributed under the terms of the Modified BSD License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Combined hook for using a runtime with an AI agent.
|
|
7
|
+
*
|
|
8
|
+
* This is the main entry point for consumers who want a simple,
|
|
9
|
+
* all-in-one solution for cloud runtime + agent management.
|
|
10
|
+
*
|
|
11
|
+
* @module runtime/useAgentRuntime
|
|
12
|
+
*/
|
|
13
|
+
import { useEffect, useRef, useCallback } from 'react';
|
|
14
|
+
import { useRuntimeStore, useRuntime, useAgent, useRuntimeStatus, useRuntimeError, useIsLaunching, } from './runtimeStore';
|
|
15
|
+
/**
|
|
16
|
+
* Combined hook for using a runtime with an AI agent.
|
|
17
|
+
*
|
|
18
|
+
* This hook provides everything needed to:
|
|
19
|
+
* 1. Connect to an existing runtime (or launch a new one)
|
|
20
|
+
* 2. Create an AI agent on the runtime
|
|
21
|
+
*
|
|
22
|
+
* Use this in conjunction with useNotebookTools or useLexicalTools
|
|
23
|
+
* for frontend tool execution.
|
|
24
|
+
*
|
|
25
|
+
* @param options - Configuration options
|
|
26
|
+
* @returns Complete agent runtime state and controls
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```tsx
|
|
30
|
+
* // For notebooks
|
|
31
|
+
* import { useAgentRuntime } from '@datalayer/agent-runtimes/lib/runtime';
|
|
32
|
+
* import { useNotebookTools } from '@datalayer/agent-runtimes/lib/tools/adapters/agent-runtimes';
|
|
33
|
+
*
|
|
34
|
+
* function NotebookEditor({ notebookId }) {
|
|
35
|
+
* const {
|
|
36
|
+
* isReady,
|
|
37
|
+
* endpoint,
|
|
38
|
+
* error,
|
|
39
|
+
* connectToRuntime,
|
|
40
|
+
* } = useAgentRuntime({
|
|
41
|
+
* autoCreateAgent: true,
|
|
42
|
+
* agentConfig: {
|
|
43
|
+
* model: 'anthropic:claude-sonnet-4-5',
|
|
44
|
+
* systemPrompt: 'You help users with Jupyter notebooks.',
|
|
45
|
+
* },
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* // Get tools separately
|
|
49
|
+
* const tools = useNotebookTools(notebookId);
|
|
50
|
+
*
|
|
51
|
+
* // Connect when user assigns a runtime
|
|
52
|
+
* const onRuntimeAssigned = (serviceManager, podName, poolName) => {
|
|
53
|
+
* connectToRuntime({ serviceManager, podName, jupyterpoolName: poolName });
|
|
54
|
+
* };
|
|
55
|
+
*
|
|
56
|
+
* return (
|
|
57
|
+
* <>
|
|
58
|
+
* <Notebook />
|
|
59
|
+
* {isReady && (
|
|
60
|
+
* <ChatFloating endpoint={endpoint} tools={tools} />
|
|
61
|
+
* )}
|
|
62
|
+
* {error && <ErrorBanner>{error}</ErrorBanner>}
|
|
63
|
+
* </>
|
|
64
|
+
* );
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export function useAgentRuntime(options = {}) {
|
|
69
|
+
const { agentConfig, autoCreateAgent = true } = options;
|
|
70
|
+
// Get store state
|
|
71
|
+
const runtime = useRuntime();
|
|
72
|
+
const agent = useAgent();
|
|
73
|
+
const status = useRuntimeStatus();
|
|
74
|
+
const error = useRuntimeError();
|
|
75
|
+
const isLaunching = useIsLaunching();
|
|
76
|
+
// Get store actions
|
|
77
|
+
const launchRuntime = useRuntimeStore(state => state.launchRuntime);
|
|
78
|
+
const connectToRuntime = useRuntimeStore(state => state.connectToRuntime);
|
|
79
|
+
const disconnect = useRuntimeStore(state => state.disconnect);
|
|
80
|
+
const createAgentAction = useRuntimeStore(state => state.createAgent);
|
|
81
|
+
// Track if we've created the agent to prevent duplicates
|
|
82
|
+
const hasCreatedAgentRef = useRef(false);
|
|
83
|
+
const agentConfigRef = useRef(agentConfig);
|
|
84
|
+
agentConfigRef.current = agentConfig;
|
|
85
|
+
// Auto-create agent when runtime is ready
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
if (autoCreateAgent &&
|
|
88
|
+
runtime &&
|
|
89
|
+
status === 'ready' &&
|
|
90
|
+
!agent &&
|
|
91
|
+
!hasCreatedAgentRef.current) {
|
|
92
|
+
hasCreatedAgentRef.current = true;
|
|
93
|
+
createAgentAction(agentConfigRef.current).catch(err => {
|
|
94
|
+
console.error('[useAgentRuntime] Failed to auto-create agent:', err);
|
|
95
|
+
hasCreatedAgentRef.current = false;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}, [autoCreateAgent, runtime, status, agent, createAgentAction]);
|
|
99
|
+
// Reset agent creation tracking on disconnect
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
if (status === 'disconnected' || status === 'idle') {
|
|
102
|
+
hasCreatedAgentRef.current = false;
|
|
103
|
+
}
|
|
104
|
+
}, [status]);
|
|
105
|
+
// Memoized create agent function
|
|
106
|
+
const createAgent = useCallback((config) => createAgentAction(config || agentConfig), [createAgentAction, agentConfig]);
|
|
107
|
+
// Derived state
|
|
108
|
+
const isReady = status === 'ready' && !!agent?.isReady;
|
|
109
|
+
const endpoint = agent?.endpoint || null;
|
|
110
|
+
const serviceManager = runtime?.serviceManager || null;
|
|
111
|
+
return {
|
|
112
|
+
runtime,
|
|
113
|
+
agent,
|
|
114
|
+
status,
|
|
115
|
+
error,
|
|
116
|
+
isLaunching,
|
|
117
|
+
isReady,
|
|
118
|
+
endpoint,
|
|
119
|
+
serviceManager,
|
|
120
|
+
launchRuntime,
|
|
121
|
+
connectToRuntime,
|
|
122
|
+
createAgent,
|
|
123
|
+
disconnect,
|
|
124
|
+
};
|
|
125
|
+
}
|