@vegamo/loom 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +465 -0
- package/dist/agents/core/agent.d.ts +65 -0
- package/dist/agents/core/agent.d.ts.map +1 -0
- package/dist/agents/core/agent.js +277 -0
- package/dist/agents/core/agent.js.map +1 -0
- package/dist/agents/core/prompt.d.ts +11 -0
- package/dist/agents/core/prompt.d.ts.map +1 -0
- package/dist/agents/core/prompt.js +117 -0
- package/dist/agents/core/prompt.js.map +1 -0
- package/dist/agents/core/types.d.ts +49 -0
- package/dist/agents/core/types.d.ts.map +1 -0
- package/dist/agents/core/types.js +5 -0
- package/dist/agents/core/types.js.map +1 -0
- package/dist/agents/memory/memory.d.ts +62 -0
- package/dist/agents/memory/memory.d.ts.map +1 -0
- package/dist/agents/memory/memory.js +113 -0
- package/dist/agents/memory/memory.js.map +1 -0
- package/dist/agents/memory/types.d.ts +15 -0
- package/dist/agents/memory/types.d.ts.map +1 -0
- package/dist/agents/memory/types.js +5 -0
- package/dist/agents/memory/types.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +104 -0
- package/dist/index.js.map +1 -0
- package/dist/llm/client.d.ts +49 -0
- package/dist/llm/client.d.ts.map +1 -0
- package/dist/llm/client.js +256 -0
- package/dist/llm/client.js.map +1 -0
- package/dist/llm/config.d.ts +13 -0
- package/dist/llm/config.d.ts.map +1 -0
- package/dist/llm/config.js +50 -0
- package/dist/llm/config.js.map +1 -0
- package/dist/llm/types.d.ts +74 -0
- package/dist/llm/types.d.ts.map +1 -0
- package/dist/llm/types.js +17 -0
- package/dist/llm/types.js.map +1 -0
- package/dist/mocks/generator.d.ts +10 -0
- package/dist/mocks/generator.d.ts.map +1 -0
- package/dist/mocks/generator.js +19 -0
- package/dist/mocks/generator.js.map +1 -0
- package/dist/mocks/router.d.ts +13 -0
- package/dist/mocks/router.d.ts.map +1 -0
- package/dist/mocks/router.js +44 -0
- package/dist/mocks/router.js.map +1 -0
- package/dist/mocks/server.d.ts +21 -0
- package/dist/mocks/server.d.ts.map +1 -0
- package/dist/mocks/server.js +36 -0
- package/dist/mocks/server.js.map +1 -0
- package/dist/serve.d.ts +17 -0
- package/dist/serve.d.ts.map +1 -0
- package/dist/serve.js +56 -0
- package/dist/serve.js.map +1 -0
- package/dist/shared/config-wizard.d.ts +7 -0
- package/dist/shared/config-wizard.d.ts.map +1 -0
- package/dist/shared/config-wizard.js +84 -0
- package/dist/shared/config-wizard.js.map +1 -0
- package/dist/shared/config.d.ts +32 -0
- package/dist/shared/config.d.ts.map +1 -0
- package/dist/shared/config.js +77 -0
- package/dist/shared/config.js.map +1 -0
- package/dist/shared/entity-utils.d.ts +30 -0
- package/dist/shared/entity-utils.d.ts.map +1 -0
- package/dist/shared/entity-utils.js +207 -0
- package/dist/shared/entity-utils.js.map +1 -0
- package/dist/shared/logger.d.ts +13 -0
- package/dist/shared/logger.d.ts.map +1 -0
- package/dist/shared/logger.js +45 -0
- package/dist/shared/logger.js.map +1 -0
- package/dist/shared/manifest-utils.d.ts +9 -0
- package/dist/shared/manifest-utils.d.ts.map +1 -0
- package/dist/shared/manifest-utils.js +110 -0
- package/dist/shared/manifest-utils.js.map +1 -0
- package/dist/shared/schema-entity-resolver.d.ts +15 -0
- package/dist/shared/schema-entity-resolver.d.ts.map +1 -0
- package/dist/shared/schema-entity-resolver.js +134 -0
- package/dist/shared/schema-entity-resolver.js.map +1 -0
- package/dist/shared/schema-utils.d.ts +21 -0
- package/dist/shared/schema-utils.d.ts.map +1 -0
- package/dist/shared/schema-utils.js +56 -0
- package/dist/shared/schema-utils.js.map +1 -0
- package/dist/shared/types.d.ts +130 -0
- package/dist/shared/types.d.ts.map +1 -0
- package/dist/shared/types.js +5 -0
- package/dist/shared/types.js.map +1 -0
- package/dist/tools/entity-file-ops.d.ts +10 -0
- package/dist/tools/entity-file-ops.d.ts.map +1 -0
- package/dist/tools/entity-file-ops.js +183 -0
- package/dist/tools/entity-file-ops.js.map +1 -0
- package/dist/tools/entity-workflow.d.ts +10 -0
- package/dist/tools/entity-workflow.d.ts.map +1 -0
- package/dist/tools/entity-workflow.js +447 -0
- package/dist/tools/entity-workflow.js.map +1 -0
- package/dist/tools/file-ops.d.ts +9 -0
- package/dist/tools/file-ops.d.ts.map +1 -0
- package/dist/tools/file-ops.js +83 -0
- package/dist/tools/file-ops.js.map +1 -0
- package/dist/tools/registry.d.ts +31 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +58 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/schema-gen.d.ts +8 -0
- package/dist/tools/schema-gen.d.ts.map +1 -0
- package/dist/tools/schema-gen.js +82 -0
- package/dist/tools/schema-gen.js.map +1 -0
- package/dist/tools/schema-validate.d.ts +8 -0
- package/dist/tools/schema-validate.d.ts.map +1 -0
- package/dist/tools/schema-validate.js +79 -0
- package/dist/tools/schema-validate.js.map +1 -0
- package/dist/tools/types.d.ts +29 -0
- package/dist/tools/types.d.ts.map +1 -0
- package/dist/tools/types.js +5 -0
- package/dist/tools/types.js.map +1 -0
- package/dist/tui/app.d.ts +13 -0
- package/dist/tui/app.d.ts.map +1 -0
- package/dist/tui/app.js +357 -0
- package/dist/tui/app.js.map +1 -0
- package/dist/tui/components/Chat.d.ts +14 -0
- package/dist/tui/components/Chat.d.ts.map +1 -0
- package/dist/tui/components/Chat.js +24 -0
- package/dist/tui/components/Chat.js.map +1 -0
- package/dist/tui/components/Header.d.ts +14 -0
- package/dist/tui/components/Header.d.ts.map +1 -0
- package/dist/tui/components/Header.js +6 -0
- package/dist/tui/components/Header.js.map +1 -0
- package/dist/tui/components/Input.d.ts +15 -0
- package/dist/tui/components/Input.d.ts.map +1 -0
- package/dist/tui/components/Input.js +74 -0
- package/dist/tui/components/Input.js.map +1 -0
- package/dist/tui/components/SchemaPreview.d.ts +13 -0
- package/dist/tui/components/SchemaPreview.d.ts.map +1 -0
- package/dist/tui/components/SchemaPreview.js +9 -0
- package/dist/tui/components/SchemaPreview.js.map +1 -0
- package/dist/tui/components/StatusBar.d.ts +15 -0
- package/dist/tui/components/StatusBar.d.ts.map +1 -0
- package/dist/tui/components/StatusBar.js +7 -0
- package/dist/tui/components/StatusBar.js.map +1 -0
- package/dist/tui/hooks/useAgent.d.ts +41 -0
- package/dist/tui/hooks/useAgent.d.ts.map +1 -0
- package/dist/tui/hooks/useAgent.js +180 -0
- package/dist/tui/hooks/useAgent.js.map +1 -0
- package/dist/view/frontend/components/App.d.ts +4 -0
- package/dist/view/frontend/components/App.d.ts.map +1 -0
- package/dist/view/frontend/components/App.js +35 -0
- package/dist/view/frontend/components/App.js.map +1 -0
- package/dist/view/frontend/components/EndpointDetailPage.d.ts +4 -0
- package/dist/view/frontend/components/EndpointDetailPage.d.ts.map +1 -0
- package/dist/view/frontend/components/EndpointDetailPage.js +103 -0
- package/dist/view/frontend/components/EndpointDetailPage.js.map +1 -0
- package/dist/view/frontend/components/HomePage.d.ts +4 -0
- package/dist/view/frontend/components/HomePage.d.ts.map +1 -0
- package/dist/view/frontend/components/HomePage.js +6 -0
- package/dist/view/frontend/components/HomePage.js.map +1 -0
- package/dist/view/frontend/components/ModuleDetailPage.d.ts +4 -0
- package/dist/view/frontend/components/ModuleDetailPage.d.ts.map +1 -0
- package/dist/view/frontend/components/ModuleDetailPage.js +64 -0
- package/dist/view/frontend/components/ModuleDetailPage.js.map +1 -0
- package/dist/view/frontend/components/ModulesPage.d.ts +4 -0
- package/dist/view/frontend/components/ModulesPage.d.ts.map +1 -0
- package/dist/view/frontend/components/ModulesPage.js +40 -0
- package/dist/view/frontend/components/ModulesPage.js.map +1 -0
- package/dist/view/frontend/index.d.ts +2 -0
- package/dist/view/frontend/index.d.ts.map +1 -0
- package/dist/view/frontend/index.js +15 -0
- package/dist/view/frontend/index.js.map +1 -0
- package/dist/view/frontend/types.d.ts +35 -0
- package/dist/view/frontend/types.d.ts.map +1 -0
- package/dist/view/frontend/types.js +2 -0
- package/dist/view/frontend/types.js.map +1 -0
- package/dist/view/frontend/utils/api.d.ts +5 -0
- package/dist/view/frontend/utils/api.d.ts.map +1 -0
- package/dist/view/frontend/utils/api.js +28 -0
- package/dist/view/frontend/utils/api.js.map +1 -0
- package/dist/view/public/bundle.js +104633 -0
- package/dist/view/public/bundle.js.map +7 -0
- package/dist/view/public/index.html +50 -0
- package/dist/view/routes/docs.d.ts +13 -0
- package/dist/view/routes/docs.d.ts.map +1 -0
- package/dist/view/routes/docs.js +53 -0
- package/dist/view/routes/docs.js.map +1 -0
- package/dist/view/routes/entities.d.ts +11 -0
- package/dist/view/routes/entities.d.ts.map +1 -0
- package/dist/view/routes/entities.js +39 -0
- package/dist/view/routes/entities.js.map +1 -0
- package/dist/view/routes/schema.d.ts +11 -0
- package/dist/view/routes/schema.d.ts.map +1 -0
- package/dist/view/routes/schema.js +43 -0
- package/dist/view/routes/schema.js.map +1 -0
- package/dist/view/server.d.ts +20 -0
- package/dist/view/server.d.ts.map +1 -0
- package/dist/view/server.js +54 -0
- package/dist/view/server.js.map +1 -0
- package/docgen.config.json +19 -0
- package/docgen.config.json.example +19 -0
- package/package.json +79 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../../../src/agents/memory/memory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAKH,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAEhC,MAAM,OAAO,MAAM;IACT,QAAQ,GAAiC,EAAE,CAAC;IAC5C,WAAW,CAAS;IAE5B,YAAY,MAAqB;QAC/B,IAAI,CAAC,WAAW,GAAG,MAAM,EAAE,WAAW,IAAI,oBAAoB,CAAC;IACjE,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,OAAmC;QACrC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC5B,IAAI,CAAC,IAAI,EAAE,C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AAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC5C,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;YACxB,IAAI,SAAS,GAAG,KAAK,CAAC,MAAM,GAAG,MAAM;gBAAE,MAAM;YAC7C,IAAI,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC;YACvB,SAAS,IAAI,KAAK,CAAC,MAAM,CAAC;QAC5B,CAAC;QAED,IAAI,CAAC,QAAQ,GAAG,CAAC,GAAG,cAAc,EAAE,GAAG,IAAI,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;;;OAOG;IACK,eAAe,CACrB,QAAsC;QAEtC,MAAM,MAAM,GAAmC,EAAE,CAAC;QAClD,IAAI,OAAO,GAAiC,EAAE,CAAC;QAE/C,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;YAC3B,IAAI,GAAG,CAAC,IAAI,KAAK,MAAM,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAC9C,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBACrB,OAAO,GAAG,EAAE,CAAC;YACf,CAAC;YACD,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACpB,CAAC;QAED,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACvB,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACvB,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory 类型定义
|
|
3
|
+
*/
|
|
4
|
+
import type { ChatCompletionMessageParam } from 'openai/resources/chat/completions.js';
|
|
5
|
+
/** 对话历史配置 */
|
|
6
|
+
export interface MemoryConfig {
|
|
7
|
+
/** 最大保留的消息轮数 (一问一答算两条) */
|
|
8
|
+
maxMessages?: number;
|
|
9
|
+
}
|
|
10
|
+
/** 对话历史快照 */
|
|
11
|
+
export interface ConversationSnapshot {
|
|
12
|
+
messages: ChatCompletionMessageParam[];
|
|
13
|
+
messageCount: number;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/agents/memory/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,sCAAsC,CAAC;AAEvF,aAAa;AACb,MAAM,WAAW,YAAY;IAC3B,0BAA0B;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,aAAa;AACb,MAAM,WAAW,oBAAoB;IACnC,QAAQ,EAAE,0BAA0B,EAAE,CAAC;IACvC,YAAY,EAAE,MAAM,CAAC;CACtB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/agents/memory/types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Loom CLI 入口
|
|
4
|
+
*
|
|
5
|
+
* 子命令:
|
|
6
|
+
* loom chat — TUI 对话,生成 JSON Schema
|
|
7
|
+
* loom view [--port] — Web 文档查看器
|
|
8
|
+
* loom mock [--port] — Mock 服务
|
|
9
|
+
* loom serve [--port] — 文档查看 + Mock 合一
|
|
10
|
+
* loom manifest rebuild — 重建 docs manifest 索引
|
|
11
|
+
*/
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA;;;;;;;;;GASG"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Loom CLI 入口
|
|
4
|
+
*
|
|
5
|
+
* 子命令:
|
|
6
|
+
* loom chat — TUI 对话,生成 JSON Schema
|
|
7
|
+
* loom view [--port] — Web 文档查看器
|
|
8
|
+
* loom mock [--port] — Mock 服务
|
|
9
|
+
* loom serve [--port] — 文档查看 + Mock 合一
|
|
10
|
+
* loom manifest rebuild — 重建 docs manifest 索引
|
|
11
|
+
*/
|
|
12
|
+
import { Command } from 'commander';
|
|
13
|
+
import { loadConfig, getOutDir } from './shared/config.js';
|
|
14
|
+
import { ensureChatConfig } from './shared/config-wizard.js';
|
|
15
|
+
import { logger } from './shared/logger.js';
|
|
16
|
+
const program = new Command();
|
|
17
|
+
program
|
|
18
|
+
.name('loom')
|
|
19
|
+
.description('AI-powered JSON Schema document generator')
|
|
20
|
+
.version('0.1.0');
|
|
21
|
+
// ============================================================
|
|
22
|
+
// loom chat
|
|
23
|
+
// ============================================================
|
|
24
|
+
program
|
|
25
|
+
.command('chat')
|
|
26
|
+
.description('Start TUI to generate JSON Schema documents via AI')
|
|
27
|
+
.option('-d, --dir <path>', 'Target project directory', process.cwd())
|
|
28
|
+
.action(async (opts) => {
|
|
29
|
+
const config = await ensureChatConfig(opts.dir);
|
|
30
|
+
const { render } = await import('ink');
|
|
31
|
+
const React = await import('react');
|
|
32
|
+
const { App } = await import('./tui/app.js');
|
|
33
|
+
const agentConfig = {
|
|
34
|
+
loomConfig: config,
|
|
35
|
+
projectDir: opts.dir,
|
|
36
|
+
};
|
|
37
|
+
render(React.createElement(App, { agentConfig }));
|
|
38
|
+
});
|
|
39
|
+
// ============================================================
|
|
40
|
+
// loom view
|
|
41
|
+
// ============================================================
|
|
42
|
+
program
|
|
43
|
+
.command('view')
|
|
44
|
+
.description('Start web documentation viewer')
|
|
45
|
+
.option('-p, --port <number>', 'Port number', '3000')
|
|
46
|
+
.option('-d, --dir <path>', 'Target project directory', process.cwd())
|
|
47
|
+
.action(async (opts) => {
|
|
48
|
+
const config = loadConfig(opts.dir);
|
|
49
|
+
const port = parseInt(opts.port, 10) || config.serve.port;
|
|
50
|
+
const docsDir = getOutDir(config, opts.dir);
|
|
51
|
+
const { startViewServer } = await import('./view/server.js');
|
|
52
|
+
await startViewServer({ port, docsDir });
|
|
53
|
+
});
|
|
54
|
+
// ============================================================
|
|
55
|
+
// loom mock
|
|
56
|
+
// ============================================================
|
|
57
|
+
program
|
|
58
|
+
.command('mock')
|
|
59
|
+
.description('Start mock API server based on JSON Schemas')
|
|
60
|
+
.option('-p, --port <number>', 'Port number', '3001')
|
|
61
|
+
.option('-d, --dir <path>', 'Target project directory', process.cwd())
|
|
62
|
+
.action(async (opts) => {
|
|
63
|
+
const config = loadConfig(opts.dir);
|
|
64
|
+
const port = parseInt(opts.port, 10) || config.mock.port;
|
|
65
|
+
const docsDir = getOutDir(config, opts.dir);
|
|
66
|
+
const { startMockServer } = await import('./mocks/server.js');
|
|
67
|
+
await startMockServer({ port, docsDir });
|
|
68
|
+
});
|
|
69
|
+
// ============================================================
|
|
70
|
+
// loom serve
|
|
71
|
+
// ============================================================
|
|
72
|
+
program
|
|
73
|
+
.command('serve')
|
|
74
|
+
.description('Start combined web viewer + mock server')
|
|
75
|
+
.option('-p, --port <number>', 'Port number', '3000')
|
|
76
|
+
.option('-d, --dir <path>', 'Target project directory', process.cwd())
|
|
77
|
+
.action(async (opts) => {
|
|
78
|
+
const config = loadConfig(opts.dir);
|
|
79
|
+
const port = parseInt(opts.port, 10) || config.serve.port;
|
|
80
|
+
const docsDir = getOutDir(config, opts.dir);
|
|
81
|
+
const { startServe } = await import('./serve.js');
|
|
82
|
+
await startServe({ port, docsDir });
|
|
83
|
+
});
|
|
84
|
+
// ============================================================
|
|
85
|
+
// loom manifest
|
|
86
|
+
// ============================================================
|
|
87
|
+
const manifestCommand = program
|
|
88
|
+
.command('manifest')
|
|
89
|
+
.description('Manifest utilities');
|
|
90
|
+
manifestCommand
|
|
91
|
+
.command('rebuild')
|
|
92
|
+
.description('Rebuild docs manifest index')
|
|
93
|
+
.option('-d, --dir <path>', 'Target project directory', process.cwd())
|
|
94
|
+
.action(async (opts) => {
|
|
95
|
+
const config = loadConfig(opts.dir);
|
|
96
|
+
const docsDir = getOutDir(config, opts.dir);
|
|
97
|
+
const { rebuildManifest } = await import('./shared/manifest-utils.js');
|
|
98
|
+
const manifest = rebuildManifest(docsDir);
|
|
99
|
+
logger.success(`Manifest rebuilt at ${docsDir}/.loom-manifest.json`);
|
|
100
|
+
logger.info(`Indexed ${manifest.endpointIndex.length} endpoints, ` +
|
|
101
|
+
`${manifest.entities.length} entities, ${manifest.references.length} references.`);
|
|
102
|
+
});
|
|
103
|
+
program.parse();
|
|
104
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA;;;;;;;;;GASG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;AAE9B,OAAO;KACJ,IAAI,CAAC,MAAM,CAAC;KACZ,WAAW,CAAC,2CAA2C,CAAC;KACxD,OAAO,CAAC,OAAO,CAAC,CAAC;AAEpB,+DAA+D;AAC/D,YAAY;AACZ,+DAA+D;AAC/D,OAAO;KACJ,OAAO,CAAC,MAAM,CAAC;KACf,WAAW,CAAC,oDAAoD,CAAC;KACjE,MAAM,CAAC,kBAAkB,EAAE,0BAA0B,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC;KACrE,MAAM,CAAC,KAAK,EAAE,IAAI,EA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 基于 OpenAI SDK,通过配置 baseURL 兼容 DeepSeek 等提供商
|
|
5
|
+
*
|
|
6
|
+
* 能力:
|
|
7
|
+
* - 非 streaming 完整请求 (chat)
|
|
8
|
+
* - Streaming 逐字输出 (chatStream)
|
|
9
|
+
* - 自动重试 (可重试错误)
|
|
10
|
+
* - 超时控制
|
|
11
|
+
* - AbortController 取消
|
|
12
|
+
*/
|
|
13
|
+
import type { LLMConfig } from '../shared/types.js';
|
|
14
|
+
import type { LLMRequest, LLMResponse, LLMStreamCallback } from './types.js';
|
|
15
|
+
export declare class LLMClient {
|
|
16
|
+
private client;
|
|
17
|
+
private config;
|
|
18
|
+
constructor(config: LLMConfig);
|
|
19
|
+
/**
|
|
20
|
+
* 发送完整聊天请求 (等待全部响应)
|
|
21
|
+
*/
|
|
22
|
+
chat(request: LLMRequest): Promise<LLMResponse>;
|
|
23
|
+
/**
|
|
24
|
+
* 发送 streaming 聊天请求
|
|
25
|
+
*
|
|
26
|
+
* 通过回调逐步接收文本片段和 tool call 信息
|
|
27
|
+
* 最终返回完整的 LLMResponse
|
|
28
|
+
*/
|
|
29
|
+
chatStream(request: LLMRequest, onEvent: LLMStreamCallback): Promise<LLMResponse>;
|
|
30
|
+
/**
|
|
31
|
+
* 解析非 streaming 响应
|
|
32
|
+
*/
|
|
33
|
+
private parseResponse;
|
|
34
|
+
/**
|
|
35
|
+
* 带重试的执行器
|
|
36
|
+
*
|
|
37
|
+
* 自动处理:
|
|
38
|
+
* - 可重试错误的指数退避重试
|
|
39
|
+
* - 外部 AbortSignal 取消
|
|
40
|
+
* - OpenAI SDK 错误的统一转换
|
|
41
|
+
*/
|
|
42
|
+
private withRetry;
|
|
43
|
+
/**
|
|
44
|
+
* 将各种错误统一转换为 LLMError
|
|
45
|
+
*/
|
|
46
|
+
private normalizeError;
|
|
47
|
+
private sleep;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/llm/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EACV,UAAU,EACV,WAAW,EACX,iBAAiB,EAGlB,MAAM,YAAY,CAAC;AAYpB,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,MAAM,CAAY;gBAEd,MAAM,EAAE,SAAS;IAuB7B;;OAEG;IACG,IAAI,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC;IAoCrD;;;;;OAKG;IACG,UAAU,CACd,OAAO,EAAE,UAAU,EACnB,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,WAAW,CAAC;IA6FvB;;OAEG;IACH,OAAO,CAAC,aAAa;IA+BrB;;;;;;;OAOG;YACW,SAAS;IA+CvB;;OAEG;IACH,OAAO,CAAC,cAAc;IAuDtB,OAAO,CAAC,KAAK;CAGd"}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM 客户端
|
|
3
|
+
*
|
|
4
|
+
* 基于 OpenAI SDK,通过配置 baseURL 兼容 DeepSeek 等提供商
|
|
5
|
+
*
|
|
6
|
+
* 能力:
|
|
7
|
+
* - 非 streaming 完整请求 (chat)
|
|
8
|
+
* - Streaming 逐字输出 (chatStream)
|
|
9
|
+
* - 自动重试 (可重试错误)
|
|
10
|
+
* - 超时控制
|
|
11
|
+
* - AbortController 取消
|
|
12
|
+
*/
|
|
13
|
+
import OpenAI from 'openai';
|
|
14
|
+
import { LLMError } from './types.js';
|
|
15
|
+
import { resolveLLMConfig } from './config.js';
|
|
16
|
+
import { logger } from '../shared/logger.js';
|
|
17
|
+
/** 默认超时 (ms) */
|
|
18
|
+
const DEFAULT_TIMEOUT = 120_000;
|
|
19
|
+
/** 默认最大重试次数 */
|
|
20
|
+
const DEFAULT_MAX_RETRIES = 2;
|
|
21
|
+
/** 重试间隔基数 (ms) */
|
|
22
|
+
const RETRY_BASE_DELAY = 1000;
|
|
23
|
+
export class LLMClient {
|
|
24
|
+
client;
|
|
25
|
+
config;
|
|
26
|
+
constructor(config) {
|
|
27
|
+
this.config = resolveLLMConfig(config);
|
|
28
|
+
if (!this.config.apiKey) {
|
|
29
|
+
throw new LLMError(`API key not found for provider "${this.config.provider}". ` +
|
|
30
|
+
`Set it in loom.config.json.`, 'auth_error');
|
|
31
|
+
}
|
|
32
|
+
this.client = new OpenAI({
|
|
33
|
+
apiKey: this.config.apiKey,
|
|
34
|
+
baseURL: this.config.baseURL,
|
|
35
|
+
timeout: DEFAULT_TIMEOUT,
|
|
36
|
+
maxRetries: 0, // 我们自己控制重试
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
// ============================================================
|
|
40
|
+
// 非 streaming 请求
|
|
41
|
+
// ============================================================
|
|
42
|
+
/**
|
|
43
|
+
* 发送完整聊天请求 (等待全部响应)
|
|
44
|
+
*/
|
|
45
|
+
async chat(request) {
|
|
46
|
+
return this.withRetry(async (signal) => {
|
|
47
|
+
logger.debug(`LLM request: ${request.messages.length} messages`);
|
|
48
|
+
const response = await this.client.chat.completions.create({
|
|
49
|
+
model: this.config.model,
|
|
50
|
+
messages: request.messages,
|
|
51
|
+
tools: request.tools?.length ? request.tools : undefined,
|
|
52
|
+
temperature: request.temperature ?? this.config.temperature ?? 0.7,
|
|
53
|
+
max_tokens: request.maxTokens ?? this.config.maxTokens,
|
|
54
|
+
}, { signal });
|
|
55
|
+
const choice = response.choices[0];
|
|
56
|
+
if (!choice) {
|
|
57
|
+
throw new LLMError('Empty response from LLM', 'invalid_response');
|
|
58
|
+
}
|
|
59
|
+
const result = this.parseResponse(choice, response.usage);
|
|
60
|
+
logger.debug(`LLM response: content=${result.content?.length ?? 0} chars, ` +
|
|
61
|
+
`toolCalls=${result.toolCalls.length}, ` +
|
|
62
|
+
`finish=${result.finishReason}`);
|
|
63
|
+
return result;
|
|
64
|
+
}, request.signal);
|
|
65
|
+
}
|
|
66
|
+
// ============================================================
|
|
67
|
+
// Streaming 请求
|
|
68
|
+
// ============================================================
|
|
69
|
+
/**
|
|
70
|
+
* 发送 streaming 聊天请求
|
|
71
|
+
*
|
|
72
|
+
* 通过回调逐步接收文本片段和 tool call 信息
|
|
73
|
+
* 最终返回完整的 LLMResponse
|
|
74
|
+
*/
|
|
75
|
+
async chatStream(request, onEvent) {
|
|
76
|
+
return this.withRetry(async (signal) => {
|
|
77
|
+
logger.debug(`LLM stream request: ${request.messages.length} messages`);
|
|
78
|
+
const stream = await this.client.chat.completions.create({
|
|
79
|
+
model: this.config.model,
|
|
80
|
+
messages: request.messages,
|
|
81
|
+
tools: request.tools?.length ? request.tools : undefined,
|
|
82
|
+
temperature: request.temperature ?? this.config.temperature ?? 0.7,
|
|
83
|
+
max_tokens: request.maxTokens ?? this.config.maxTokens,
|
|
84
|
+
stream: true,
|
|
85
|
+
}, { signal });
|
|
86
|
+
// 累积状态
|
|
87
|
+
let contentBuffer = '';
|
|
88
|
+
let finishReason = '';
|
|
89
|
+
const toolCallBuffers = new Map();
|
|
90
|
+
for await (const chunk of stream) {
|
|
91
|
+
const delta = chunk.choices[0]?.delta;
|
|
92
|
+
if (!delta)
|
|
93
|
+
continue;
|
|
94
|
+
// 文本内容增量
|
|
95
|
+
if (delta.content) {
|
|
96
|
+
contentBuffer += delta.content;
|
|
97
|
+
onEvent({ type: 'content_delta', delta: delta.content });
|
|
98
|
+
}
|
|
99
|
+
// Tool call 增量 (tool call 是分片传来的)
|
|
100
|
+
if (delta.tool_calls) {
|
|
101
|
+
for (const tc of delta.tool_calls) {
|
|
102
|
+
const idx = tc.index;
|
|
103
|
+
if (!toolCallBuffers.has(idx)) {
|
|
104
|
+
toolCallBuffers.set(idx, { id: '', name: '', arguments: '' });
|
|
105
|
+
}
|
|
106
|
+
const buf = toolCallBuffers.get(idx);
|
|
107
|
+
if (tc.id)
|
|
108
|
+
buf.id = tc.id;
|
|
109
|
+
if (tc.function?.name)
|
|
110
|
+
buf.name += tc.function.name;
|
|
111
|
+
if (tc.function?.arguments)
|
|
112
|
+
buf.arguments += tc.function.arguments;
|
|
113
|
+
onEvent({
|
|
114
|
+
type: 'tool_call_delta',
|
|
115
|
+
toolCall: {
|
|
116
|
+
index: idx,
|
|
117
|
+
id: buf.id || undefined,
|
|
118
|
+
function: { name: buf.name, arguments: buf.arguments },
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// finish reason
|
|
124
|
+
if (chunk.choices[0]?.finish_reason) {
|
|
125
|
+
finishReason = chunk.choices[0].finish_reason;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// 组装最终响应
|
|
129
|
+
const toolCalls = Array.from(toolCallBuffers.values())
|
|
130
|
+
.filter((tc) => tc.id && tc.name)
|
|
131
|
+
.map((tc) => ({
|
|
132
|
+
id: tc.id,
|
|
133
|
+
function: { name: tc.name, arguments: tc.arguments },
|
|
134
|
+
}));
|
|
135
|
+
const result = {
|
|
136
|
+
content: contentBuffer || null,
|
|
137
|
+
toolCalls,
|
|
138
|
+
finishReason,
|
|
139
|
+
};
|
|
140
|
+
onEvent({ type: 'done', response: result });
|
|
141
|
+
logger.debug(`LLM stream done: content=${contentBuffer.length} chars, ` +
|
|
142
|
+
`toolCalls=${toolCalls.length}, finish=${finishReason}`);
|
|
143
|
+
return result;
|
|
144
|
+
}, request.signal);
|
|
145
|
+
}
|
|
146
|
+
// ============================================================
|
|
147
|
+
// 内部工具方法
|
|
148
|
+
// ============================================================
|
|
149
|
+
/**
|
|
150
|
+
* 解析非 streaming 响应
|
|
151
|
+
*/
|
|
152
|
+
parseResponse(choice, usage) {
|
|
153
|
+
const parsedUsage = usage
|
|
154
|
+
? {
|
|
155
|
+
promptTokens: usage.prompt_tokens,
|
|
156
|
+
completionTokens: usage.completion_tokens,
|
|
157
|
+
totalTokens: usage.total_tokens,
|
|
158
|
+
}
|
|
159
|
+
: undefined;
|
|
160
|
+
return {
|
|
161
|
+
content: choice.message.content,
|
|
162
|
+
toolCalls: (choice.message.tool_calls || [])
|
|
163
|
+
.filter((tc) => tc.type === 'function')
|
|
164
|
+
.map((tc) => ({
|
|
165
|
+
id: tc.id,
|
|
166
|
+
function: {
|
|
167
|
+
name: tc.function.name,
|
|
168
|
+
arguments: tc.function.arguments,
|
|
169
|
+
},
|
|
170
|
+
})),
|
|
171
|
+
finishReason: choice.finish_reason ?? undefined,
|
|
172
|
+
usage: parsedUsage,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* 带重试的执行器
|
|
177
|
+
*
|
|
178
|
+
* 自动处理:
|
|
179
|
+
* - 可重试错误的指数退避重试
|
|
180
|
+
* - 外部 AbortSignal 取消
|
|
181
|
+
* - OpenAI SDK 错误的统一转换
|
|
182
|
+
*/
|
|
183
|
+
async withRetry(fn, externalSignal) {
|
|
184
|
+
let lastError = null;
|
|
185
|
+
for (let attempt = 0; attempt <= DEFAULT_MAX_RETRIES; attempt++) {
|
|
186
|
+
// 检查外部取消
|
|
187
|
+
if (externalSignal?.aborted) {
|
|
188
|
+
throw new LLMError('Request aborted', 'aborted');
|
|
189
|
+
}
|
|
190
|
+
// 合并内外超时信号
|
|
191
|
+
const timeoutController = new AbortController();
|
|
192
|
+
const timeout = setTimeout(() => timeoutController.abort(), DEFAULT_TIMEOUT);
|
|
193
|
+
const combinedSignal = externalSignal
|
|
194
|
+
? AbortSignal.any([externalSignal, timeoutController.signal])
|
|
195
|
+
: timeoutController.signal;
|
|
196
|
+
try {
|
|
197
|
+
const result = await fn(combinedSignal);
|
|
198
|
+
clearTimeout(timeout);
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
catch (err) {
|
|
202
|
+
clearTimeout(timeout);
|
|
203
|
+
const llmError = this.normalizeError(err);
|
|
204
|
+
lastError = llmError;
|
|
205
|
+
// 不可重试 或 已达最大重试次数 → 直接抛出
|
|
206
|
+
if (!llmError.retryable || attempt >= DEFAULT_MAX_RETRIES) {
|
|
207
|
+
throw llmError;
|
|
208
|
+
}
|
|
209
|
+
// 指数退避等待
|
|
210
|
+
const delay = RETRY_BASE_DELAY * Math.pow(2, attempt);
|
|
211
|
+
logger.warn(`LLM request failed (attempt ${attempt + 1}/${DEFAULT_MAX_RETRIES + 1}), ` +
|
|
212
|
+
`retrying in ${delay}ms: ${llmError.message}`);
|
|
213
|
+
await this.sleep(delay);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
throw lastError || new LLMError('Unknown error', 'unknown');
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* 将各种错误统一转换为 LLMError
|
|
220
|
+
*/
|
|
221
|
+
normalizeError(err) {
|
|
222
|
+
if (err instanceof LLMError)
|
|
223
|
+
return err;
|
|
224
|
+
if (err instanceof OpenAI.APIError) {
|
|
225
|
+
const status = err.status;
|
|
226
|
+
if (status === 401 || status === 403) {
|
|
227
|
+
return new LLMError(`Authentication failed: ${err.message}`, 'auth_error', { statusCode: status });
|
|
228
|
+
}
|
|
229
|
+
if (status === 429) {
|
|
230
|
+
return new LLMError(`Rate limit exceeded: ${err.message}`, 'rate_limit', { retryable: true, statusCode: status });
|
|
231
|
+
}
|
|
232
|
+
if (status && status >= 500) {
|
|
233
|
+
return new LLMError(`Server error: ${err.message}`, 'network_error', { retryable: true, statusCode: status });
|
|
234
|
+
}
|
|
235
|
+
return new LLMError(err.message, 'unknown', { statusCode: status });
|
|
236
|
+
}
|
|
237
|
+
if (err instanceof DOMException && err.name === 'AbortError') {
|
|
238
|
+
return new LLMError('Request aborted', 'aborted');
|
|
239
|
+
}
|
|
240
|
+
if (err instanceof Error) {
|
|
241
|
+
// 网络错误通常可重试
|
|
242
|
+
if (err.message.includes('ECONNREFUSED') ||
|
|
243
|
+
err.message.includes('ECONNRESET') ||
|
|
244
|
+
err.message.includes('ETIMEDOUT') ||
|
|
245
|
+
err.message.includes('fetch failed')) {
|
|
246
|
+
return new LLMError(`Network error: ${err.message}`, 'network_error', { retryable: true });
|
|
247
|
+
}
|
|
248
|
+
return new LLMError(err.message, 'unknown');
|
|
249
|
+
}
|
|
250
|
+
return new LLMError(String(err), 'unknown');
|
|
251
|
+
}
|
|
252
|
+
sleep(ms) {
|
|
253
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/llm/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAS5B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAE7C,gBAAgB;AAChB,MAAM,eAAe,GAAG,OAAO,CAAC;AAChC,eAAe;AACf,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAC9B,kBAAkB;AAClB,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAE9B,MAAM,OAAO,SAAS;IACZ,MAAM,CAAS;IACf,MAAM,CAAY;IAE1B,YAAY,MAAiB;QAC3B,IAAI,CAAC,MAAM,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;QAEvC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;YACxB,MAAM,IAAI,QAAQ,CAChB,mCAAmC,IAAI,CAAC,MAAM,CAAC,QAAQ,KAAK;gBACzD,6BAA6B,EAChC,YAAY,CACb,CAAC;QACJ,CAAC;QAED,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CAAC;YACvB,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM;YAC1B,OAAO,EA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,CAAC;YACJ,CAAC;YACD,OAAO,IAAI,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAC9C,CAAC;QAED,OAAO,IAAI,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,SAAS,CAAC,CAAC;IAC9C,CAAC;IAEO,KAAK,CAAC,EAAU;QACtB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;IAC3D,CAAC;CACF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM 配置管理
|
|
3
|
+
*/
|
|
4
|
+
import type { LLMConfig } from '../shared/types.js';
|
|
5
|
+
/**
|
|
6
|
+
* 解析 LLM 配置,合并预置值
|
|
7
|
+
*/
|
|
8
|
+
export declare function resolveLLMConfig(config: LLMConfig): LLMConfig;
|
|
9
|
+
/**
|
|
10
|
+
* 获取所有支持的预置 provider 名称
|
|
11
|
+
*/
|
|
12
|
+
export declare function getSupportedProviders(): string[];
|
|
13
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/llm/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAgBpD;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,SAAS,GAAG,SAAS,CAW7D;AAiBD;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,EAAE,CAEhD"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM 配置管理
|
|
3
|
+
*/
|
|
4
|
+
/** 预置的 LLM 提供商配置 */
|
|
5
|
+
const PROVIDER_PRESETS = {
|
|
6
|
+
deepseek: {
|
|
7
|
+
baseURL: 'https://api.deepseek.com/v1',
|
|
8
|
+
model: 'deepseek-chat',
|
|
9
|
+
temperature: 0.7,
|
|
10
|
+
},
|
|
11
|
+
openai: {
|
|
12
|
+
baseURL: 'https://api.openai.com/v1',
|
|
13
|
+
model: 'gpt-4o',
|
|
14
|
+
temperature: 0.7,
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* 解析 LLM 配置,合并预置值
|
|
19
|
+
*/
|
|
20
|
+
export function resolveLLMConfig(config) {
|
|
21
|
+
const preset = PROVIDER_PRESETS[config.provider] || {};
|
|
22
|
+
const resolved = {
|
|
23
|
+
...preset,
|
|
24
|
+
...config,
|
|
25
|
+
};
|
|
26
|
+
// 配置校验
|
|
27
|
+
validateConfig(resolved);
|
|
28
|
+
return resolved;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 校验关键配置字段
|
|
32
|
+
*/
|
|
33
|
+
function validateConfig(config) {
|
|
34
|
+
if (!config.baseURL) {
|
|
35
|
+
throw new Error(`LLM config error: baseURL is required for provider "${config.provider}"`);
|
|
36
|
+
}
|
|
37
|
+
if (!config.model) {
|
|
38
|
+
throw new Error(`LLM config error: model is required for provider "${config.provider}"`);
|
|
39
|
+
}
|
|
40
|
+
if (!config.apiKey) {
|
|
41
|
+
throw new Error(`LLM config error: apiKey is required for provider "${config.provider}"`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 获取所有支持的预置 provider 名称
|
|
46
|
+
*/
|
|
47
|
+
export function getSupportedProviders() {
|
|
48
|
+
return Object.keys(PROVIDER_PRESETS);
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/llm/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,oBAAoB;AACpB,MAAM,gBAAgB,GAAuC;IAC3D,QAAQ,EAAE;QACR,OAAO,EAAE,6BAA6B;QACtC,KAAK,EAAE,eAAe;QACtB,WAAW,EAAE,GAAG;KACjB;IACD,MAAM,EAAE;QACN,OAAO,EAAE,2BAA2B;QACpC,KAAK,EAAE,QAAQ;QACf,WAAW,EAAE,GAAG;KACjB;CACF,CAAC;AAEF;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAiB;IAChD,MAAM,MAAM,GAAG,gBAAgB,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;IACvD,MAAM,QAAQ,GAAc;QAC1B,GAAG,MAAM;QACT,GAAG,MAAM;KACV,CAAC;IAEF,OAAO;IACP,cAAc,CAAC,QAAQ,CAAC,CAAC;IAEzB,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;GAEG;AACH,SAAS,cAAc,CAAC,MAAiB;IACvC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,uDAAuD,MAAM,CAAC,QAAQ,GAAG,CAAC,CAAC;IAC7F,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,qDAAqD,MAAM,CAAC,QAAQ,GAAG,CAAC,CAAC;IAC3F,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,sDAAsD,MAAM,CAAC,QAAQ,GAAG,CAAC,CAAC;IAC5F,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB;IACnC,OAAO,MAAM,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM 相关类型定义
|
|
3
|
+
*/
|
|
4
|
+
import type { ChatCompletionMessageParam } from 'openai/resources/chat/completions.js';
|
|
5
|
+
/** LLM 请求参数 */
|
|
6
|
+
export interface LLMRequest {
|
|
7
|
+
messages: ChatCompletionMessageParam[];
|
|
8
|
+
tools?: LLMToolDefinition[];
|
|
9
|
+
temperature?: number;
|
|
10
|
+
maxTokens?: number;
|
|
11
|
+
/** 用于取消请求 */
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
}
|
|
14
|
+
/** LLM 工具定义 (OpenAI function calling 格式) */
|
|
15
|
+
export interface LLMToolDefinition {
|
|
16
|
+
type: 'function';
|
|
17
|
+
function: {
|
|
18
|
+
name: string;
|
|
19
|
+
description: string;
|
|
20
|
+
parameters: Record<string, unknown>;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** LLM Tool Call 结果 */
|
|
24
|
+
export interface LLMToolCall {
|
|
25
|
+
id: string;
|
|
26
|
+
function: {
|
|
27
|
+
name: string;
|
|
28
|
+
arguments: string;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** LLM 完整响应 */
|
|
32
|
+
export interface LLMResponse {
|
|
33
|
+
content: string | null;
|
|
34
|
+
toolCalls: LLMToolCall[];
|
|
35
|
+
finishReason?: string;
|
|
36
|
+
usage?: LLMUsage;
|
|
37
|
+
}
|
|
38
|
+
/** Token 用量 */
|
|
39
|
+
export interface LLMUsage {
|
|
40
|
+
promptTokens: number;
|
|
41
|
+
completionTokens: number;
|
|
42
|
+
totalTokens: number;
|
|
43
|
+
}
|
|
44
|
+
/** Streaming 事件类型 */
|
|
45
|
+
export type LLMStreamEventType = 'content_delta' | 'tool_call_delta' | 'done' | 'error';
|
|
46
|
+
/** Streaming 事件 */
|
|
47
|
+
export interface LLMStreamEvent {
|
|
48
|
+
type: LLMStreamEventType;
|
|
49
|
+
/** content_delta 时为文本片段 */
|
|
50
|
+
delta?: string;
|
|
51
|
+
/** tool_call_delta 时的信息 */
|
|
52
|
+
toolCall?: Partial<LLMToolCall> & {
|
|
53
|
+
index?: number;
|
|
54
|
+
};
|
|
55
|
+
/** done 时的完整响应 */
|
|
56
|
+
response?: LLMResponse;
|
|
57
|
+
/** error 时的错误信息 */
|
|
58
|
+
error?: string;
|
|
59
|
+
}
|
|
60
|
+
/** Streaming 回调 */
|
|
61
|
+
export type LLMStreamCallback = (event: LLMStreamEvent) => void;
|
|
62
|
+
/** LLM 错误类型 */
|
|
63
|
+
export type LLMErrorCode = 'auth_error' | 'rate_limit' | 'timeout' | 'network_error' | 'invalid_response' | 'aborted' | 'unknown';
|
|
64
|
+
/** LLM 自定义错误 */
|
|
65
|
+
export declare class LLMError extends Error {
|
|
66
|
+
code: LLMErrorCode;
|
|
67
|
+
retryable: boolean;
|
|
68
|
+
statusCode?: number;
|
|
69
|
+
constructor(message: string, code: LLMErrorCode, opts?: {
|
|
70
|
+
retryable?: boolean;
|
|
71
|
+
statusCode?: number;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/llm/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,sCAAsC,CAAC;AAMvF,eAAe;AACf,MAAM,WAAW,UAAU;IACzB,QAAQ,EAAE,0BAA0B,EAAE,CAAC;IACvC,KAAK,CAAC,EAAE,iBAAiB,EAAE,CAAC;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,aAAa;IACb,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,4CAA4C;AAC5C,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EAAE;QACR,IAAI,EAAE,MAAM,CAAC;QACb,WAAW,EAAE,MAAM,CAAC;QACpB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KACrC,CAAC;CACH;AAMD,uBAAuB;AACvB,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;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