Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tool results assets + misc improvements #568

Merged
merged 5 commits into from
Dec 18, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ function CronTask({ mode, initialValues }: CronTaskProps) {
.tool_router_key
: '',
},
llmOrAgentId: defaultAgentId, // TODO: backend doesnt send this atm
llmOrAgentId:
'CreateJobWithConfigAndMessage' in initialValues.action
? initialValues.action.CreateJobWithConfigAndMessage.llm_provider
: defaultAgentId,
});
}
}, [form, initialValues]);
Expand Down Expand Up @@ -248,7 +251,7 @@ function CronTask({ mode, initialValues }: CronTaskProps) {
return (
<SubpageLayout
className="max-w-4xl"
title={`${mode === 'create' ? 'Create' : 'Edit'}} Cron Task`}
title={`${mode === 'create' ? 'Create' : 'Edit'} Cron Task`}
>
<p className="text-gray-80 -mt-8 py-3 pb-6 text-center text-sm">
Schedule recurring tasks at a specified time
Expand Down Expand Up @@ -317,7 +320,7 @@ function CronTask({ mode, initialValues }: CronTaskProps) {
</span>

<div className="space-y-4">
<div className="grid grid-cols-[1fr_100px] items-center">
<div className="grid grid-cols-[1fr_auto] items-center">
<span className="text-gray-80 text-xs">AI/Agent</span>
<AIModelSelector
onValueChange={(value) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CodeLanguage,
ToolMetadata,
} from '@shinkai_network/shinkai-message-ts/api/tools/types';
import { useGetShinkaiFileProtocol } from '@shinkai_network/shinkai-node-state/v2/queries/getShinkaiFileProtocol/useGetShinkaiFileProtocol';
import {
Badge,
Button,
Expand All @@ -29,20 +30,29 @@ import {
TooltipPortal,
TooltipTrigger,
} from '@shinkai_network/shinkai-ui';
import { SendIcon } from '@shinkai_network/shinkai-ui/assets';
import {
fileIconMap,
FileTypeIcon,
SendIcon,
} from '@shinkai_network/shinkai-ui/assets';
import { cn } from '@shinkai_network/shinkai-ui/utils';
import { save } from '@tauri-apps/plugin-dialog';
import * as fs from '@tauri-apps/plugin-fs';
import { BaseDirectory } from '@tauri-apps/plugin-fs';
import { AnimatePresence, motion } from 'framer-motion';
import {
ArrowUpRight,
Loader2,
LucideArrowLeft,
Paperclip,
Play,
Redo2Icon,
Save,
Undo2Icon,
} from 'lucide-react';
import { useEffect, useRef, useState } from 'react';
import { Link, To } from 'react-router-dom';
import { toast } from 'sonner';

import { useAuth } from '../../../store/auth';
import { AIModelSelector } from '../../chat/chat-action-bar/ai-update-selection-action-bar';
Expand Down Expand Up @@ -112,6 +122,7 @@ function PlaygroundToolEditor({
metadataEditorRef,
executeToolCodeQuery,
toolResult,
toolResultFiles,
isDirtyCodeEditor,
setIsDirtyCodeEditor,
forceGenerateMetadata,
Expand Down Expand Up @@ -703,15 +714,33 @@ function PlaygroundToolEditor({
<div ref={toolResultBoxRef}>
{executeToolCodeQuery.isSuccess &&
toolResult && (
<ToolCodeEditor
language="json"
readOnly
value={JSON.stringify(
toolResult,
null,
2,
<div className="space-y-3 py-3">
{toolResultFiles.length > 0 && (
<div className="inline-flex items-center gap-4">
<h1>Generated Files</h1>
<div className="flex flex-wrap gap-2">
{toolResultFiles?.map(
(file) => (
<ToolResultFileCard
filePath={file}
key={file}
/>
),
)}
</div>
</div>
)}
/>

<ToolCodeEditor
language="json"
readOnly
value={JSON.stringify(
toolResult,
null,
2,
)}
/>
</div>
)}
</div>
</motion.div>
Expand Down Expand Up @@ -813,3 +842,70 @@ function PlaygroundToolEditor({
}

export default PlaygroundToolEditor;

function ToolResultFileCard({ filePath }: { filePath: string }) {
const auth = useAuth((state) => state.auth);
const { refetch } = useGetShinkaiFileProtocol(
{
nodeAddress: auth?.node_address ?? '',
token: auth?.api_v2_key ?? '',
file: filePath,
},
{
enabled: false,
},
);

const fileNameBase = filePath.split('/')?.at(-1) ?? 'untitled_tool';
const fileExtension = fileNameBase.split('.')?.at(-1) ?? '';

return (
<Button
className="h-[30px] gap-1.5 rounded-lg text-xs"
onClick={async () => {
const response = await refetch();
const file = new Blob([response.data ?? ''], {
type: 'application/octet-stream',
});

const arrayBuffer = await file.arrayBuffer();
const content = new Uint8Array(arrayBuffer);

const savePath = await save({
defaultPath: `${fileNameBase}.${fileExtension}`,
filters: [
{
name: 'File',
extensions: [fileExtension],
},
],
});

if (!savePath) {
toast.info('File saving cancelled');
return;
}

await fs.writeFile(savePath, content, {
baseDir: BaseDirectory.Download,
});

toast.success(`${fileNameBase} downloaded successfully`);
}}
size="auto"
variant="outline"
>
<div className="flex shrink-0 items-center justify-center">
{fileExtension && fileIconMap[fileExtension] ? (
<FileTypeIcon
className="text-gray-80 h-[18px] w-[18px] shrink-0"
type={fileExtension}
/>
) : (
<Paperclip className="text-gray-80 h-3.5 w-3.5 shrink-0" />
)}
</div>
<div className="text-left text-xs">{filePath.split('/')?.at(-1)}</div>
</Button>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,14 @@ export const useToolCode = ({
}, 0);
};

const { __created_files__: toolResultFiles, ...toolResultWithoutFiles } =
(toolResult || {
__created_files__: [],
}) as {
__created_files__: string[];
[key: string]: any;
};

return {
chatInboxId,
toolCode,
Expand All @@ -437,7 +445,8 @@ export const useToolCode = ({
metadataEditorRef,
createToolCode,
executeToolCodeQuery,
toolResult,
toolResult: toolResultWithoutFiles,
toolResultFiles: toolResultFiles,
forceGenerateMetadata,
isDirtyCodeEditor,
setIsDirtyCodeEditor,
Expand Down
11 changes: 11 additions & 0 deletions apps/shinkai-desktop/src/components/playground-tool/utils/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ export function detectLanguage(code: string): string {
if (isTypeScript) return 'TypeScript';
return 'Unknown';
}

export function getLanguage(language: string): CodeLanguage {
switch (language) {
case 'python':
return CodeLanguage.Python;
case 'typeScript':
return CodeLanguage.Typescript;
default:
return CodeLanguage.Typescript;
}
}
5 changes: 2 additions & 3 deletions apps/shinkai-desktop/src/pages/edit-tool.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import validator from '@rjsf/validator-ajv8';
import { CodeLanguage } from '@shinkai_network/shinkai-message-ts/api/tools/types';
import { buildInboxIdFromJobId } from '@shinkai_network/shinkai-message-ts/utils/inbox_name_handler';
import { useGetPlaygroundTool } from '@shinkai_network/shinkai-node-state/v2/queries/getPlaygroundTool/useGetPlaygroundTool';
import { useMemo } from 'react';
import { useParams } from 'react-router-dom';

import PlaygroundToolEditor from '../components/playground-tool/components/tool-playground';
import { detectLanguage } from '../components/playground-tool/utils/code';
import { getLanguage } from '../components/playground-tool/utils/code';
import { useAuth } from '../store/auth';

function EditToolPage() {
Expand Down Expand Up @@ -74,7 +73,7 @@ function EditToolPage() {
return (
<PlaygroundToolEditor
createToolCodeFormInitialValues={{
language: detectLanguage(playgroundTool?.code ?? '') as CodeLanguage,
language: getLanguage(playgroundTool?.language ?? ''),
}}
initialChatInboxId={chatInboxId}
mode="edit"
Expand Down
Loading