@inferencesh/sdk 0.5.2 → 0.5.3

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.
@@ -48,7 +48,8 @@ export function createActions(ctx) {
48
48
  if (!handler) {
49
49
  console.warn(`[AgentSDK] No handler for client tool: ${functionName}`);
50
50
  api.submitToolResult(client, invocation.id, JSON.stringify({
51
- error: `No handler registered for tool: ${functionName}`
51
+ status: 'not_available',
52
+ message: `Client tool "${functionName}" is not available in this environment`,
52
53
  }));
53
54
  continue;
54
55
  }
@@ -77,10 +78,19 @@ export function createActions(ctx) {
77
78
  setStreamManager(undefined);
78
79
  dispatch({ type: 'SET_STATUS', payload: 'connecting' });
79
80
  callbacks.onStatusChange?.('connecting');
80
- // Fetch initial chat
81
- const chat = await api.fetchChat(client, id);
82
- if (chat) {
83
- setChat(chat);
81
+ try {
82
+ // Fetch initial chat
83
+ const chat = await api.fetchChat(client, id);
84
+ if (chat) {
85
+ setChat(chat);
86
+ }
87
+ }
88
+ catch (error) {
89
+ console.error('[AgentSDK] Failed to fetch chat:', error);
90
+ dispatch({ type: 'SET_STATUS', payload: 'idle' });
91
+ dispatch({ type: 'SET_IS_GENERATING', payload: false });
92
+ callbacks.onStatusChange?.('idle');
93
+ return;
84
94
  }
85
95
  // Single unified stream with TypedEvents (both Chat and ChatMessage events)
86
96
  const manager = new StreamManager({
@@ -96,6 +106,17 @@ export function createActions(ctx) {
96
106
  dispatch({ type: 'SET_STATUS', payload: 'streaming' });
97
107
  callbacks.onStatusChange?.('streaming');
98
108
  },
109
+ onStop: () => {
110
+ // Only reset if this is an unexpected stop (stream died, max reconnects exhausted).
111
+ // If stopStream() was called intentionally, it clears the manager ref first,
112
+ // so getStreamManager() will be undefined and we skip the duplicate dispatch.
113
+ if (getStreamManager()) {
114
+ setStreamManager(undefined);
115
+ dispatch({ type: 'SET_STATUS', payload: 'idle' });
116
+ dispatch({ type: 'SET_IS_GENERATING', payload: false });
117
+ callbacks.onStatusChange?.('idle');
118
+ }
119
+ },
99
120
  });
100
121
  // Listen for Chat object updates (status changes)
101
122
  manager.addEventListener('chats', (chatData) => {
@@ -110,10 +131,11 @@ export function createActions(ctx) {
110
131
  };
111
132
  const stopStream = () => {
112
133
  const manager = getStreamManager();
134
+ // Clear ref first so onStop callback (from manager.stop) is a no-op
135
+ setStreamManager(undefined);
113
136
  if (manager) {
114
137
  manager.stop();
115
138
  }
116
- setStreamManager(undefined);
117
139
  dispatch({ type: 'SET_STATUS', payload: 'idle' });
118
140
  dispatch({ type: 'SET_IS_GENERATING', payload: false });
119
141
  callbacks.onStatusChange?.('idle');
@@ -150,6 +172,12 @@ export function createActions(ctx) {
150
172
  streamChat(newChatId);
151
173
  }
152
174
  }
175
+ else {
176
+ // API returned no result — reset status so we don't get stuck
177
+ dispatch({ type: 'SET_STATUS', payload: 'idle' });
178
+ dispatch({ type: 'SET_IS_GENERATING', payload: false });
179
+ callbacks.onStatusChange?.('idle');
180
+ }
153
181
  }
154
182
  catch (error) {
155
183
  console.error('[AgentSDK] Failed to send message:', error);
@@ -27,7 +27,7 @@ import type { AgentChatProviderProps } from './types';
27
27
  * }
28
28
  * ```
29
29
  */
30
- export declare function AgentChatProvider({ client, agentConfig, chatId, onChatCreated, onStatusChange, onError, children, }: AgentChatProviderProps): import("react/jsx-runtime").JSX.Element;
30
+ export declare function AgentChatProvider({ client, agentConfig, chatId, clientToolHandlers: extraHandlers, onChatCreated, onStatusChange, onError, children, }: AgentChatProviderProps): import("react/jsx-runtime").JSX.Element;
31
31
  export declare namespace AgentChatProvider {
32
32
  var displayName: string;
33
33
  }
@@ -9,6 +9,14 @@ import { useReducer, useRef, useEffect, useMemo } from 'react';
9
9
  import { AgentChatContext } from './context';
10
10
  import { chatReducer, initialState } from './reducer';
11
11
  import { createActions, getClientToolHandlers } from './actions';
12
+ function mergeHandlers(base, extra) {
13
+ if (!extra || extra.size === 0)
14
+ return base;
15
+ const merged = new Map(base);
16
+ for (const [k, v] of extra)
17
+ merged.set(k, v);
18
+ return merged;
19
+ }
12
20
  /**
13
21
  * AgentChatProvider - Provides chat state and actions to children
14
22
  *
@@ -31,20 +39,20 @@ import { createActions, getClientToolHandlers } from './actions';
31
39
  * }
32
40
  * ```
33
41
  */
34
- export function AgentChatProvider({ client, agentConfig, chatId, onChatCreated, onStatusChange, onError, children, }) {
42
+ export function AgentChatProvider({ client, agentConfig, chatId, clientToolHandlers: extraHandlers, onChatCreated, onStatusChange, onError, children, }) {
35
43
  // Core state via useReducer
36
44
  const [state, dispatch] = useReducer(chatReducer, initialState);
37
45
  // Refs for mutable values that actions need access to
38
46
  const configRef = useRef(agentConfig);
39
47
  const chatIdRef = useRef(chatId ?? null);
40
48
  const streamManagerRef = useRef(undefined);
41
- const clientToolHandlersRef = useRef(getClientToolHandlers(agentConfig));
49
+ const clientToolHandlersRef = useRef(mergeHandlers(getClientToolHandlers(agentConfig), extraHandlers));
42
50
  const callbacksRef = useRef({ onChatCreated, onStatusChange, onError });
43
51
  // Keep refs in sync with props
44
52
  useEffect(() => {
45
53
  configRef.current = agentConfig;
46
- clientToolHandlersRef.current = getClientToolHandlers(agentConfig);
47
- }, [agentConfig]);
54
+ clientToolHandlersRef.current = mergeHandlers(getClientToolHandlers(agentConfig), extraHandlers);
55
+ }, [agentConfig, extraHandlers]);
48
56
  useEffect(() => {
49
57
  callbacksRef.current = { onChatCreated, onStatusChange, onError };
50
58
  }, [onChatCreated, onStatusChange, onError]);
@@ -134,6 +134,8 @@ export interface AgentChatProviderProps {
134
134
  agentConfig: AgentOptions;
135
135
  /** Optional existing chat ID to continue */
136
136
  chatId?: string;
137
+ /** Extra client tool handlers, merged with any from agentConfig. Use with TemplateAgentConfig to handle tools defined server-side. */
138
+ clientToolHandlers?: Map<string, ClientToolHandlerFn>;
137
139
  /** Callback when a new chat is created */
138
140
  onChatCreated?: (chatId: string) => void;
139
141
  /** Callback when chat status changes */
package/dist/types.d.ts CHANGED
@@ -7,6 +7,7 @@ export interface InternalToolsConfig {
7
7
  widget?: boolean;
8
8
  finish?: boolean;
9
9
  skills?: boolean;
10
+ host_context?: boolean;
10
11
  }
11
12
  /**
12
13
  * ToolType represents the type of tool (used in both AgentTool definition and ToolInvocation)
@@ -396,6 +397,16 @@ export interface ApiAgentRunRequest {
396
397
  */
397
398
  stream?: boolean;
398
399
  }
400
+ /**
401
+ * EmbedAgentRunRequest is the embed variant of ApiAgentRunRequest.
402
+ * Only template agents are supported (no ad-hoc configs).
403
+ */
404
+ export interface EmbedAgentRunRequest {
405
+ chat_id?: string;
406
+ agent: string;
407
+ input: ChatTaskInput;
408
+ stream?: boolean;
409
+ }
399
410
  export interface CreateAgentMessageRequest {
400
411
  chat_id?: string;
401
412
  agent_id?: string;
@@ -508,6 +519,26 @@ export interface CreateAgentRequest {
508
519
  */
509
520
  export interface SDKTypes {
510
521
  }
522
+ export interface SkillPublishRequest {
523
+ name: string;
524
+ description: string;
525
+ category: string;
526
+ repo_url: string;
527
+ license: string;
528
+ allowed_tools: string;
529
+ compatibility: string;
530
+ instructions: string;
531
+ files: SkillFile[];
532
+ metadata?: {
533
+ [key: string]: string;
534
+ };
535
+ /**
536
+ * Spec fields for roundtrip fidelity
537
+ */
538
+ disable_model_invocation: boolean;
539
+ user_invocable?: boolean;
540
+ context: string;
541
+ }
511
542
  export interface CheckoutCreateRequest {
512
543
  amount: number;
513
544
  success_url: string;
@@ -1745,6 +1776,16 @@ export interface InstanceEnvVar {
1745
1776
  name: string;
1746
1777
  value: string;
1747
1778
  }
1779
+ /**
1780
+ * SkillFile represents a file in the skill directory (stored as JSONB in skill_versions)
1781
+ */
1782
+ export interface SkillFile {
1783
+ path: string;
1784
+ uri?: string;
1785
+ size: number;
1786
+ hash: string;
1787
+ content?: string;
1788
+ }
1748
1789
  /**
1749
1790
  * Hardware/System related types
1750
1791
  */
@@ -2092,6 +2133,7 @@ export interface ToolInvocationDTO extends BaseModel, PermissionModelDTO {
2092
2133
  chat_message_id: string;
2093
2134
  tool_invocation_id: string;
2094
2135
  type: ToolType;
2136
+ display_name?: string;
2095
2137
  execution_id?: string;
2096
2138
  function: ToolInvocationFunction;
2097
2139
  status: ToolInvocationStatus;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inferencesh/sdk",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "Official JavaScript/TypeScript SDK for inference.sh - Run AI models with a simple API",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",