Skip to content
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"build": "yarn process-dynamic-import --lint && yarn fast-build shared-prompt-renderer && yarn fast-build core && yarn fast-build",
"bump": "yakumo version",
"dep": "yakumo upgrade",
"test": "cross-env TS_NODE_PROJECT=packages/core/tests/tsconfig.json yakumo mocha -r tsconfig-paths/register -r esbuild-register -r yml-register --exit",
"pub": "yakumo publish --tag latest",
"pub:next": "yakumo publish --tag next",
"lint": "yarn eslint packages --cache --ext=ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-claude/src/requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class ClaudeRequester extends ModelRequester<ClientConfig> {
: undefined
})

const iterator = sseIterable(response)
const iterator = sseIterable(response, params)
const reasoningState = new ReasoningState<ClaudeReasoningBlockParam>()

for await (const event of iterator) {
Expand Down
19 changes: 17 additions & 2 deletions packages/adapter-deepseek/src/requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
completionStream,
createEmbeddings,
createRequestContext,
createRequestSignal,
parseOpenAIModelNameWithReasoningEffort,
processStreamResponse
} from '@chatluna/v1-shared-adapter'
Expand Down Expand Up @@ -87,17 +88,31 @@ export class DeepseekRequester
delete request.top_p
}

const requestSignal = createRequestSignal(params)

try {
const response = await this.post('chat/completions', request, {
signal: params.signal
signal: requestSignal.signal
})
requestSignal.clearTimeout()

yield* processStreamResponse(requestContext, sseIterable(response))
yield* processStreamResponse(
requestContext,
sseIterable(response, {
timeout: params.timeout,
signal: requestSignal.signal
})
)
Comment thread
dingyi222666 marked this conversation as resolved.
Resolved
} catch (e) {
if (requestSignal.signal?.aborted) {
throw requestSignal.signal.reason ?? e
}
if (e instanceof ChatLunaError) {
throw e
}
throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e)
} finally {
requestSignal.dispose()
}
}

Expand Down
233 changes: 138 additions & 95 deletions packages/adapter-dify/src/requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
isMessageContentText
} from 'koishi-plugin-chatluna/utils/string'
import { File, FormData } from 'undici'
import { createRequestSignal } from '@chatluna/v1-shared-adapter'
import {
mapMimeToFileType,
resolveFilePayload,
Expand Down Expand Up @@ -162,73 +163,94 @@ export class DifyRequester extends ModelRequester<DifyClientConfig> {
requestBody.files = files
}

const response = await this._post(
'/chat-messages',
requestBody,
config.apiKey,
{
signal: params.signal
}
)
const requestSignal = createRequestSignal(params)

try {
const response = await this._post(
'/chat-messages',
requestBody,
config.apiKey,
{
signal: requestSignal.signal
}
)
requestSignal.clearTimeout()

const iterator = sseIterable(response)
let updatedDifyConversationId: string | undefined
const iterator = sseIterable(response, {
timeout: params.timeout,
signal: requestSignal.signal
})
let updatedDifyConversationId: string | undefined

for await (const event of iterator) {
const chunk = event.data
for await (const event of iterator) {
const chunk = event.data

if (chunk == null) {
continue
}
if (chunk == null) {
continue
}

let data: AssistantStreamResponse
let data: AssistantStreamResponse

try {
data = JSON.parse(chunk)
} catch (err) {
this.ctx.logger.error(
'error when parsing dify stream response, Result:' + chunk
)
throw new ChatLunaError(
ChatLunaErrorCode.API_REQUEST_FAILED,
new Error(
'error when calling dify completion, Result: ' + chunk
try {
data = JSON.parse(chunk)
} catch (err) {
this.ctx.logger.error(
'error when parsing dify stream response, Result:' +
chunk
)
)
}
throw new ChatLunaError(
ChatLunaErrorCode.API_REQUEST_FAILED,
new Error(
'error when calling dify completion, Result: ' +
chunk
)
)
}

if (data.event === 'error') {
throw new ChatLunaError(
ChatLunaErrorCode.API_REQUEST_FAILED,
new Error(
'error when calling dify completion, Result:' + chunk
if (data.event === 'error') {
throw new ChatLunaError(
ChatLunaErrorCode.API_REQUEST_FAILED,
new Error(
'error when calling dify completion, Result:' +
chunk
)
)
)
}
}

const content = data.answer
const content = data.answer

if (content != null) {
const messageChunk = new AIMessageChunk({ content })
const generationChunk = new ChatGenerationChunk({
message: messageChunk,
text: content
})
if (content != null) {
const messageChunk = new AIMessageChunk({ content })
const generationChunk = new ChatGenerationChunk({
message: messageChunk,
text: content
})

yield generationChunk
}
yield generationChunk
}

updatedDifyConversationId = data.conversation_id
updatedDifyConversationId = data.conversation_id

if (data.event === 'message_end') {
await this.updateDifyConversationId(
conversationId,
config.workflowName,
updatedDifyConversationId,
difyUser
)
break
if (data.event === 'message_end') {
await this.updateDifyConversationId(
conversationId,
config.workflowName,
updatedDifyConversationId,
difyUser
)
break
}
}
} catch (e) {
if (requestSignal.signal?.aborted) {
throw requestSignal.signal.reason ?? e
}
if (e instanceof ChatLunaError) {
throw e
}
throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e)
} finally {
requestSignal.dispose()
}
}

Expand Down Expand Up @@ -267,60 +289,81 @@ export class DifyRequester extends ModelRequester<DifyClientConfig> {
requestBody.files = files
}

const response = await this._post(
'/workflows/run',
requestBody,
config.apiKey,
{
signal: params.signal
}
)
const requestSignal = createRequestSignal(params)

const iterator = sseIterable(response)
try {
const response = await this._post(
'/workflows/run',
requestBody,
config.apiKey,
{
signal: requestSignal.signal
}
)
requestSignal.clearTimeout()

for await (const event of iterator) {
const chunk = event.data
const iterator = sseIterable(response, {
timeout: params.timeout,
signal: requestSignal.signal
})

if (chunk == null) {
continue
}
for await (const event of iterator) {
const chunk = event.data

if (chunk == null) {
continue
}

let data: AssistantStreamResponse
let data: AssistantStreamResponse

try {
data = JSON.parse(chunk)
} catch (err) {
this.ctx.logger.error(
'error when parsing dify stream response, Result:' + chunk
)
throw new ChatLunaError(
ChatLunaErrorCode.API_REQUEST_FAILED,
new Error(
'error when calling dify completion, Result: ' + chunk
try {
data = JSON.parse(chunk)
} catch (err) {
this.ctx.logger.error(
'error when parsing dify stream response, Result:' +
chunk
)
)
}
throw new ChatLunaError(
ChatLunaErrorCode.API_REQUEST_FAILED,
new Error(
'error when calling dify completion, Result: ' +
chunk
)
)
}

if (data.event === 'error') {
throw new ChatLunaError(
ChatLunaErrorCode.API_REQUEST_FAILED,
new Error(
'error when calling dify completion, Result:' + chunk
if (data.event === 'error') {
throw new ChatLunaError(
ChatLunaErrorCode.API_REQUEST_FAILED,
new Error(
'error when calling dify completion, Result:' +
chunk
)
)
)
}
}

const content = data.answer
const content = data.answer

if (content != null) {
const messageChunk = new AIMessageChunk({ content })
const generationChunk = new ChatGenerationChunk({
message: messageChunk,
text: content
})
if (content != null) {
const messageChunk = new AIMessageChunk({ content })
const generationChunk = new ChatGenerationChunk({
message: messageChunk,
text: content
})

yield generationChunk
yield generationChunk
}
}
} catch (e) {
if (requestSignal.signal?.aborted) {
throw requestSignal.signal.reason ?? e
}
if (e instanceof ChatLunaError) {
throw e
}
throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e)
} finally {
requestSignal.dispose()
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-doubao/src/requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class DoubaoRequester
signal: params.signal
})

const iterator = sseIterable(response)
const iterator = sseIterable(response, params)
yield* processStreamResponse(requestContext, iterator)
} catch (e) {
if (e instanceof ChatLunaError) {
Expand Down
Loading