-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle tool calls in SDK and playground
We have a task to allow users give a response on one or more tool calls that the AI can request in our playground. The thing is that the work necessary to make this work is also necessary for making tool call work on our SDK so this PR is a spike to have a picture of what are the moving parts necessary to make this happen
- Loading branch information
1 parent
b3f233b
commit 0bae63b
Showing
48 changed files
with
1,562 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...uid]/documents/[documentUuid]/_components/DocumentEditor/Editor/Playground/useMessages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { | ||
ToolPart, | ||
ToolRequest, | ||
PromptlVersion, | ||
VersionedMessage, | ||
extractToolContents, | ||
} from '@latitude-data/web-ui' | ||
import { Message as CompilerMessage } from '@latitude-data/compiler' | ||
import { useCallback, useState } from 'react' | ||
|
||
function isToolRequest(part: ToolPart): part is ToolRequest { | ||
return 'toolCallId' in part | ||
} | ||
|
||
function getUnrespondedToolRequests<V extends PromptlVersion>({ | ||
version: _, | ||
messages, | ||
}: { | ||
version: V | ||
messages: VersionedMessage<V>[] | ||
}) { | ||
// FIXME: Kill compiler please. I made this module compatible with | ||
// both old compiler and promptl. But because everything is typed with | ||
// old compiler prompts in promptl version are also formatted as old compiler | ||
const parts = extractToolContents({ | ||
version: 0, | ||
messages: messages as VersionedMessage<0>[], | ||
}) | ||
const toolRequestIds = new Set<string>() | ||
const toolResponses = new Set<string>() | ||
|
||
parts.forEach((part) => { | ||
if (isToolRequest(part)) { | ||
toolRequestIds.add(part.toolCallId) | ||
} else { | ||
toolResponses.add(part.id) | ||
} | ||
}) | ||
|
||
return parts.filter( | ||
(part): part is ToolRequest => | ||
isToolRequest(part) && !toolResponses.has(part.toolCallId), | ||
) | ||
} | ||
|
||
type Props<V extends PromptlVersion> = { version: V } | ||
|
||
export function useMessages<V extends PromptlVersion>({ version }: Props<V>) { | ||
const [messages, setMessages] = useState<VersionedMessage<V>[]>([]) | ||
const [unresponedToolCalls, setUnresponedToolCalls] = useState<ToolRequest[]>( | ||
[], | ||
) | ||
// FIXME: Kill compiler please | ||
// every where we have old compiler types. To avoid Typescript crying we | ||
// allow only compiler messages and then transform them to versioned messages | ||
const addMessages = useCallback( | ||
(m: CompilerMessage[]) => { | ||
const msg = m as VersionedMessage<V>[] | ||
setMessages((prevMessages) => { | ||
const newMessages = prevMessages.concat(msg) | ||
setUnresponedToolCalls( | ||
getUnrespondedToolRequests({ version, messages: newMessages }), | ||
) | ||
return newMessages | ||
}) | ||
}, | ||
[version], | ||
) | ||
|
||
return { | ||
addMessages, | ||
messages: messages as CompilerMessage[], | ||
unresponedToolCalls, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.