OneBot: Leantime 改造版源码(BOM/Univer 表格/AI 接管/品牌替换等)

This commit is contained in:
wangruiguo
2026-09-03 18:49:20 +08:00
commit d647428529
3501 changed files with 1988906 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
<?php
namespace Leantime\Domain\Bom\Tools;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Server\Tools\ToolInputSchema;
use Laravel\Mcp\Server\Tools\ToolResult;
use Leantime\Domain\Bom\Services\Bom;
/**
* 给主数据BOM/工艺文件/工具清单)添加一个动态列。
*/
class AddMasterColumnTool extends Tool
{
public function __construct(
private Bom $bomService,
) {}
public function name(): string
{
return 'addMasterColumn';
}
public function description(): string
{
return '给主数据BOM/工艺文件/工具清单添加一个动态列自定义字段。key 为存储标识英文或中文label 为显示名。';
}
public function schema(ToolInputSchema $schema): ToolInputSchema
{
return $schema
->integer('id')->description('主数据 ID。')->required()
->string('key')->description('列标识(英文或中文,用于存储)。')->required()
->string('label')->description('列显示名,默认同 key。');
}
public function handle(array $arguments): ToolResult
{
$id = (int) ($arguments['id'] ?? 0);
$key = (string) ($arguments['key'] ?? '');
$label = (string) ($arguments['label'] ?? $key);
if ($key === '') {
return ToolResult::error('key 不能为空');
}
$columnId = $this->bomService->addColumn($id, $key, $label);
if ($columnId > 0) {
return ToolResult::text("动态列添加成功:{$key}columnId {$columnId})。");
}
return ToolResult::error('添加列失败可能无权限、key 冲突固定列、或主数据不存在)。');
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace Leantime\Domain\Bom\Tools;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Server\Tools\ToolInputSchema;
use Laravel\Mcp\Server\Tools\ToolResult;
use Leantime\Domain\Bom\Services\Bom;
/**
* 给主数据BOM/工艺文件/工具清单)添加一条明细行。
*/
class AddMasterItemTool extends Tool
{
public function __construct(
private Bom $bomService,
) {}
public function name(): string
{
return 'addMasterItem';
}
public function description(): string
{
return '给主数据BOM/工艺文件/工具清单添加一条明细行。固定列seq(序号)、partNo(零件编号)、partName(零件名称)、partDrawingNo(零件图号)、material(材质)、spec(零件规格)、qtyPerUnit(单件用量)、unit(单位)、process(工序)、remark(备注);动态列用 key 传值。';
}
public function schema(ToolInputSchema $schema): ToolInputSchema
{
return $schema
->integer('id')->description('主数据 ID。')->required()
->integer('seq')->description('序号。')
->string('partNo')->description('零件编号。')
->string('partName')->description('零件名称。')
->string('partDrawingNo')->description('零件图号。')
->string('material')->description('材质。')
->string('spec')->description('零件规格。')
->string('qtyPerUnit')->description('单件用量。')
->string('unit')->description('单位。')
->string('process')->description('工序。')
->string('remark')->description('备注。')
->raw('dynamicColumns', [
'type' => 'object',
'description' => '动态列的值,键为列 key值为字符串。',
'additionalProperties' => ['type' => 'string'],
]);
}
public function handle(array $arguments): ToolResult
{
$id = (int) ($arguments['id'] ?? 0);
$values = [
'seq' => (int) ($arguments['seq'] ?? 0),
'partNo' => (string) ($arguments['partNo'] ?? ''),
'partName' => (string) ($arguments['partName'] ?? ''),
'partDrawingNo' => (string) ($arguments['partDrawingNo'] ?? ''),
'material' => (string) ($arguments['material'] ?? ''),
'spec' => (string) ($arguments['spec'] ?? ''),
'qtyPerUnit' => (string) ($arguments['qtyPerUnit'] ?? ''),
'unit' => (string) ($arguments['unit'] ?? ''),
'process' => (string) ($arguments['process'] ?? ''),
'remark' => (string) ($arguments['remark'] ?? ''),
];
// 动态列
$dynamic = $arguments['dynamicColumns'] ?? [];
if (is_array($dynamic)) {
foreach ($dynamic as $k => $v) {
$values[(string) $k] = (string) $v;
}
}
$itemId = $this->bomService->saveItem($id, $values);
if ($itemId > 0) {
return ToolResult::text("明细行添加成功itemId {$itemId}");
}
return ToolResult::error('添加明细行失败(可能无权限或主数据不存在)。');
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace Leantime\Domain\Bom\Tools;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Server\Tools\ToolInputSchema;
use Laravel\Mcp\Server\Tools\ToolResult;
use Leantime\Domain\Bom\Services\Bom;
/**
* 创建全局主数据BOM/工艺文件/工具清单)。
*/
class CreateMasterTool extends Tool
{
public function __construct(
private Bom $bomService,
) {}
public function name(): string
{
return 'createMaster';
}
public function description(): string
{
return '创建一个全局主数据BOM/工艺文件/工具清单)。主数据是跨项目共享的全局资源,创建后所有项目可见。';
}
public function schema(ToolInputSchema $schema): ToolInputSchema
{
return $schema
->string('type')->description('主数据类型bom | process | tooling默认 bom。')
->string('bomNo')->description('编号。')->required()
->string('productName')->description('名称/产品名。')->required()
->string('specification')->description('规格型号。')
->string('drawingNo')->description('图号。')
->string('version')->description('版本。')
->string('remark')->description('备注。');
}
public function handle(array $arguments): ToolResult
{
$type = (string) ($arguments['type'] ?? 'bom');
if (! in_array($type, ['bom', 'process', 'tooling'], true)) {
$type = 'bom';
}
$id = $this->bomService->createBom([
'type' => $type,
'bomNo' => (string) ($arguments['bomNo'] ?? ''),
'productName' => (string) ($arguments['productName'] ?? ''),
'specification' => (string) ($arguments['specification'] ?? ''),
'drawingNo' => (string) ($arguments['drawingNo'] ?? ''),
'version' => (string) ($arguments['version'] ?? ''),
'remark' => (string) ($arguments['remark'] ?? ''),
]);
if ($id > 0) {
return ToolResult::text("主数据创建成功ID: {$id}(类型 {$type})。");
}
return ToolResult::error('主数据创建失败(可能无权限或参数不完整)。');
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace Leantime\Domain\Bom\Tools;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Server\Tools\ToolInputSchema;
use Laravel\Mcp\Server\Tools\ToolResult;
use Leantime\Domain\Bom\Services\Bom;
/**
* 删除全局主数据BOM/工艺文件/工具清单)——高风险写操作。
*
* 会级联删除该主数据的全部明细行、动态列、Teable 数据源。
* 调用方AI应先通过 getMasterDetail 列出将删除的内容并征得用户确认。
*/
class DeleteMasterTool extends Tool
{
public function __construct(
private Bom $bomService,
) {}
public function name(): string
{
return 'deleteMaster';
}
public function description(): string
{
return '删除一个全局主数据BOM/工艺文件/工具清单)。高风险操作:会级联删除其全部明细行、动态列、数据源,不可恢复。调用前必须先 getMasterDetail 列出内容并征得用户明确确认。';
}
public function schema(ToolInputSchema $schema): ToolInputSchema
{
return $schema
->integer('id')->description('要删除的主数据 ID。')->required()
->boolean('confirmed')->description('用户是否已明确确认删除。必须为 true 才执行。')->required();
}
public function handle(array $arguments): ToolResult
{
$id = (int) ($arguments['id'] ?? 0);
$confirmed = (bool) ($arguments['confirmed'] ?? false);
if (! $confirmed) {
return ToolResult::error('未确认删除。请先 getMasterDetail 列出将删除的内容,征得用户确认后再传 confirmed=true。');
}
$detail = $this->bomService->getBomDetail($id);
if ($detail === false) {
return ToolResult::error("主数据不存在或无权访问:{$id}");
}
$bom = $detail['bom'] ?? [];
$itemCount = count($detail['items'] ?? []);
$label = trim((string) ($bom['bomNo'] ?? '').' '.(string) ($bom['productName'] ?? ''));
if ($this->bomService->deleteBom($id)) {
return ToolResult::text("主数据已删除:{$label}ID {$id}),连同 {$itemCount} 行明细一并删除。");
}
return ToolResult::error('删除失败(可能无权限)。');
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Leantime\Domain\Bom\Tools;
use Illuminate\Support\Str;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Server\Tools\Annotations\IsReadOnly;
use Laravel\Mcp\Server\Tools\ToolInputSchema;
use Laravel\Mcp\Server\Tools\ToolResult;
use Leantime\Domain\Bom\Services\Bom;
/**
* 查看主数据BOM/工艺文件/工具清单)详情:含固定列 + 动态列 + 明细行 + 数据源。
*/
#[IsReadOnly]
class GetMasterDetailTool extends Tool
{
public function __construct(
private Bom $bomService,
) {}
public function name(): string
{
return 'getMasterDetail';
}
public function description(): string
{
return '查看主数据BOM/工艺文件/工具清单的完整明细含固定列、动态列、明细行、Teable 数据源。';
}
public function schema(ToolInputSchema $schema): ToolInputSchema
{
return $schema
->integer('id')->description('主数据 ID。')->required()
->integer('limit')->description('最多返回多少行明细,默认 50。');
}
public function handle(array $arguments): ToolResult
{
$id = (int) ($arguments['id'] ?? 0);
$limit = (int) ($arguments['limit'] ?? 50);
$detail = $this->bomService->getBomDetail($id);
if ($detail === false) {
return ToolResult::error("主数据不存在或无权访问:{$id}");
}
$bom = $detail['bom'] ?? [];
$columns = $detail['columns'] ?? [];
$items = $detail['items'] ?? [];
$result = [
'id' => (int) ($bom['id'] ?? 0),
'type' => (string) ($bom['type'] ?? 'bom'),
'bomNo' => (string) ($bom['bomNo'] ?? ''),
'productName' => Str::sanitizeForLLM((string) ($bom['productName'] ?? '')),
'version' => (string) ($bom['version'] ?? ''),
'columns' => array_map(fn ($c) => [
'key' => (string) $c['key'],
'label' => (string) ($c['label'] ?? $c['key']),
], $columns),
'itemCount' => count($items),
'items' => array_slice(array_map(function ($it) {
$row = [];
foreach ($it as $k => $v) {
if (is_scalar($v)) {
$row[$k] = Str::sanitizeForLLM((string) $v);
}
}
return $row;
}, $items), 0, $limit),
];
return ToolResult::json($result);
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace Leantime\Domain\Bom\Tools;
use Illuminate\Support\Str;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Server\Tools\Annotations\IsReadOnly;
use Laravel\Mcp\Server\Tools\ToolInputSchema;
use Laravel\Mcp\Server\Tools\ToolResult;
use Leantime\Domain\Bom\Services\Bom;
/**
* 列出全局主数据BOM / 工艺文件 / 工具清单)。
*/
#[IsReadOnly]
class ListMastersTool extends Tool
{
public function __construct(
private Bom $bomService,
) {}
public function name(): string
{
return 'listMasters';
}
public function description(): string
{
return '列出全局主数据BOM / 工艺文件 / 工具清单。主数据是跨项目共享的全局资源type 取值bom=物料清单、process=工艺文件、tooling=工具清单。';
}
public function schema(ToolInputSchema $schema): ToolInputSchema
{
return $schema
->string('type')->description('主数据类型bom | process | tooling默认 bom。');
}
public function handle(array $arguments): ToolResult
{
$type = (string) ($arguments['type'] ?? 'bom');
if (! in_array($type, ['bom', 'process', 'tooling'], true)) {
$type = 'bom';
}
$masters = $this->bomService->getMasters($type, 0);
if (empty($masters)) {
return ToolResult::text("{$type} 主数据。");
}
$rows = [];
foreach ($masters as $m) {
$rows[] = [
'id' => (int) $m['id'],
'type' => (string) ($m['type'] ?? 'bom'),
'bomNo' => (string) ($m['bomNo'] ?? ''),
'productName' => Str::sanitizeForLLM((string) ($m['productName'] ?? '')),
'version' => (string) ($m['version'] ?? ''),
'specification' => Str::sanitizeForLLM((string) ($m['specification'] ?? '')),
];
}
return ToolResult::json(['type' => $type, 'count' => count($rows), 'items' => $rows]);
}
}