Skip to content

Commit ea53571

Browse files
committed
feat: Add DevChat workflow extension API docs
- Implemented inline documentation for foundation module functions - Included docstrings with detailed descriptions and return types - Functions documented include context retrieval and user input handling
1 parent bcba743 commit ea53571

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

libs/foundation/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from .foundation import (
2+
get_context_contents,
3+
get_devchat_site_packages_path,
4+
get_llm_model,
5+
get_parent_hash,
6+
get_user_input_files,
7+
get_user_input_text
8+
)
9+
10+
__all__ = [
11+
"get_context_contents",
12+
"get_devchat_site_packages_path",
13+
"get_llm_model",
14+
"get_parent_hash",
15+
"get_user_input_files",
16+
"get_user_input_text"
17+
]

libs/foundation/foundation.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import os
2+
import json
3+
4+
5+
def get_devchat_site_packages_path():
6+
"""@DevChatApi
7+
Retrieve the DevChat site packages path from the environment variable.
8+
9+
Returns:
10+
str: The path stored in DEVCHAT_PYTHONPATH environment variable,
11+
or an empty string if the variable isn't set.
12+
"""
13+
return os.environ.get("DEVCHAT_PYTHONPATH", "")
14+
15+
16+
def get_llm_model():
17+
"""@DevChatApi
18+
Retrieve the DevChat LLM model from the environment variable.
19+
20+
Returns:
21+
str: The LLM model stored in LLM_MODEL environment variable,
22+
or an empty string if the variable isn't set.
23+
"""
24+
return os.environ.get("LLM_MODEL", "gpt-3.5-turbo-1106")
25+
26+
27+
def get_parent_hash():
28+
"""@DevChatApi
29+
Retrieves the parent hash value from the environment variable.
30+
31+
This function is designed to obtain the hash value of the parent message in a sequence of chat interactions,
32+
where each pair of question and response is associated with respective hash values. It helps in reconstructing
33+
the order of conversation threads by providing the parent hash for the current message.
34+
35+
Returns:
36+
str: The parent hash value stored in the PARENT_HASH environment variable,
37+
or an empty string if the variable isn't set.
38+
"""
39+
return os.environ.get("PARENT_HASH", "")
40+
41+
42+
def get_context_contents():
43+
"""@DevChatApi
44+
Retrieve the chat context history from an environment variable.
45+
46+
This function looks up the `CONTEXT_CONTENTS` environment variable, parses it as JSON, and returns the result.
47+
It's used to obtain the historical content of the chat, which is stored in a JSON array format.
48+
Each item in the array represents a message with a sender role and content, for example:
49+
[{"role": "user", "content": "User's message"}, {"role": "assistant", "content": "Assistant's reply"}].
50+
51+
Returns:
52+
list: A list of dictionaries representing the chat history. Each dictionary contains the keys 'role' and
53+
'content', corresponding to who sent the message and what the message was, respectively.
54+
If the `CONTEXT_CONTENTS` variable is not set, an empty list is returned.
55+
"""
56+
return json.loads(os.environ.get("CONTEXT_CONTENTS", "[]"))
57+
58+
59+
def get_user_input_text():
60+
"""@DevChatApi
61+
Fetch the most recent user input from the chat context.
62+
63+
This function retrieves the current chat context by calling the
64+
`get_context_contents` function, which returns a list of message
65+
dictionaries. It then extracts the content of the last message in the
66+
context, assuming it's from the user, returning it as a string. If the
67+
context is empty, an empty string is returned. This function is typically
68+
used to get the last input provided by the user in the chat interface.
69+
70+
Returns:
71+
str: The content of the most recent user input message if it exists,
72+
otherwise an empty string.
73+
"""
74+
contexts = get_context_contents()
75+
user_index = 0
76+
for index, context in enumerate(contexts):
77+
if context["role"] == "user":
78+
user_index = index
79+
return contexts[user_index]["content"] if len(contexts) > 0 else ""
80+
81+
82+
# 获取用户输入的相关文件内容。这些相关文件一般通过Add to DevChat右键菜单添加到聊天上下文中。
83+
def get_user_input_files():
84+
"""@DevChatApi
85+
Retrieve a list of file contents that were input by the user.
86+
87+
This function scans through the chat context (which contains all messages and interactions
88+
within a chat session) to identify any files that were added by the user, typically via the
89+
'Add to DevChat' right-click context menu option. Files added by the user are delineated in the
90+
chat context as having a 'role' other than 'assistant'. This function finds the last occurrence
91+
of a message where the 'assistant' role is responsible and then returns the content of all
92+
subsequent messages until the penultimate one, assuming these are the user's input files.
93+
94+
Returns:
95+
list[str]: A list of the content from all files that were input by the user. Each item
96+
in the list is the contents of one file.
97+
"""
98+
contexts = get_context_contents()
99+
last_index = 0
100+
for index, item in enumerate(contexts):
101+
if item["role"] == "user":
102+
last_index = index + 1
103+
if last_index == len(contexts):
104+
return []
105+
return [item["content"] for item in contexts[last_index:]]
106+
107+

0 commit comments

Comments
 (0)