Skip to content

Commit

Permalink
feat: 支持 AI 技能开发和直通模式
Browse files Browse the repository at this point in the history
Signed-off-by: Ke Jie <[email protected]>
  • Loading branch information
chzealot committed Nov 6, 2024
1 parent e9a24f3 commit 262f2bf
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
2 changes: 2 additions & 0 deletions dingtalk_stream/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from .chatbot import TextContent
from .chatbot import AtUser
from .chatbot import ChatbotHandler, AsyncChatbotHandler
from .graph import RequestLine, StatusLine, GraphMessage, GraphRequest, GraphResponse
from .graph import GraphHandler
from .card_replier import AICardStatus, AICardReplier, CardReplier
from .card_instance import MarkdownCardInstance, AIMarkdownCardInstance, CarouselCardInstance, \
MarkdownButtonCardInstance, RPAPluginCardInstance
Expand Down
129 changes: 129 additions & 0 deletions dingtalk_stream/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# -*- coding:utf-8 -*-

import json
from .stream import CallbackHandler, CallbackMessage

class GraphMessage(object):
TOPIC = '/v1.0/graph/api/invoke'

class RequestLine(object):
def __init__(self):
self.method = 'GET'
self.uri = '/'
self.extensions = {}

@classmethod
def from_dict(cls, d):
msg = RequestLine()
for name, value in d.items():
if name == 'method':
msg.method = value
elif name == 'uri':
msg.uri = value
else:
msg.extensions[name] = value
return msg

def to_dict(self):
result = self.extensions.copy()
if self.method is not None:
result['method'] = self.method
if self.uri is not None:
result['uri'] = self.uri
return result

class StatusLine(object):
def __init__(self):
self.code = 200
self.reason_phrase = 'OK'
self.extensions = {}

@classmethod
def from_dict(cls, d):
msg = RequestLine()
for name, value in d.items():
if name == 'code':
msg.code = value
elif name == 'reasonPhrase':
msg.reason_phrase = value
else:
msg.extensions[name] = value
return msg

def to_dict(self):
result = self.extensions.copy()
if self.code is not None:
result['code'] = self.code
if self.reason_phrase is not None:
result['reasonPhrase'] = self.reason_phrase
return result

class GraphRequest(object):
def __init__(self):
self.body = None
self.request_line = RequestLine()
self.headers = {}
self.extensions = {}

@classmethod
def from_dict(cls, d):
msg = GraphRequest()
for name, value in d.items():
if name == 'body':
msg.body = value
elif name == 'headers':
msg.headers = value
elif name == 'requestLine':
msg.request_line = RequestLine.from_dict(value)
else:
msg.extensions[name] = value
return msg

def to_dict(self):
result = self.extensions.copy()
if self.body is not None:
result['body'] = self.body
if self.headers is not None:
result['headers'] = self.headers
if self.request_line is not None:
result['requestLine'] = self.request_line.to_dict()
return result

class GraphResponse(object):
def __init__(self):
self.body = None
self.headers = {}
self.status_line = StatusLine()
self.extensions = {}

@classmethod
def from_dict(cls, d):
msg = GraphResponse()
for name, value in d.items():
if name == 'body':
msg.body = value
elif name == 'headers':
msg.headers = value
elif name == 'statusLine':
msg.status_line = StatusLine.from_dict(value)
else:
msg.extensions[name] = value
return msg

def to_dict(self):
result = self.extensions.copy()
if self.body is not None:
result['body'] = self.body
if self.headers is not None:
result['headers'] = self.headers
if self.status_line is not None:
result['statusLine'] = self.status_line.to_dict()
return result


class GraphHandler(CallbackHandler):

def __init__(self):
super(GraphHandler, self).__init__()


2 changes: 1 addition & 1 deletion dingtalk_stream/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION_STRING = '0.21.0'
VERSION_STRING = '0.22.0'

0 comments on commit 262f2bf

Please sign in to comment.