-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor tools as retriever / actions
- Loading branch information
1 parent
11de742
commit a2839e4
Showing
6 changed files
with
231 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,88 @@ | ||
import { State } from "./state"; | ||
import { Tool } from "./tool"; | ||
|
||
export const getSelectedText = (state: State): string => { | ||
export const getSelectedText = (state: State, parameters: {}): string => { | ||
if (!state.currentSelection) { | ||
return ""; | ||
} | ||
return state.currentSelection.toString(); | ||
}; | ||
|
||
export async function getCalendarEvents( | ||
state: State, | ||
parameters: { token: string }, | ||
) { | ||
try { | ||
// API URL to fetch calendar events | ||
const url = | ||
"https://www.googleapis.com/calendar/v3/calendars/primary/events?maxResults=10&orderBy=startTime&singleEvents=true"; | ||
|
||
// Fetch the events from the user's primary Google Calendar | ||
const response = await fetch(url, { | ||
method: "GET", | ||
headers: { | ||
Authorization: `Bearer ${parameters.token}`, | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error("Failed to fetch calendar events"); | ||
} | ||
|
||
const events = await response.json(); | ||
|
||
// Process and display events in popup (this is a basic example) | ||
if (events.items) { | ||
events.items.forEach((event: any) => { | ||
const eventElement = document.createElement("div"); | ||
eventElement.textContent = `${event.summary} - ${event.start.dateTime || event.start.date}`; | ||
document.body.appendChild(eventElement); | ||
}); | ||
} else { | ||
document.body.textContent = "No upcoming events found."; | ||
} | ||
} catch (error) { | ||
console.error("Error fetching calendar events:", error); | ||
document.body.textContent = "Error fetching events"; | ||
} | ||
} | ||
|
||
export const retrievers: Record<string, Tool> = { | ||
getSelectedText: { | ||
name: "getSelectedText", | ||
displayName: "Get Selected Text", | ||
description: | ||
"Get the user's current selected text content on the document.", | ||
schema: { | ||
type: "function", | ||
function: { | ||
name: "getSelectedText", | ||
description: | ||
"getSelectedText() -> str - Get the user's current selected text content on the document, no parameter is needed.\\n\\n Returns:\\n str: The user's current selected text content on the document.", | ||
parameters: { type: "object", properties: {}, required: [] }, | ||
}, | ||
}, | ||
implementation: getSelectedText, | ||
}, | ||
getCalendarEvents: { | ||
name: "getGoogleCalendarEvents", | ||
displayName: "Get Google Calendar Events", | ||
description: | ||
"Fetch the user's upcoming events from their primary Google Calendar.", | ||
schema: { | ||
type: "function", | ||
function: { | ||
name: "getGoogleCalendarEvents", | ||
description: | ||
"getGoogleCalendarEvents(token: string) - Fetches up to 10 upcoming events from the user's Google Calendar.\n\n Returns:\n Array: List of upcoming events with event details.", | ||
parameters: { | ||
type: "object", | ||
properties: {}, | ||
required: [], | ||
}, | ||
}, | ||
}, | ||
implementation: getCalendarEvents, | ||
}, | ||
}; |
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 |
---|---|---|
@@ -1,33 +1,18 @@ | ||
export enum Scope { | ||
DOM = "DOM", | ||
} | ||
|
||
export class State { | ||
public currentSelection: Selection | undefined; | ||
private scopeRegistration = { | ||
DOM: this.registerDOM, | ||
}; | ||
|
||
constructor(scopes: Scope[]) { | ||
this.registerScopes(scopes); | ||
} | ||
|
||
private registerScopes(scopes: Scope[]) { | ||
scopes.forEach((scope) => { | ||
this.scopeRegistration[scope](); | ||
}); | ||
} | ||
|
||
private registerDOM() { | ||
document.addEventListener("selectionchange", (): void => { | ||
const selection = window.getSelection(); | ||
if ( | ||
selection && | ||
typeof selection.rangeCount !== "undefined" && | ||
selection.rangeCount > 0 | ||
) { | ||
this.currentSelection = selection; | ||
} | ||
}); | ||
constructor() { | ||
if (document) { | ||
document.addEventListener("selectionchange", (): void => { | ||
const selection = window.getSelection(); | ||
if ( | ||
selection && | ||
typeof selection.rangeCount !== "undefined" && | ||
selection.rangeCount > 0 | ||
) { | ||
this.currentSelection = selection; | ||
} | ||
}); | ||
} | ||
} | ||
} |
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