Skip to content

Commit 7eb6dd1

Browse files
author
linyuan.yang
committed
openai 问题
1 parent 20be2ed commit 7eb6dd1

5 files changed

Lines changed: 59 additions & 9 deletions

File tree

‎packages/admin/src/i18n/en.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,9 @@ export default {
731731
},
732732
logs: {
733733
title: 'Logs',
734+
lifecycle: 'Lifecycle Logs',
735+
lifecycle_file: 'Lifecycle Logs (process.log)',
736+
lifecycle_empty: 'No lifecycle entries yet. Sbot startup and exit events will appear here.',
734737
all_levels: 'All Levels',
735738
search_placeholder: 'Search keyword...',
736739
last_n: 'Last {n} lines',

‎packages/admin/src/i18n/zh.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,9 @@ export default {
731731
},
732732
logs: {
733733
title: '日志',
734+
lifecycle: '生命周期日志',
735+
lifecycle_file: '生命周期日志(process.log)',
736+
lifecycle_empty: '暂无生命周期日志,sbot 启动或退出后会记录在这里',
734737
all_levels: '全部级别',
735738
search_placeholder: '搜索关键词...',
736739
last_n: '最近 {n} 行',

‎packages/admin/src/views/admin/LogsView.vue‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { ref, onMounted, onUnmounted, nextTick, watch } from 'vue'
2+
import { computed, ref, onMounted, onUnmounted, nextTick, watch } from 'vue'
33
import { useI18n } from 'vue-i18n'
44
import { apiFetch } from '@/shared/api'
55
import { useToast, SButton, SSelect, SInput, SPageToolbar } from 'sbot-ui'
@@ -23,13 +23,25 @@ let refreshTimer: ReturnType<typeof setInterval> | null = null
2323
2424
const levels = ['', 'DEBUG', 'INFO', 'WARN', 'ERROR']
2525
const intervalOptions = [3, 5, 10, 30]
26+
const lifecycleLogFile = 'process.log'
27+
const hasLifecycleLog = computed(() => files.value.includes(lifecycleLogFile))
28+
const isLifecycleLog = computed(() => selectedFile.value === lifecycleLogFile)
29+
const emptyText = computed(() => isLifecycleLog.value ? t('logs.lifecycle_empty') : t('logs.empty'))
30+
31+
function fileLabel(file: string): string {
32+
return file === lifecycleLogFile ? t('logs.lifecycle_file') : file
33+
}
34+
35+
function selectLifecycleLog() {
36+
if (hasLifecycleLog.value) selectedFile.value = lifecycleLogFile
37+
}
2638
2739
async function loadFiles() {
2840
try {
2941
const res = await apiFetch('/api/logs')
3042
files.value = res.data || []
3143
if (files.value.length && !selectedFile.value) {
32-
selectedFile.value = files.value[0]
44+
selectedFile.value = files.value.find(file => file !== lifecycleLogFile) || files.value[0]
3345
}
3446
} catch (e: any) {
3547
show(e.message, 'error')
@@ -113,8 +125,16 @@ onUnmounted(() => stopAutoRefresh())
113125
<template>
114126
<div style="height:100%;display:flex;flex-direction:column;overflow:hidden">
115127
<SPageToolbar :title="t('logs.title')">
128+
<SButton
129+
:type="isLifecycleLog ? 'primary' : 'outline'"
130+
size="sm"
131+
:disabled="!hasLifecycleLog"
132+
@click="selectLifecycleLog"
133+
>
134+
{{ t('logs.lifecycle') }}
135+
</SButton>
116136
<SSelect v-model="selectedFile" size="sm" class="logs-file-select">
117-
<option v-for="f in files" :key="f" :value="f">{{ f }}</option>
137+
<option v-for="f in files" :key="f" :value="f">{{ fileLabel(f) }}</option>
118138
</SSelect>
119139
<SSelect v-model="levelFilter" size="sm" @change="loadContent()">
120140
<option value="">{{ t('logs.all_levels') }}</option>
@@ -139,7 +159,7 @@ onUnmounted(() => stopAutoRefresh())
139159
</SPageToolbar>
140160
<div ref="logRef" class="log-viewer">
141161
<div v-if="loading" class="log-empty">{{ t('common.loading') }}</div>
142-
<div v-else-if="!lines.length" class="log-empty">{{ t('logs.empty') }}</div>
162+
<div v-else-if="!lines.length" class="log-empty">{{ emptyText }}</div>
143163
<template v-else>
144164
<div v-for="(line, i) in lines" :key="i" class="log-line" :class="lineClass(line)">{{ line }}</div>
145165
</template>

‎packages/scorpio.ai/src/Model/OpenAIModelService.ts‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1-
import { ChatOpenAI } from "@langchain/openai";
1+
import { type BaseMessageChunk } from "@langchain/core/messages";
2+
import { ChatOpenAI, ChatOpenAICompletions, type ChatOpenAIFields } from "@langchain/openai";
23
import { ModelServiceBase } from "./ModelServiceBase";
34
import { type StructuredInvokeOptions } from "./IModelService";
45
import { type ChatMessage } from "../Saver/IAgentSaverService";
56
import { getInvokeConfig, StructuredOutputMethod, toStructuredInput } from "./structuredOutput";
67

8+
/**
9+
* 部分 OpenAI 兼容端点不会在首个流式 delta 中返回 role。
10+
* LangChain 默认会把这种 delta 转成 ChatMessageChunk,导致后续无法按 AI 消息处理;
11+
* 模型输出在缺少显式 role 时应按 assistant 解析。
12+
*/
13+
class CompatibleChatOpenAICompletions extends ChatOpenAICompletions {
14+
protected override _convertCompletionsDeltaToBaseMessageChunk(
15+
delta: Record<string, any>,
16+
rawResponse: any,
17+
defaultRole?: any,
18+
): BaseMessageChunk {
19+
return super._convertCompletionsDeltaToBaseMessageChunk(
20+
delta,
21+
rawResponse,
22+
defaultRole ?? "assistant",
23+
);
24+
}
25+
}
26+
727
/**
828
* OpenAI 模型服务实现
929
* 封装 @langchain/openai 的 ChatOpenAI
1030
*/
1131
export class OpenAIModelService extends ModelServiceBase<ChatOpenAI> {
1232

13-
protected buildChatOpenAIOptions(): ConstructorParameters<typeof ChatOpenAI>[0] {
33+
protected buildChatOpenAIOptions(): ChatOpenAIFields {
1434
return {
1535
configuration: {
1636
baseURL: this.config.baseURL,
@@ -67,7 +87,11 @@ export class OpenAIModelService extends ModelServiceBase<ChatOpenAI> {
6787
}
6888

6989
protected createModel(): ChatOpenAI {
70-
return new ChatOpenAI(this.buildChatOpenAIOptions());
90+
const options = this.buildChatOpenAIOptions();
91+
return new ChatOpenAI({
92+
...options,
93+
completions: new CompatibleChatOpenAICompletions(options),
94+
});
7195
}
7296

7397
async invokeStructured<T = any>(schema: any, prompt: string | ChatMessage[], options?: StructuredInvokeOptions): Promise<T> {

‎packages/scorpio.ai/src/Model/OpenAIResponseModelService.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChatOpenAI } from "@langchain/openai";
1+
import { type ChatOpenAIFields } from "@langchain/openai";
22
import { ModelConfig } from "./types";
33
import { OpenAIModelService } from "./OpenAIModelService";
44

@@ -11,7 +11,7 @@ export class OpenAIResponseModelService extends OpenAIModelService {
1111
super(config);
1212
}
1313

14-
protected override buildChatOpenAIOptions(): ConstructorParameters<typeof ChatOpenAI>[0] {
14+
protected override buildChatOpenAIOptions(): ChatOpenAIFields {
1515
return {
1616
...super.buildChatOpenAIOptions(),
1717
useResponsesApi: true,

0 commit comments

Comments
 (0)