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

enhance: add content param to create google doc tool #387

Merged
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
11 changes: 5 additions & 6 deletions google/docs/create_doc.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import os

from auth import client
from move_doc import move_doc
from update_doc import update_doc

def main():
try:
# Get the title from the DOC_TITLE environment variable or default to 'Untitled Document'
title = os.getenv('DOC_TITLE', 'Untitled Document')

doc_content = os.getenv('DOC_CONTENT')
folder_path = os.getenv('DOC_DRIVE_DIR', '').strip()

# Authenticate and build the Docs and Drive API services
docs_service = client('docs', 'v1')
drive_service = client('drive', 'v3')

# Create a new Google Doc with the specified title
document = docs_service.documents().create(body={"title": title}).execute()
Expand All @@ -20,9 +20,8 @@ def main():
document_id = document.get('documentId')
print(f"New document created with ID: {document_id}")

# Move the document to the specified folder path
folder_path = os.getenv('DOC_DRIVE_DIR', '').strip()
move_doc(drive_service, document_id, folder_path)
# Add the content to the document and move it to the specified folder
update_doc(document_id, doc_content, folder_path)

except Exception as err:
print(f"Error: {err}")
Expand Down
2 changes: 1 addition & 1 deletion google/docs/move_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ def get_or_create_folder(service, folder_path, parent_id="root"):
fields="id, parents"
).execute()

print(f"Document moved to folder: {folder_path}")
print(f"Document with ID {document_id} moved to folder: {folder_path}")
2 changes: 0 additions & 2 deletions google/docs/read_doc.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import sys
import os

from googleapiclient.http import MediaIoBaseDownload

from auth import client
from id import extract_file_id

Expand Down
32 changes: 10 additions & 22 deletions google/docs/tool.gpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ Param: doc_ref: Google Docs ID or share link of the document to read
Name: Update Google Doc
Description: Updates a Google Doc
Share Tools: Create Google Doc
Share Context: Google Docs Context, Update Google Doc Context
Share Context: Google Docs Context
Credential: ../credential
Param: doc_ref: Google Docs ID or share link of the document to read
Param: new_doc_content: Markdown formatted content to replace the existing content of the document with
Param: new_drive_dir: Optional folder path in Google Drive to move the document to after updating it. Use "/" to move the document back to the root folder.
Param: doc_ref: Google Docs ID or share link of the document to read.
Param: doc_drive_dir: Optional folder path in Google Drive to move the document to after updating it. Use "/" to move the document back to the root folder.
Param: doc_content: Markdown formatted content to replace the existing content of the document with.

#!/usr/bin/env python3 ${GPTSCRIPT_TOOL_DIR}/update_doc.py

---
Name: Create Google Doc
Description: Creates a blank Google Doc
Description: Creates a Google Doc with the specified title and optional content.
Share Context: Google Docs Context
Credential: ../credential
Param: doc_title: The title of the document to create
Param: doc_title: The title of the document to create.
Param: doc_drive_dir: Optional folder path in Google Drive to move the document to after creating it. If not provided, the document will be created in the root folder.
Param: doc_content: Optional markdown formatted content to add to the document after creating it.

#!/usr/bin/env python3 ${GPTSCRIPT_TOOL_DIR}/create_doc.py

Expand All @@ -48,25 +49,12 @@ Document names are not considered Doc IDs.
Google Docs share links match the following regex: (?:https?://(?:drive|docs)\.google\.com/(?:file/d/|document/d/|open\?id=|uc\?id=))([a-zA-Z0-9_-]{33,})
If the user does not provide a URL for the Google Doc they want to work with, ask them to provide it.
For the doc_drive_dir parameter, you can optionally specify folder paths directly, such as Haikus or Haikus/New, to move the document to a specific folder in Google Drive.
When calling the `Create Google Doc` tool, you may optionally provide the entire document as a markdown formatted string using the `doc_content` parameter.
When calling the `Update Google Doc` tool, you must provide the entire modified document as a markdown formatted string using the `doc_content` parameter. The entire document is replaced by the content of the `doc_content` argument.
For the `drive_dir` parameter, you can optionally specify folder paths directly, such as `Haikus` or `Haikus/New`, to move the document to a specific folder in Google Drive. If you do not provide a `drive_dir`, the new documents will be created in the root Google Drive folder, and updated documents will remain in their current folder.

## End of instructions for using Google Docs tools


---
Name: Update Google Doc Context
Type: context

#!sys.echo

## Instructions for using the Update Google Doc tool

When calling the Update Google Doc tool, provide the entire modified document as a markdown formatted
string using the new_doc_content parameter. The entire document is replaced by the content of the new_doc_content
argument.
For the new_drive_dir parameter, you can optionally specify folder paths directly, such as Haikus or Haikus/New, to move the document to a specific folder in Google Drive.

## End of instructions for using the Update Google Doc tool

---
!metadata:*:category
Google Docs
Expand Down
39 changes: 21 additions & 18 deletions google/docs/update_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,13 @@ def add_text_request(text, bold=False, italic=False, underline=False, link=None)

return requests


def main():
try:
doc_ref = os.getenv('DOC_REF')
new_doc_content = os.getenv('NEW_DOC_CONTENT')
new_drive_dir = os.getenv('NEW_DRIVE_DIR', '').strip() # Get the optional NEW_DRIVE_DIR

if not doc_ref:
raise ValueError('DOC_REF environment variable is missing or empty')

if not new_doc_content:
raise ValueError('NEW_DOC_CONTENT environment variable is missing or empty')

def update_doc(file_id, doc_content, drive_dir):
if doc_content:
try:
requests = markdown_to_google_doc_requests(new_doc_content)
requests = markdown_to_google_doc_requests(doc_content)
except Exception as e:
raise ValueError(f"Failed to parse NEW_DOC_CONTENT: {e}")
raise ValueError(f"Failed to parse given doc content: {e}")

file_id = extract_file_id(doc_ref)
docs_service = client('docs', 'v1')
drive_service = client('drive', 'v3')

Expand Down Expand Up @@ -142,8 +130,23 @@ def main():

print(f"Document updated successfully: {file_id}")

# Move the document to the specified folder, if NEW_DRIVE_DIR is set
move_doc(drive_service, file_id, new_drive_dir)
# Move the document to the specified folder
move_doc(drive_service, file_id, drive_dir)

def main():
try:
doc_ref = os.getenv('DOC_REF')
doc_content = os.getenv('DOC_CONTENT')
drive_dir = os.getenv('DOC_DRIVE_DIR', '').strip()

if not doc_ref:
raise ValueError('DOC_REF environment variable is missing or empty')

if not doc_content:
raise ValueError('DOC_CONTENT environment variable is missing or empty')

file_id = extract_file_id(doc_ref)
update_doc(file_id, doc_content, drive_dir)

except Exception as err:
sys.stderr.write(f"Error: {err}\n")
Expand Down