Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/jotai-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"build": "bunchee",
"dev": "bunchee --watch",
"lint": "prettier --c .",
"test": "echo \"Error: no test specified\" && exit 1",
"ui-test": "vitest --config vitest.ui.config.ts"
"test": "vitest"
},
"keywords": [
"ai",
Expand Down
48 changes: 7 additions & 41 deletions packages/jotai-ai/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { Message } from '@ai-sdk/ui-utils';
import type { CoreToolMessage } from 'ai';

import { generateId as defaultGenerateId } from '@ai-sdk/ui-utils';

export { defaultGenerateId };
export { generateId } from '@ai-sdk/ui-utils';

export const isPromiseLike = (
value: unknown,
Expand All @@ -16,51 +13,20 @@ export const isPromiseLike = (
);
};

// Modified from https://github.com/vercel/ai-chatbot/blob/e6806aaa542c831e874681a50df39fe26c59d25e/lib/utils.ts
export function addToolMessageToChat({
toolMessage,
messages,
}: {
toolMessage: CoreToolMessage;
messages: Message[];
}): Message[] {
return messages.map(message => {
if (message.toolInvocations) {
return {
...message,
toolInvocations: message.toolInvocations.map(toolInvocation => {
const toolResult = toolMessage.content.find(
tool => tool.toolCallId === toolInvocation.toolCallId,
);

if (toolResult) {
return {
...toolInvocation,
state: 'result',
result: toolResult.result,
};
}

return toolInvocation;
}),
};
}

return message;
});
}

/**
Check if the message is an assistant message with completed tool calls.
The message must have at least one tool invocation and all tool invocations
must have a result.
*/
export function isAssistantMessageWithCompletedToolCalls(message: Message) {
const toolInvocationParts = message.parts?.filter(
part => part.type === 'tool-invocation',
);
return (
message.role === 'assistant' &&
message.toolInvocations &&
message.toolInvocations.length > 0 &&
message.toolInvocations.every(toolInvocation => 'result' in toolInvocation)
toolInvocationParts &&
toolInvocationParts.length > 0 &&
toolInvocationParts.every(part => part.toolInvocation.state === 'result')
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ export default defineConfig({
test: {
environment: 'jsdom',
globals: true,
includeSource: [
'src/**/*.ui.test.tsx',
// 'tests/**/*.?(c|m)[jt]s?(x)',
],
includeSource: ['src/**/*.ui.test.tsx'],
},
});
Loading